Skip to main content

TypeContext

Trait TypeContext 

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>( &self, override: Handle<Override>, out: &mut W, ) -> Result<(), Error> where W: Write; fn write_unnamed_struct<W>( &self, inner: &TypeInner, out: &mut W, ) -> Result<(), Error> where W: Write; // Provided methods fn write_non_wgsl_inner<W>( &self, inner: &TypeInner, out: &mut W, ) -> Result<(), Error> where W: Write { ... } fn write_non_wgsl_scalar<W>( &self, scalar: Scalar, out: &mut W, ) -> Result<(), Error> where W: Write { ... } fn write_type<W>( &self, handle: Handle<Type>, out: &mut W, ) -> Result<(), Error> where W: Write { ... } fn write_type_inner<W>( &self, inner: &TypeInner, out: &mut W, ) -> Result<(), Error> where W: Write { ... } fn write_scalar<W>(&self, scalar: Scalar, out: &mut W) -> Result<(), Error> where W: Write { ... } fn write_type_resolution<W>( &self, resolution: &TypeResolution, out: &mut W, ) -> Result<(), Error> where W: Write { ... } fn write_type_conclusion<W>( &self, conclusion: &Conclusion, out: &mut W, ) -> Result<(), Error> where W: Write { ... } fn write_type_rule<W>( &self, name: &str, rule: &Rule, out: &mut W, ) -> Result<(), Error> where W: Write { ... } 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 { ... }
}
Available on crate feature unstable-wgpu-27 only.
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§

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

Return the Type referred to by handle.

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

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

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

Write the WGSL form of override to out.

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

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§

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

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.

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

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.

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

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.

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

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.

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

Write the Scalar scalar as a WGSL type.

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

Write the TypeResolution resolution as a WGSL type.

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

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

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

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§

§

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.

§

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.