Skip to main content

Compiler

Struct Compiler 

Source
pub struct Compiler<R = ()> {
    pub options: CompileOptions,
    pub resolver: R,
}
Expand description

The WESL compiler.

§Basic Usage

let compiler = Compiler::default();
let wgsl_string = compiler
    .compile("path/to/shader.wgsl")
    .inspect_err(|e| eprintln!("{e}")) // pretty-print errors
    .unwrap()
    .to_string();

Fields§

§options: CompileOptions§resolver: R

Implementations§

Source§

impl<R1> Compiler<R1>

Source

pub fn with_resolver<R2>(self, resolver: R2) -> Compiler<R2>

Set the compilation crate::Resolver or crate::AsyncResolver.

Source§

impl<R> Compiler<R>

Source

pub fn new_with_resolver(options: CompileOptions, resolver: R) -> Self

Shorthand for Compiler::new(options).with_resolver(resolver).

Source§

impl Compiler<()>

Source

pub fn new(options: CompileOptions) -> Self

Create a new compiler.

By default, the compiler will use a StandardResolver when compiling.

Source§

impl Compiler<()>

Source

pub fn compile(&self, path: impl AsRef<Path>) -> Result<CompileResult, Error>

Compile a WESL shader to WGSL.

path defines where to look for shader files. It can point to a wesl.toml file, a directory or a shader file.

The main module (which exposes entry points, bindings and overrides) is determined from path: It is the package root module if path points to a toml file or a directory, otherwise it is the file that path points to. See Self::compile_module to compile a different main module.

PathPackage root directoryMain module
wesl.tomlroot field in wesl.tomlpackage.wesl in root dir
directorythe directorypackage.wesl in root dir
.wesl filethe parent directorythe file specified

Note: .wgsl extensions are also supported, but .wesl takes priority.

Source

pub fn compile_module( &self, pkg_root_dir: impl AsRef<Path>, main_path: &ModulePath, ) -> Result<CompileResult, Error>

Variant of Self::compile with a custom main module path.

Source

pub async fn compile_async(&self, path: &Path) -> Result<CompileResult, Error>

Async version of Self::compile.

Source

pub async fn compile_module_async( &self, pkg_root_dir: impl AsRef<Path>, main_path: &ModulePath, ) -> Result<CompileResult, Error>

Async version of Self::compile_module.

Source§

impl<R: Resolver> Compiler<R>

Source

pub fn compile_root(&self) -> Result<CompileResult, Error>

Compile a WESL shader to WGSL.

The main module defaults to the package root module. See Self::compile_module to compile a different main module.

Source

pub fn compile_module( &self, main_path: &ModulePath, ) -> Result<CompileResult, Error>

Variant of Self::compile with a custom main module path.

Source

pub fn compile( &self, fs_main_path: impl AsRef<Path>, ) -> Result<CompileResult, Error>

Variant of Self::compile_root with a custom main module path.

fs_main_path defines the main module path according to the resolver’s file system mapping implemented in Resolver::module_path.

§Warning

This function works best with filesystem resolvers which implement Resolver::module_path. If not, this function assumes that the package root directory is the current working directory.

§Panics

Can panic if ModulePath::from_path fails.

Source

pub async fn compile_root_async(&self) -> Result<CompileResult, Error>

Async version of Self::compile_root.

Source

pub async fn compile_module_async( &self, main_path: &ModulePath, ) -> Result<CompileResult, Error>

Async version of Self::compile_module.

Source

pub async fn compile_async( &self, fs_main_path: &impl AsRef<Path>, ) -> Result<CompileResult, Error>

Async version of Self::compile.

Trait Implementations§

Source§

impl<R: Clone> Clone for Compiler<R>

Source§

fn clone(&self) -> Compiler<R>

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<R: Debug> Debug for Compiler<R>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Compiler<()>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<R> Freeze for Compiler<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for Compiler<R>
where R: RefUnwindSafe,

§

impl<R> Send for Compiler<R>
where R: Send,

§

impl<R> Sync for Compiler<R>
where R: Sync,

§

impl<R> Unpin for Compiler<R>
where R: Unpin,

§

impl<R> UnsafeUnpin for Compiler<R>
where R: UnsafeUnpin,

§

impl<R> UnwindSafe for Compiler<R>
where R: UnwindSafe,

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> 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> 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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.