MCPcopy Create free account
hub / github.com/Rust-GPU/rust-gpu / find_lib

Function find_lib

tests/src/main.rs:265–305  ·  view source on GitHub ↗

Attempt find the rlib that matches `base`, if multiple rlibs are found then a clean build is required and `None` is returned.

(
    deps_target_dir: &Path,
    base: impl AsRef<Path>,
    dep_kind: DepKind,
    target: &str,
)

Source from the content-addressed store, hash-verified

263/// Attempt find the rlib that matches `base`, if multiple rlibs are found
264/// then a clean build is required and `None` is returned.
265fn find_lib(
266 deps_target_dir: &Path,
267 base: impl AsRef<Path>,
268 dep_kind: DepKind,
269 target: &str,
270) -> Result<Option<PathBuf>> {
271 let base = base.as_ref();
272 let (expected_prefix, expected_extension) = dep_kind.prefix_and_extension();
273 let expected_name = format!("{}{}", expected_prefix, base.display());
274
275 let dir = deps_target_dir.join(dep_kind.target_dir_suffix(target));
276
277 let paths = std::fs::read_dir(dir)?
278 .filter_map(Result::ok)
279 .map(|entry| entry.path())
280 .filter(|path| {
281 let name = {
282 let name = path.file_stem();
283 if name.is_none() {
284 return false;
285 }
286 name.unwrap()
287 };
288
289 let name_matches = name.to_str().unwrap().starts_with(&expected_name)
290 && name.len() == expected_name.len() + 17 // we expect our name, '-', and then 16 hexadecimal digits
291 && ends_with_dash_hash(name.to_str().unwrap());
292 let extension_matches = path
293 .extension()
294 .map_or(false, |ext| ext == expected_extension);
295
296 name_matches && extension_matches
297 })
298 .collect::<Vec<_>>();
299
300 Ok(if paths.len() > 1 {
301 None
302 } else {
303 paths.into_iter().next()
304 })
305}
306
307/// Returns whether this string ends with a dash ('-'), followed by 16 lowercase hexadecimal characters
308fn ends_with_dash_hash(s: &str) -> bool {

Callers 1

build_depsFunction · 0.85

Calls 7

ends_with_dash_hashFunction · 0.85
prefix_and_extensionMethod · 0.80
target_dir_suffixMethod · 0.80
extensionMethod · 0.80
into_iterMethod · 0.80
as_refMethod · 0.45
nextMethod · 0.45

Tested by

no test coverage detected