Skip to content

Commit

Permalink
make wgsl_struct const
Browse files Browse the repository at this point in the history
  • Loading branch information
victorvde committed Jul 26, 2023
1 parent 64fd334 commit 2dfd7f8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
10 changes: 5 additions & 5 deletions derive/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,11 +674,11 @@ pub fn derive_shader_type(input: DeriveInput, root: &Path) -> TokenStream {

impl #impl_generics #root::WgslStruct for #name #ty_generics
{
fn wgsl_struct() -> ::std::string::String {
::std::format!("struct {} {{\n", #name_string)
#( + &::std::format!("{} {}: {},\n", #field_layout_attributes, #field_strings, #field_wgsl_types) )*
+ "}\n"
}
const WGSL_STRUCT: &'static ::core::primitive::str =
#root::ConstStr::new()
.str("struct ").str(#name_string).str(" {\n")
#( .str(#field_layout_attributes).str(" ").str(#field_strings).str(": ").str(#field_wgsl_types).str(",\n") )*
.str("}\n").as_str();
}

#extra
Expand Down
4 changes: 2 additions & 2 deletions src/core/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,6 @@ pub trait CreateFrom: Sized {
}

pub trait WgslStruct {
/// Returns the [WGSL struct](https://www.w3.org/TR/WGSL/#struct-types) definition for the implementing Rust struct
fn wgsl_struct() -> String;
/// The [WGSL struct](https://www.w3.org/TR/WGSL/#struct-types) definition for the implementing Rust struct
const WGSL_STRUCT: &'static str;
}
2 changes: 1 addition & 1 deletion tests/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ fn all_types() {
fn wgsl_struct() {
assert_eq!(A::WGSL_TYPE, "A");
assert_eq!(
A::wgsl_struct(),
A::WGSL_STRUCT,
"struct A {
f: f32,
u: u32,
Expand Down
5 changes: 3 additions & 2 deletions tests/wgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ fn test_wgpu() {
in_buffer.write(&b).unwrap();
assert_eq!(in_byte_buffer.len(), b.size().get() as _);

let shader_text = A::wgsl_struct() + &B::wgsl_struct() + include_str!("./shaders/general.wgsl");
let shader_text =
A::WGSL_STRUCT.to_string() + &B::WGSL_STRUCT + include_str!("./shaders/general.wgsl");
let shader = ShaderModuleDescriptor {
label: Some("./shaders/general.wgsl"),
source: ShaderSource::Wgsl(shader_text.into()),
Expand Down Expand Up @@ -143,7 +144,7 @@ fn array_length() {
in_buffer.write(&in_value).unwrap();
assert_eq!(in_byte_buffer.len(), in_value.size().get() as _);

let shader_text = A::wgsl_struct() + include_str!("./shaders/array_length.wgsl");
let shader_text = A::WGSL_STRUCT.to_string() + include_str!("./shaders/array_length.wgsl");
let shader = ShaderModuleDescriptor {
label: Some("./shaders/array_length.wgsl"),
source: ShaderSource::Wgsl(shader_text.into()),
Expand Down

0 comments on commit 2dfd7f8

Please sign in to comment.