4
4
//! version. Then with that we `git checkout` the `rust-gpu` repo that corresponds to that version.
5
5
//! From there we can look at the source code to get the required Rust toolchain.
6
6
7
+ // TODO: remove this & fix documentation
8
+ #![ expect( clippy:: missing_errors_doc, reason = "temporary allow" ) ]
9
+
7
10
use std:: {
8
11
fs,
9
12
path:: { Path , PathBuf } ,
@@ -15,11 +18,13 @@ use cargo_metadata::{
15
18
semver:: Version ,
16
19
Metadata , MetadataCommand , Package ,
17
20
} ;
18
- use rustc_codegen_spirv_cache:: cache:: cache_dir;
21
+
22
+ use crate :: cache:: cache_dir;
19
23
20
24
#[ expect(
25
+ rustdoc:: bare_urls,
21
26
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"
23
28
) ]
24
29
/// The source and version of `rust-gpu`.
25
30
/// Eg:
@@ -29,6 +34,7 @@ use rustc_codegen_spirv_cache::cache::cache_dir;
29
34
/// - a revision of "abc213"
30
35
/// * a local Path
31
36
#[ derive( Eq , PartialEq , Clone , Debug ) ]
37
+ #[ non_exhaustive]
32
38
pub enum SpirvSource {
33
39
/// If the shader specifies a simple version like `spirv-std = "0.9.0"` then the source of
34
40
/// `rust-gpu` is the conventional crates.io version.
@@ -58,6 +64,7 @@ impl core::fmt::Display for SpirvSource {
58
64
clippy:: min_ident_chars,
59
65
reason = "It's a core library trait implementation"
60
66
) ]
67
+ #[ inline]
61
68
fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
62
69
match self {
63
70
Self :: CratesIO ( version) => version. fmt ( f) ,
@@ -79,6 +86,7 @@ impl core::fmt::Display for SpirvSource {
79
86
80
87
impl SpirvSource {
81
88
/// Figures out which source of `rust-gpu` to use
89
+ #[ inline]
82
90
pub fn new (
83
91
shader_crate_path : & Path ,
84
92
maybe_rust_gpu_source : Option < & str > ,
@@ -105,6 +113,7 @@ impl SpirvSource {
105
113
}
106
114
107
115
/// Look into the shader crate to get the version of `rust-gpu` it's using.
116
+ #[ inline]
108
117
pub fn get_rust_gpu_deps_from_shader ( shader_crate_path : & Path ) -> anyhow:: Result < Self > {
109
118
let crate_metadata = query_metadata ( shader_crate_path) ?;
110
119
let spirv_std_package = crate_metadata. find_package ( "spirv-std" ) ?;
@@ -120,19 +129,25 @@ impl SpirvSource {
120
129
/// Convert the `SpirvSource` to a cache directory in which we can build it.
121
130
/// It needs to be dynamically created because an end-user might want to swap out the source,
122
131
/// maybe using their own fork for example.
132
+ #[ inline]
123
133
pub fn install_dir ( & self ) -> anyhow:: Result < PathBuf > {
124
134
match self {
125
135
Self :: Path {
126
136
rust_gpu_repo_root, ..
127
137
} => Ok ( rust_gpu_repo_root. as_std_path ( ) . to_owned ( ) ) ,
128
138
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 ( ) ) ;
130
140
Ok ( cache_dir ( ) ?. join ( "codegen" ) . join ( dir) )
131
141
}
132
142
}
133
143
}
134
144
135
145
/// 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]
136
151
pub const fn is_path ( & self ) -> bool {
137
152
matches ! ( self , Self :: Path { .. } )
138
153
}
@@ -191,7 +206,21 @@ impl SpirvSource {
191
206
}
192
207
}
193
208
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
+
194
222
/// get the Package metadata from some crate
223
+ #[ inline]
195
224
pub fn query_metadata ( crate_path : & Path ) -> anyhow:: Result < Metadata > {
196
225
log:: debug!( "Running `cargo metadata` on `{}`" , crate_path. display( ) ) ;
197
226
let metadata = MetadataCommand :: new ( )
@@ -211,6 +240,7 @@ pub trait FindPackage {
211
240
}
212
241
213
242
impl FindPackage for Metadata {
243
+ #[ inline]
214
244
fn find_package ( & self , crate_name : & str ) -> anyhow:: Result < & Package > {
215
245
if let Some ( package) = self
216
246
. packages
@@ -229,6 +259,7 @@ impl FindPackage for Metadata {
229
259
}
230
260
231
261
/// Parse the `rust-toolchain.toml` in the working tree of the checked-out version of the `rust-gpu` repo.
262
+ #[ inline]
232
263
pub fn get_channel_from_rustc_codegen_spirv_build_script (
233
264
rustc_codegen_spirv_package : & Package ,
234
265
) -> anyhow:: Result < String > {
@@ -257,9 +288,14 @@ mod test {
257
288
use cargo_metadata:: { PackageBuilder , PackageId , Source } ;
258
289
use cargo_util_schemas:: manifest:: PackageName ;
259
290
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
+
260
296
#[ test_log:: test]
261
297
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 ( ) ;
263
299
let source = SpirvSource :: get_rust_gpu_deps_from_shader ( & shader_template_path) . unwrap ( ) ;
264
300
assert_eq ! (
265
301
source,
@@ -278,7 +314,7 @@ mod test {
278
314
279
315
#[ test_log:: test]
280
316
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 ( ) ;
282
318
let source = SpirvSource :: get_rust_gpu_deps_from_shader ( & shader_template_path) . unwrap ( ) ;
283
319
let dir = source. install_dir ( ) . unwrap ( ) ;
284
320
let name = dir
0 commit comments