Skip to main content

Source

Struct Source 

Source
pub struct Source {
    pub id: u64,
    pub name: &'static str,
    pub imports: &'static [&'static Source],
    pub ir_constructor: fn() -> Module,
    pub templates: &'static [GenericTemplate],
    pub instantiations: &'static [TemplateInstantiation],
    pub module_type_params: &'static [&'static str],
}
Expand description

A WGSL source.

A Source is the public-facing spec for a #[wgsl] mod foo { ... } block: the macro emits one pub static foo::WGSL_SOURCE: Source per module. WGSL itself doesn’t support importing modules, but wgsl-rs does (glob imports only), so a Source can import other Sources.

Concrete and template sources share the same type. Templates are detected at runtime via Source::is_template; a template source’s Source::wgsl_source returns SourceError::TemplateWgsl and the caller must use the macro-emitted instantiate::<…>() to obtain a concrete ir::Module first.

ⓘ
#[wgsl]
pub mod constants {
    pub const NUMBER: u32 = 1234;
}
#[wgsl]
pub mod main {
    use super::constants::*;

    fn add_to_number(n: u32) -> u32 {
        n + NUMBER
    }
}

Fields§

§id: u64

Unique identifier for this source, assigned at proc-macro expansion time from the consuming compilation unit’s identity (Cargo package name + version, plus crate/bin target names) and a per-unit counter (wgsl-rs#165). Exactly unique within one compilation unit; across units separated by a 64-bit hash, so collisions are astronomically unlikely but not impossible. Used to deduplicate sources during source assembly (diamond import graphs, same-named sources from different paths).

§name: &'static str

Name of the source.

§imports: &'static [&'static Source]

Imports of other WGSL sources.

§ir_constructor: fn() -> Module

Constructor function that builds this module’s IR. Called lazily by wgsl_source() / instantiate().

The returned IR is owned and freshly constructed on each call; the caller is free to clone, mutate, or substitute over it.

§templates: &'static [GenericTemplate]

Generic function templates defined in this module.

Consuming modules reference these via instantiations to produce monomorphized functions at source-assembly time. The IR is held as a constructor, just like the parent module’s IR.

§instantiations: &'static [TemplateInstantiation]

Cross-module template instantiations.

Each entry references a template from an imported module and provides the concrete IR type arguments to substitute for the type parameters. These are resolved at source-assembly time in wgsl_source().

§module_type_params: &'static [&'static str]

Module-level type parameters, in instantiation order.

Two distinct kinds of declaration contribute to this list:

  • impl Trait linkage variables, e.g. uniform!(group(0), binding(0), FRAME: impl Convert<f32>). Each such declaration contributes the variable’s identifier ("FRAME").
  • Generic shader entry points, e.g. pub fn frag_main<T: Convert<f32>>() -> Vec4f. Each one of an entry point’s type parameters contributes a positional name of the form "<fn_name>_<index>" (e.g. "frag_main_0"). Source names like T are intentionally not used so that two entry points sharing a letter don’t collide.

The two groups appear in declaration order: linkage variables first, then entry points.

When this slice is empty, the module is concrete and Self::wgsl_source produces a directly usable WGSL source. When it is non-empty, callers should use the instantiate function generated alongside the source, which uses wgsl_rs::linkage::Type<Is = ...> constraints to check at compile time that every parameter has been bound.

Implementations§

Source§

impl Source

Source

pub fn is_template(&self) -> bool

Returns whether this source is a template — i.e. has unresolved module-level type parameters.

Template sources cannot be used as shader sources directly; call instantiate(...) to produce a concrete WGSL source.

Source

pub fn wgsl_source(&self) -> Result<String, SourceError<'_>>

Returns the assembled WGSL source for this source and all its imports, including cross-source template instantiations.

If this source is a template, the returned error is SourceError::TemplateWgsl; the caller must use the macro-emitted instantiate::<…>() to obtain a concrete ir::Module first and then call ir::Module::wgsl_source.

Source§

impl Source

Source

pub fn validate(&self) -> Result<(), String>

Validates the concatenated WGSL source of this source and its imports.

This method concatenates the WGSL source from this source and all its imports (recursively), then validates the result using naga.

§Returns

Returns Ok(()) if validation succeeds, or an error message describing the validation failure. Template sources return an Err(String) instructing the caller to instantiate::<…>() first (they cannot be validated standalone — their IR contains __TP{name}__ placeholders that aren’t valid WGSL).

§Example
ⓘ
#[wgsl]
pub mod my_shader {
    // ...
}

// Validate at runtime
my_shader::WGSL_SOURCE.validate().expect("WGSL validation failed");

Trait Implementations§

Source§

impl Clone for Source

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Source

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AnySendSync for T
where T: Any + Send + Sync,

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Type for T

Source§

type Is = T

The associated type, which for the blanket impl is the type itself.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,