Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove dependency on once_cell #90

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 6.0.1

- Remove dependency on `once_cell` for Windows users, replace with `std::sync::OnceLock`.

## 6.0.0

- MSRV is now 1.70
Expand Down
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "which"
version = "6.0.0"
version = "6.0.1"
edition = "2021"
rust-version = "1.70"
authors = ["Harry Fei <tiziyuanfang@gmail.com>"]
Expand All @@ -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
Loading