44//! version. Then with that we `git checkout` the `rust-gpu` repo that corresponds to that version.
55//! From there we can look at the source code to get the required Rust toolchain.
66
7+ // TODO: remove this & fix documentation
8+ #![ expect( clippy:: missing_errors_doc, reason = "temporary allow" ) ]
9+
710use std:: {
811 fs,
912 path:: { Path , PathBuf } ,
@@ -15,11 +18,13 @@ use cargo_metadata::{
1518 semver:: Version ,
1619 Metadata , MetadataCommand , Package ,
1720} ;
18- use rustc_codegen_spirv_cache:: cache:: cache_dir;
21+
22+ use crate :: cache:: cache_dir;
1923
2024#[ expect(
25+ rustdoc:: bare_urls,
2126 clippy:: doc_markdown,
22- reason = "The URL should appear literally like this. But Clippy wants a markdown clickable link"
27+ reason = "The URL should appear literally like this. But Clippy & rustdoc want a markdown clickable link"
2328) ]
2429/// The source and version of `rust-gpu`.
2530/// Eg:
@@ -29,6 +34,7 @@ use rustc_codegen_spirv_cache::cache::cache_dir;
2934/// - a revision of "abc213"
3035/// * a local Path
3136#[ derive( Eq , PartialEq , Clone , Debug ) ]
37+ #[ non_exhaustive]
3238pub enum SpirvSource {
3339 /// If the shader specifies a simple version like `spirv-std = "0.9.0"` then the source of
3440 /// `rust-gpu` is the conventional crates.io version.
@@ -58,6 +64,7 @@ impl core::fmt::Display for SpirvSource {
5864 clippy:: min_ident_chars,
5965 reason = "It's a core library trait implementation"
6066 ) ]
67+ #[ inline]
6168 fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
6269 match self {
6370 Self :: CratesIO ( version) => version. fmt ( f) ,
@@ -79,6 +86,7 @@ impl core::fmt::Display for SpirvSource {
7986
8087impl SpirvSource {
8188 /// Figures out which source of `rust-gpu` to use
89+ #[ inline]
8290 pub fn new (
8391 shader_crate_path : & Path ,
8492 maybe_rust_gpu_source : Option < & str > ,
@@ -105,6 +113,7 @@ impl SpirvSource {
105113 }
106114
107115 /// Look into the shader crate to get the version of `rust-gpu` it's using.
116+ #[ inline]
108117 pub fn get_rust_gpu_deps_from_shader ( shader_crate_path : & Path ) -> anyhow:: Result < Self > {
109118 let crate_metadata = query_metadata ( shader_crate_path) ?;
110119 let spirv_std_package = crate_metadata. find_package ( "spirv-std" ) ?;
@@ -120,19 +129,25 @@ impl SpirvSource {
120129 /// Convert the `SpirvSource` to a cache directory in which we can build it.
121130 /// It needs to be dynamically created because an end-user might want to swap out the source,
122131 /// maybe using their own fork for example.
132+ #[ inline]
123133 pub fn install_dir ( & self ) -> anyhow:: Result < PathBuf > {
124134 match self {
125135 Self :: Path {
126136 rust_gpu_repo_root, ..
127137 } => Ok ( rust_gpu_repo_root. as_std_path ( ) . to_owned ( ) ) ,
128138 Self :: CratesIO { .. } | Self :: Git { .. } => {
129- let dir = crate :: to_dirname ( self . to_string ( ) . as_ref ( ) ) ;
139+ let dir = to_dirname ( self . to_string ( ) . as_ref ( ) ) ;
130140 Ok ( cache_dir ( ) ?. join ( "codegen" ) . join ( dir) )
131141 }
132142 }
133143 }
134144
135145 /// Returns true if self is a Path
146+ #[ expect(
147+ clippy:: must_use_candidate,
148+ reason = "calculations are cheap, `bool` is `Copy`"
149+ ) ]
150+ #[ inline]
136151 pub const fn is_path ( & self ) -> bool {
137152 matches ! ( self , Self :: Path { .. } )
138153 }
@@ -191,7 +206,21 @@ impl SpirvSource {
191206 }
192207}
193208
209+ /// Returns a string suitable to use as a directory.
210+ ///
211+ /// Created from the spirv-builder source dep and the rustc channel.
212+ fn to_dirname ( text : & str ) -> String {
213+ text. replace (
214+ [ std:: path:: MAIN_SEPARATOR , '\\' , '/' , '.' , ':' , '@' , '=' ] ,
215+ "_" ,
216+ )
217+ . split ( [ '{' , '}' , ' ' , '\n' , '"' , '\'' ] )
218+ . collect :: < Vec < _ > > ( )
219+ . concat ( )
220+ }
221+
194222/// get the Package metadata from some crate
223+ #[ inline]
195224pub fn query_metadata ( crate_path : & Path ) -> anyhow:: Result < Metadata > {
196225 log:: debug!( "Running `cargo metadata` on `{}`" , crate_path. display( ) ) ;
197226 let metadata = MetadataCommand :: new ( )
@@ -211,6 +240,7 @@ pub trait FindPackage {
211240}
212241
213242impl FindPackage for Metadata {
243+ #[ inline]
214244 fn find_package ( & self , crate_name : & str ) -> anyhow:: Result < & Package > {
215245 if let Some ( package) = self
216246 . packages
@@ -229,6 +259,7 @@ impl FindPackage for Metadata {
229259}
230260
231261/// Parse the `rust-toolchain.toml` in the working tree of the checked-out version of the `rust-gpu` repo.
262+ #[ inline]
232263pub fn get_channel_from_rustc_codegen_spirv_build_script (
233264 rustc_codegen_spirv_package : & Package ,
234265) -> anyhow:: Result < String > {
@@ -257,9 +288,14 @@ mod test {
257288 use cargo_metadata:: { PackageBuilder , PackageId , Source } ;
258289 use cargo_util_schemas:: manifest:: PackageName ;
259290
291+ pub fn shader_crate_template_path ( ) -> std:: path:: PathBuf {
292+ let project_base = std:: path:: PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" ) ) ;
293+ project_base. join ( "../shader-crate-template" )
294+ }
295+
260296 #[ test_log:: test]
261297 fn parsing_spirv_std_dep_for_shader_template ( ) {
262- let shader_template_path = crate :: test :: shader_crate_template_path ( ) ;
298+ let shader_template_path = shader_crate_template_path ( ) ;
263299 let source = SpirvSource :: get_rust_gpu_deps_from_shader ( & shader_template_path) . unwrap ( ) ;
264300 assert_eq ! (
265301 source,
@@ -278,7 +314,7 @@ mod test {
278314
279315 #[ test_log:: test]
280316 fn cached_checkout_dir_sanity ( ) {
281- let shader_template_path = crate :: test :: shader_crate_template_path ( ) ;
317+ let shader_template_path = shader_crate_template_path ( ) ;
282318 let source = SpirvSource :: get_rust_gpu_deps_from_shader ( & shader_template_path) . unwrap ( ) ;
283319 let dir = source. install_dir ( ) . unwrap ( ) ;
284320 let name = dir
0 commit comments