Skip to content

Commit 4881ce7

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 44a1451 commit 4881ce7

File tree

7 files changed

+33
-65
lines changed

7 files changed

+33
-65
lines changed

Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ lto = true
4848
[dependencies]
4949
bitflags = "^2"
5050
itertools = "^0.13"
51-
# MSRV(1.70): Use OnceLock.
52-
# MSRV(1.80): Use LazyLock.
53-
lazy_static = "^1"
5451
libc = "^0.2"
5552
memchr = "^2"
5653
# MSRV(1.65): Update to >=0.4.1 which uses let_else. 0.4.0 was broken.

src/capi/error.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,15 @@ use std::{
2727
error::Error as StdError,
2828
ffi::CString,
2929
ptr,
30-
sync::Mutex,
30+
sync::{LazyLock, Mutex},
3131
};
3232

3333
use libc::{c_char, c_int};
3434
use rand::{self, Rng};
3535

36-
// TODO: Switch this to using a slab or similar structure, possibly using a less
37-
// heavy-weight lock? Maybe sharded-slab?
38-
// MSRV(1.70): Use OnceLock.
39-
// MSRV(1.80): Use LazyLock.
40-
lazy_static! {
41-
static ref ERROR_MAP: Mutex<HashMap<CReturn, Error>> = Mutex::new(HashMap::new());
42-
}
36+
// TODO: Switch this to using a slab or similar structure, possibly using a less heavy-weight lock?
37+
static ERROR_MAP: LazyLock<Mutex<HashMap<CReturn, Error>>> =
38+
LazyLock::new(|| Mutex::new(HashMap::new()));
4339

4440
pub(crate) fn store_error(err: Error) -> CReturn {
4541
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
@@ -153,8 +153,6 @@
153153

154154
#[macro_use]
155155
extern crate bitflags;
156-
#[macro_use]
157-
extern crate lazy_static;
158156
extern crate libc;
159157

160158
// `Handle` implementation.

src/procfs.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,12 @@ use std::{
3434
io::Error as IOError,
3535
os::unix::io::{AsFd, BorrowedFd, OwnedFd},
3636
path::{Path, PathBuf},
37+
sync::LazyLock,
3738
};
3839

39-
// MSRV(1.70): Use OnceLock.
40-
// MSRV(1.80): Use LazyLock.
41-
lazy_static! {
42-
/// A lazy-allocated `procfs` handle which is used globally by libpathrs.
43-
///
44-
/// As creating `procfs` handles can be somewhat expensive, library users
45-
/// are recommended to make use of this handle for `procfs` operations if
46-
/// possible.
47-
pub static ref GLOBAL_PROCFS_HANDLE: ProcfsHandle =
48-
ProcfsHandle::new().expect("should be able to get some /proc handle");
49-
}
40+
/// A `procfs` handle to which is used globally by libpathrs.
41+
pub(crate) static GLOBAL_PROCFS_HANDLE: LazyLock<ProcfsHandle> =
42+
LazyLock::new(|| ProcfsHandle::new().expect("should be able to get some /proc handle"));
5043

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

src/resolvers/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use std::{
3131
os::unix::io::{AsFd, OwnedFd},
3232
path::{Path, PathBuf},
3333
rc::Rc,
34+
sync::LazyLock,
3435
};
3536

3637
/// `O_PATH`-based userspace resolver.
@@ -63,15 +64,13 @@ pub(crate) enum ResolverBackend {
6364
// hyper-concerned users.
6465
}
6566

66-
// MSRV(1.70): Use OnceLock.
67-
// MSRV(1.80): Use LazyLock.
68-
lazy_static! {
69-
static ref DEFAULT_RESOLVER_TYPE: ResolverBackend = if *syscalls::OPENAT2_IS_SUPPORTED {
67+
static DEFAULT_RESOLVER_TYPE: LazyLock<ResolverBackend> = LazyLock::new(|| {
68+
if *syscalls::OPENAT2_IS_SUPPORTED {
7069
ResolverBackend::KernelOpenat2
7170
} else {
7271
ResolverBackend::EmulatedOpath
73-
};
74-
}
72+
}
73+
});
7574

7675
impl Default for ResolverBackend {
7776
fn default() -> Self {

src/resolvers/opath/impl.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use std::{
5858
},
5959
path::{Path, PathBuf},
6060
rc::Rc,
61+
sync::LazyLock,
6162
};
6263

6364
use itertools::Itertools;
@@ -130,17 +131,14 @@ fn check_current<RootFd: AsFd, Fd: AsFd, P: AsRef<Path>>(
130131
Ok(())
131132
}
132133

133-
// MSRV(1.70): Use OnceLock.
134-
// MSRV(1.80): Use LazyLock.
135-
lazy_static! {
136-
/// Cached copy of `fs.protected_symlinks` sysctl.
137-
// TODO: In theory this value could change during the lifetime of the
138-
// program, but there's no nice way of detecting that, and the overhead of
139-
// checking this for every symlink lookup is more likely to be an issue.
140-
static ref PROTECTED_SYMLINKS_SYSCTL: u32 =
141-
utils::sysctl_read_parse(&GLOBAL_PROCFS_HANDLE, "fs.protected_symlinks")
142-
.expect("should be able to parse fs.protected_symlinks");
143-
}
134+
/// Cached copy of `fs.protected_symlinks` sysctl.
135+
// TODO: In theory this value could change during the lifetime of the
136+
// program, but there's no nice way of detecting that, and the overhead of
137+
// checking this for every symlink lookup is more likely to be an issue.
138+
static PROTECTED_SYMLINKS_SYSCTL: LazyLock<u32> = LazyLock::new(|| {
139+
utils::sysctl_read_parse(&GLOBAL_PROCFS_HANDLE, "fs.protected_symlinks")
140+
.expect("should be able to parse fs.protected_symlinks")
141+
});
144142

145143
/// Verify that we should follow the symlink as per `fs.protected_symlinks`.
146144
///

src/syscalls.rs

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use std::{
3535
},
3636
path::{Path, PathBuf},
3737
ptr,
38+
sync::LazyLock,
3839
};
3940

4041
use libc::{c_int, c_uint, dev_t, mode_t, stat, statfs};
@@ -52,13 +53,6 @@ pub(crate) const AT_FDCWD: BorrowedFd<'static> = unsafe { BorrowedFd::borrow_raw
5253
#[cfg(test)]
5354
pub(crate) const BADFD: BorrowedFd<'static> = unsafe { BorrowedFd::borrow_raw(-libc::EBADF) };
5455

55-
// MSRV(1.70): Use OnceLock.
56-
// MSRV(1.80): Use LazyLock.
57-
lazy_static! {
58-
pub(crate) static ref OPENAT2_IS_SUPPORTED: bool =
59-
openat2(AT_FDCWD, ".", &Default::default()).is_ok();
60-
}
61-
6256
/// Representation of a file descriptor and its associated path at a given point
6357
/// in time.
6458
///
@@ -561,23 +555,13 @@ pub(crate) fn renameat<Fd1: AsFd, P1: AsRef<Path>, Fd2: AsFd, P2: AsRef<Path>>(
561555
}
562556
}
563557

