//! Generating WGSL source code for Naga IR types.
use alloc::format; use alloc::string::{String, ToString};
/// Types that can return the WGSL source representation of their /// values as a `'static` string. /// /// This trait is specifically for types whose WGSL forms are simple /// enough that they can always be returned as a static string. /// /// - If only some values have a WGSL representation, consider /// implementing [`TryToWgsl`] instead. /// /// - If a type's WGSL form requires dynamic formatting, so that /// returning a `&'static str` isn't feasible, consider implementing /// [`core::fmt::Display`] on some wrapper type instead. pubtrait ToWgsl: Sized { /// Return WGSL source code representation of `self`. fn to_wgsl(self) -> &'static str;
}
/// Types that may be able to return the WGSL source representation /// for their values as a `'static` string. /// /// This trait is specifically for types whose values are either /// simple enough that their WGSL form can be represented a static /// string, or aren't representable in WGSL at all. /// /// - If all values in the type have `&'static str` representations in /// WGSL, consider implementing [`ToWgsl`] instead. /// /// - If a type's WGSL form requires dynamic formatting, so that /// returning a `&'static str` isn't feasible, consider implementing /// [`core::fmt::Display`] on some wrapper type instead. pubtrait TryToWgsl: Sized { /// Return the WGSL form of `self` as a `'static` string. /// /// If `self` doesn't have a representation in WGSL (standard or /// as extended by Naga), then return `None`. fn try_to_wgsl(self) -> Option<&'static str>;
/// What kind of WGSL thing `Self` represents. const DESCRIPTION: &'static str;
/// Return the WGSL form of `self` as appropriate for diagnostics. /// /// If `self` can be expressed in WGSL, return that form as a /// [`String`]. Otherwise, return some representation of `self` /// that is appropriate for use in diagnostic messages. /// /// The default implementation of this function falls back to /// `self`'s [`Debug`] form. /// /// [`Debug`]: core::fmt::Debug fn to_wgsl_for_diagnostics(self) -> String where Self: core::fmt::Debug + Copy,
{ matchself.try_to_wgsl() {
Some(static_string) => static_string.to_string(),
None => format!("{{non-WGSL {} {self:?}}}", Self::DESCRIPTION),
}
}
}
/// Return the WGSL address space and access mode strings for `space`. /// /// Why don't we implement [`ToWgsl`] for [`AddressSpace`]? /// /// In WGSL, the full form of a pointer type is `ptr<AS, T, AM>`, where: /// - `AS` is the address space, /// - `T` is the store type, and /// - `AM` is the access mode. /// /// Since the type `T` intervenes between the address space and the /// access mode, there isn't really any individual WGSL grammar /// production that corresponds to an [`AddressSpace`], so [`ToWgsl`] /// is too simple-minded for this case. /// /// Furthermore, we want to write `var<AS[, AM]>` for most address /// spaces, but we want to just write `var foo: T` for handle types. /// /// [`AddressSpace`]: crate::AddressSpace pubconstfn address_space_str(
space: crate::AddressSpace,
) -> (Option<&'static str>, Option<&'static str>) { usecrate::AddressSpace asAs;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.