From 10312ddeffc91538f00c74aefdb0e39504ed59c5 Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Fri, 22 Mar 2024 17:04:09 -0600 Subject: [PATCH] remove dependency on once_cell --- Cargo.toml | 1 - src/finder.rs | 45 +++++++++++++++++++++++---------------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 90f1c9f..35a3bf7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,6 @@ rustix = { version = "0.38.30", default-features = false, features = ["fs", "std [target.'cfg(windows)'.dependencies] winsafe = { version = "0.0.19", features = ["kernel"] } -once_cell = "1" [dev-dependencies] tempfile = "3.9.0" diff --git a/src/finder.rs b/src/finder.rs index bea5620..cd73968 100644 --- a/src/finder.rs +++ b/src/finder.rs @@ -169,37 +169,38 @@ impl Finder { where P: IntoIterator, { - use once_cell::sync::Lazy; + use std::sync::OnceLock; // Sample %PATHEXT%: .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC // PATH_EXTENSIONS is then [".COM", ".EXE", ".BAT", …]. // (In one use of PATH_EXTENSIONS we skip the dot, but in the other we need it; // hence its retention.) - static PATH_EXTENSIONS: Lazy> = Lazy::new(|| { - env::var("PATHEXT") - .map(|pathext| { - pathext - .split(';') - .filter_map(|s| { - if s.as_bytes().first() == Some(&b'.') { - Some(s.to_owned()) - } else { - // Invalid segment; just ignore it. - None - } - }) - .collect() - }) - // PATHEXT not being set or not being a proper Unicode string is exceedingly - // improbable and would probably break Windows badly. Still, don't crash: - .unwrap_or_default() - }); + static PATH_EXTENSIONS: OnceLock> = OnceLock::new(); paths .into_iter() .flat_map(move |p| -> Box> { + let path_extensions = PATH_EXTENSIONS.get_or_init(|| { + env::var("PATHEXT") + .map(|pathext| { + pathext + .split(';') + .filter_map(|s| { + if s.as_bytes().first() == Some(&b'.') { + Some(s.to_owned()) + } else { + // Invalid segment; just ignore it. + None + } + }) + .collect() + }) + // PATHEXT not being set or not being a proper Unicode string is exceedingly + // improbable and would probably break Windows badly. Still, don't crash: + .unwrap_or_default() + }); // Check if path already have executable extension - if has_executable_extension(&p, &PATH_EXTENSIONS) { + if has_executable_extension(&p, path_extensions) { Box::new(iter::once(p)) } else { // Appended paths with windows executable extensions. @@ -210,7 +211,7 @@ impl Finder { // c:/windows/bin[.ext].CMD // ... Box::new( - iter::once(p.clone()).chain(PATH_EXTENSIONS.iter().map(move |e| { + iter::once(p.clone()).chain(path_extensions.iter().map(move |e| { // Append the extension. let mut p = p.clone().into_os_string(); p.push(e);