564-
// MSRV(1.70): Use OnceLock.
565-
// MSRV(1.80): Use LazyLock.
566-
lazy_static! {
567-
pub(crate) static ref RENAME_FLAGS_SUPPORTED: bool = {
568-
match renameat2(
569-
AT_FDCWD,
570-
".",
571-
AT_FDCWD,
572-
".",
573-
libc::RENAME_EXCHANGE,
574-
) {
575-
Ok(_) => true,
576-
// We expect EBUSY, but just to be safe we only check for ENOSYS.
577-
Err(err) => err.root_cause().raw_os_error() != Some(libc::ENOSYS),
578-
}
579-
};
580-
}
558+
pub(crate) static RENAME_FLAGS_SUPPORTED: LazyLock<bool> = LazyLock::new(|| {
559+
match renameat2(AT_FDCWD, ".", AT_FDCWD, ".", libc::RENAME_EXCHANGE) {
560+
Ok(_) => true,
561+
// We expect EBUSY, but just to be safe we only check for ENOSYS.
562+
Err(err) => err.root_cause().raw_os_error() != Some(libc::ENOSYS),
563+
}
564+
});
581565

582566
/// Wrapper for `renameat2(2)`.
583567
///
@@ -717,6 +701,9 @@ pub(crate) fn statx<Fd: AsFd, P: AsRef<Path>>(
717701
}
718702
}
719703

704+
pub(crate) static OPENAT2_IS_SUPPORTED: LazyLock<bool> =
705+
LazyLock::new(|| openat2(AT_FDCWD, ".", &Default::default()).is_ok());
706+
720707
/// Arguments for how `openat2` should open the target path.
721708
// TODO: Maybe switch to libc::open_how?
722709
#[repr(C)]

0 commit comments

Comments
 (0)