Skip to content

Commit 7c70700

Browse files
committed
*: migrate from lazy_static! to std::sync::LazyLock
This was only stabilised in Rust 1.80.0, so this might cause issues when building on older distributions (depending on when libpathrs starts getting packaged). Signed-off-by: Aleksa Sarai <[email protected]>
1 parent 8e64539 commit 7c70700

File tree

6 files changed

+29
-37
lines changed

6 files changed

+29
-37
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ panic = "abort"
4848

4949
[dependencies]
5050
bitflags = "^2"
51-
lazy_static = "^1"
5251
libc = "^0.2"
5352
memchr = "^2"
5453
rand = "^0.8"

src/capi/ret.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use std::{
2727
fs::File,
2828
mem::ManuallyDrop,
2929
os::unix::io::{FromRawFd, IntoRawFd, RawFd},
30-
sync::Mutex,
30+
sync::{LazyLock, Mutex},
3131
};
3232

3333
use libc::c_int;
@@ -40,9 +40,8 @@ pub(super) trait IntoCReturn {
4040
}
4141

4242
// TODO: Switch this to using a slab or similar structure, possibly using a less heavy-weight lock?
43-
lazy_static! {
44-
static ref ERROR_MAP: Mutex<HashMap<CReturn, Error>> = Mutex::new(HashMap::new());
45-
}
43+
static ERROR_MAP: LazyLock<Mutex<HashMap<CReturn, Error>>> =
44+
LazyLock::new(|| Mutex::new(HashMap::new()));
4645

4746
fn store_error(err: Error) -> CReturn {
4847
let mut err_map = ERROR_MAP.lock().unwrap();

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@
129129

130130
#[macro_use]
131131
extern crate bitflags;
132-
#[macro_use]
133-
extern crate lazy_static;
134132
extern crate libc;
135133
#[macro_use]
136134
extern crate snafu;

src/procfs.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,14 @@ use std::{
3131
os::fd::AsRawFd,
3232
os::unix::fs::MetadataExt,
3333
path::{Path, PathBuf},
34+
sync::LazyLock,
3435
};
3536

3637
use snafu::{OptionExt, ResultExt};
3738

38-
lazy_static! {
39-
/// A `procfs` handle to which is used globally by libpathrs.
40-
pub(crate) static ref PROCFS_HANDLE: ProcfsHandle =
41-
ProcfsHandle::new().expect("should be able to get some /proc handle");
42-
}
39+
/// A `procfs` handle to which is used globally by libpathrs.
40+
pub(crate) static PROCFS_HANDLE: LazyLock<ProcfsHandle> =
41+
LazyLock::new(|| ProcfsHandle::new().expect("should be able to get some /proc handle"));
4342

4443
/// Indicate what base directory should be used when doing `/proc/...`
4544
/// operations with a [`ProcfsHandle`].

src/resolvers/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
use crate::{error::Error, flags::ResolverFlags, syscalls, Handle};
2222

23-
use std::{fs::File, path::Path};
23+
use std::{fs::File, path::Path, sync::LazyLock};
2424

2525
/// `O_PATH`-based userspace resolver.
2626
pub mod opath;
@@ -51,13 +51,13 @@ pub enum ResolverBackend {
5151
// hyper-concerned users.
5252
}
5353

54-
lazy_static! {
55-
static ref DEFAULT_RESOLVER_TYPE: ResolverBackend = if *syscalls::OPENAT2_IS_SUPPORTED {
54+
static DEFAULT_RESOLVER_TYPE: LazyLock<ResolverBackend> = LazyLock::new(|| {
55+
if *syscalls::OPENAT2_IS_SUPPORTED {
5656
ResolverBackend::KernelOpenat2
5757
} else {
5858
ResolverBackend::EmulatedOpath
59-
};
60-
}
59+
}
60+
});
6161

6262
impl Default for ResolverBackend {
6363
fn default() -> Self {

src/syscalls.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,12 @@ use std::{
3636
},
3737
path::{Path, PathBuf},
3838
ptr,
39+
sync::LazyLock,
3940
};
4041

4142
use libc::{c_int, c_uint, dev_t, mode_t, stat, statfs};
4243
use snafu::ResultExt;
4344

44-
lazy_static! {
45-
pub(crate) static ref OPENAT2_IS_SUPPORTED: bool =
46-
openat2(libc::AT_FDCWD, ".", &Default::default()).is_ok();
47-
}
48-
4945
/// Representation of a file descriptor and its associated path at a given point
5046
/// in time.
5147
///
@@ -639,21 +635,19 @@ pub(crate) fn renameat<P1: AsRef<Path>, P2: AsRef<Path>>(
639635
}
640636
}
641637

642-
lazy_static! {
643-
pub(crate) static ref RENAME_FLAGS_SUPPORTED: bool = {
644-
match renameat2(
645-
libc::AT_FDCWD,
646-
".",
647-
libc::AT_FDCWD,
648-
".",
649-
libc::RENAME_EXCHANGE,
650-
) {
651-
Ok(_) => true,
652-
// We expect EBUSY, but just to be safe we only check for ENOSYS.
653-
Err(err) => err.root_cause().raw_os_error() != Some(libc::ENOSYS),
654-
}
655-
};
656-
}
638+
pub(crate) static RENAME_FLAGS_SUPPORTED: LazyLock<bool> = LazyLock::new(|| {
639+
match renameat2(
640+
libc::AT_FDCWD,
641+
".",
642+
libc::AT_FDCWD,
643+
".",
644+
libc::RENAME_EXCHANGE,
645+
) {
646+
Ok(_) => true,
647+
// We expect EBUSY, but just to be safe we only check for ENOSYS.
648+
Err(err) => err.root_cause().raw_os_error() != Some(libc::ENOSYS),
649+
}
650+
});
657651

658652
/// Wrapper for `renameat2(2)`.
659653
///
@@ -781,6 +775,9 @@ pub(crate) fn statx<P: AsRef<Path>>(
781775
}
782776
}
783777

778+
pub(crate) static OPENAT2_IS_SUPPORTED: LazyLock<bool> =
779+
LazyLock::new(|| openat2(libc::AT_FDCWD, ".", &Default::default()).is_ok());
780+
784781
/// Arguments for how `openat2` should open the target path.
785782
// TODO: Maybe switch to libc::open_how?
786783
#[repr(C)]

0 commit comments

Comments
 (0)