Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Supported Rust Subset

wgsl-rs transpiles a deliberately constrained subset of Rust to WGSL. The macro is additive: it never translates or rewrites user code. Constructs that cannot map cleanly to WGSL are rejected at compile time rather than approximated.

Supported

  • Structs (including generic and const-generic structs)
  • Enums with #[repr(u32)]
  • impl blocks (free functions in WGSL), including trait impls and generic trait impls on arrays
  • Free functions
  • const items
  • let and let mut bindings
  • if / else, while, loop, for, match
  • All binary, unary, and compound assignment operators
  • Arrays
  • Generic functions and generic structs (monomorphized)
  • Const generic parameters (const N: usize / const N: u32) on functions, structs, impl blocks, and template entry points

Not Supported

FeatureReasonWorkaround
Trait definitionsTraits are Rust-only; impls generate WGSL functionsUse concrete types in function signatures
Borrowing / refsWGSL has no borrow semanticsUse the ptr! macro for pointer types
Arbitrary importsModule mapping is glob-onlyUse use crate::module::*;
ClosuresNo closure capture model in WGSLWrite named functions
asyncNo async runtime on GPU
Dynamic dispatchNo vtables in WGSLUse enums or monomorphization
QSelf call syntax<[u32; 4]>::method() not yet supportedUse T::method() via monomorphization (#131)

Feature Table

FeatureSupported?Notes
StructsYesIncluding generic and const-generic
EnumsYesRequires #[repr(u32)]
impl blocksYesBecome free WGSL functions
Trait implsYesMethods become mangled WGSL functions
Generic array implsYesimpl<T: Trait> Trait for [T; N] (#133)
Free functionsYes
const itemsYes
let / let mutYes
if / elseYes
whileYes
loopYes
forYes
matchYesSee non_literal_match_statement_patterns allow
Binary operatorsYes
Unary operatorsYes
Compound assignmentsYes
ArraysYes
Generic functionsYesMonomorphized at macro time
Generic structsYesMonomorphized
Const genericsYesu32/usize only, monomorphized or instantiated
TraitsNoDefinitions are Rust-only; impls are supported
Borrowing / referencesNoUse ptr! macro
Arbitrary importsNoGlob only
ClosuresNo
asyncNo
Dynamic dispatchNo
QSelf call syntaxNo<[u32; 4]>::method() (#131)