From 1b7dfc9814281cb495182b8204fc24a7609a93c6 Mon Sep 17 00:00:00 2001 From: Firestar99 <31222740+Firestar99@users.noreply.github.com> Date: Sun, 3 Mar 2024 15:47:23 +0100 Subject: [PATCH] shader struct name replacing (#2484) Co-authored-by: Firestar99 <4696087-firestar99@users.noreply.gitlab.com> --- vulkano-shaders/src/structs.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/vulkano-shaders/src/structs.rs b/vulkano-shaders/src/structs.rs index 6b8f986a..06aa2bfc 100644 --- a/vulkano-shaders/src/structs.rs +++ b/vulkano-shaders/src/structs.rs @@ -690,9 +690,15 @@ impl TypeStruct { .iter() .find_map(|instruction| match instruction { Instruction::Name { name, .. } => { - // rust-gpu uses fully qualified rust paths as names which contain `:`. - // I sady don't know how to check against all kinds of invalid ident chars. - let name = name.replace(':', "_"); + // Replace chars that could potentially cause the ident to be invalid with "_". + // For example, Rust-GPU names structs by their fully qualified rust name (e.g. + // "foo::bar::MyStruct") in which the ":" is an invalid character for idents. + let mut name = + name.replace(|c: char| !(c.is_ascii_alphanumeric() || c == '_'), "_"); + if name.starts_with(|c: char| !c.is_ascii_alphabetic()) { + name.insert(0, '_'); + } + // Worst case: invalid idents will get the UnnamedX name below syn::parse_str(&name).ok() }