naga::common::wgsl::types

Trait TypeContext

Source
pub trait TypeContext {
Show 15 methods // Required methods fn lookup_type(&self, handle: Handle<Type>) -> &Type; fn type_name(&self, handle: Handle<Type>) -> &str; fn write_override<W: Write>( &self, override: Handle<Override>, out: &mut W, ) -> Result; fn write_unnamed_struct<W: Write>( &self, inner: &TypeInner, out: &mut W, ) -> Result; // Provided methods fn write_non_wgsl_inner<W: Write>( &self, inner: &TypeInner, out: &mut W, ) -> Result { ... } fn write_non_wgsl_scalar<W: Write>( &self, scalar: Scalar, out: &mut W, ) -> Result { ... } fn write_type<W: Write>(&self, handle: Handle<Type>, out: &mut W) -> Result { ... } fn write_type_inner<W: Write>( &self, inner: &TypeInner, out: &mut W, ) -> Result { ... } fn write_scalar<W: Write>(&self, scalar: Scalar, out: &mut W) -> Result { ... } fn write_type_resolution<W: Write>( &self, resolution: &TypeResolution, out: &mut W, ) -> Result { ... } fn write_type_conclusion<W: Write>( &self, conclusion: &Conclusion, out: &mut W, ) -> Result { ... } fn write_type_rule<W: Write>( &self, name: &str, rule: &Rule, out: &mut W, ) -> Result { ... } fn type_to_string(&self, handle: Handle<Type>) -> String { ... } fn type_resolution_to_string(&self, resolution: &TypeResolution) -> String { ... } fn type_rule_to_string(&self, name: &str, rule: &Rule) -> String { ... }
}
Expand description

A context for printing Naga IR types as WGSL.

This trait’s default methods write_type and write_type_inner do the work of formatting types as WGSL. Implementors must provide the remaining methods, to customize behavior for the context at hand.

For example, the WGSL backend would provide an implementation of type_name that handles hygienic renaming, whereas the WGSL front end would simply show the name that was given in the source.

Required Methods§

Source

fn lookup_type(&self, handle: Handle<Type>) -> &Type

Return the Type referred to by handle.

Source

fn type_name(&self, handle: Handle<Type>) -> &str

Return the name to be used for the type referred to by handle.

Source

fn write_override<W: Write>( &self, override: Handle<Override>, out: &mut W, ) -> Result

Write the WGSL form of override to out.

Source

fn write_unnamed_struct<W: Write>( &self, inner: &TypeInner, out: &mut W, ) -> Result

Write a TypeInner::Struct for which we are unable to find a name.

The names of struct types are only available if we have Handle<Type>, not from TypeInner. For logging and debugging, it’s fine to just write something helpful to the developer, but for generating WGSL, this should be unreachable.

Provided Methods§

Source

fn write_non_wgsl_inner<W: Write>( &self, inner: &TypeInner, out: &mut W, ) -> Result

Write a TypeInner that has no representation as WGSL source, even including Naga extensions.

A backend might implement this with a call to the unreachable! macro, since backends are allowed to assume that the module has passed validation.

The default implementation is appropriate for generating type names to appear in error messages. It punts to TypeInner’s core::fmt::Debug implementation, since it’s probably best to show the user something they can act on.

Source

fn write_non_wgsl_scalar<W: Write>(&self, scalar: Scalar, out: &mut W) -> Result

Write a Scalar that has no representation as WGSL source, even including Naga extensions.

A backend might implement this with a call to the unreachable! macro, since backends are allowed to assume that the module has passed validation.

The default implementation is appropriate for generating type names to appear in error messages. It punts to Scalar’s core::fmt::Debug implementation, since it’s probably best to show the user something they can act on.

Source

fn write_type<W: Write>(&self, handle: Handle<Type>, out: &mut W) -> Result

Write the type ty as it would appear in a value’s declaration.

Write the type referred to by ty in module as it would appear in a var, let, etc. declaration, or in a function’s argument list.

Source

fn write_type_inner<W: Write>(&self, inner: &TypeInner, out: &mut W) -> Result

Write the TypeInner inner as it would appear in a value’s declaration.

Write inner as it would appear in a var, let, etc. declaration, or in a function’s argument list.

Note that this cannot handle writing Struct types: those must be referred to by name, but the name isn’t available in TypeInner.

Source

fn write_scalar<W: Write>(&self, scalar: Scalar, out: &mut W) -> Result

Write the Scalar scalar as a WGSL type.

Source

fn write_type_resolution<W: Write>( &self, resolution: &TypeResolution, out: &mut W, ) -> Result

Write the TypeResolution resolution as a WGSL type.

Source

fn write_type_conclusion<W: Write>( &self, conclusion: &Conclusion, out: &mut W, ) -> Result

Source

fn write_type_rule<W: Write>( &self, name: &str, rule: &Rule, out: &mut W, ) -> Result

Source

fn type_to_string(&self, handle: Handle<Type>) -> String

Source

fn type_resolution_to_string(&self, resolution: &TypeResolution) -> String

Source

fn type_rule_to_string(&self, name: &str, rule: &Rule) -> String

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl TypeContext for UniqueArena<Type>

Format types as WGSL based on a UniqueArena<Type>.

This is probably only good enough for logging:

  • It does not apply any kind of Namer renamings.

  • It generates invalid WGSL for anonymous struct types.

  • It doesn’t write override-sized arrays properly.

Source§

impl TypeContext for WriterTypeContext<'_>

Source§

impl TypeContext for ExpressionContext<'_, '_, '_>

Source§

impl TypeContext for GlobalCtx<'_>

Format types as WGSL based on a GlobalCtx.

This is probably good enough for diagnostic output, but it has some limitations:

  • It does not apply Namer renamings, to avoid collisions.

  • It generates invalid WGSL for anonymous struct types.

  • It doesn’t write the lengths of override-expression-sized arrays correctly, unless the expression is just the override identifier.