Skip to content

Commit

Permalink
Replace ancient lazy_static crate with once_cell
Browse files Browse the repository at this point in the history
Piggybacking on the [motivation in winit]: `lazy_static!` is a macro
whereas `once_cell` achieves the same using generics.  Its
implementation has also been [proposed for inclusion in `std`], making
it easier to switch to a standardized version if/when that happens.  The
author of that winit PR is making this change to many more crates,
slowly turning the scales in favour of `once_cell` in most dependency
trees.  Furthermore `lazy_static` hasn't published any updates for 3
years.

See also [the `once_cell` F.A.Q.].

[motivation in winit]: rust-windowing/winit#2313
[proposed for inclusion in `std`]: rust-lang/rust#74465
[the `once_cell` F.A.Q.]: https://docs.rs/once_cell/latest/once_cell/#faq
  • Loading branch information
MarijnS95 committed Jun 11, 2022
1 parent 60fd009 commit 9161cae
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ libc = "0.2.121"
regex = { version = "1.5.5", optional = true }

[target.'cfg(windows)'.dependencies]
lazy_static = "1.4.0"
once_cell = "1"

[dev-dependencies]
tempfile = "3.3.0"
Expand Down
40 changes: 21 additions & 19 deletions src/finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,29 +151,31 @@ impl Finder {
where
P: IntoIterator<Item = PathBuf>,
{
use once_cell::sync::Lazy;

// 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.)
lazy_static! {
static ref PATH_EXTENSIONS: Vec<String> =
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: 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()
});

paths
.into_iter()
Expand Down
4 changes: 0 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
//!
//! ```

#[cfg(windows)]
#[macro_use]
extern crate lazy_static;

mod checker;
mod error;
mod finder;
Expand Down

0 comments on commit 9161cae

Please sign in to comment.