Skip to content

Commit

Permalink
remove dependency on once_cell
Browse files Browse the repository at this point in the history
  • Loading branch information
Xaeroxe committed Mar 22, 2024
1 parent 071683c commit e95dca2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
45 changes: 23 additions & 22 deletions src/finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,37 +169,38 @@ impl Finder {
where
P: IntoIterator<Item = PathBuf>,
{
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<Vec<String>> = 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<Vec<String>> = OnceLock::new();

paths
.into_iter()
.flat_map(move |p| -> Box<dyn Iterator<Item = _>> {
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.
Expand All @@ -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);
Expand Down

0 comments on commit e95dca2

Please sign in to comment.