Skip to content

Commit

Permalink
*: migrate from lazy_static! to std::sync::LazyLock
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
cyphar committed Oct 14, 2024
1 parent 5e31275 commit b4cf90b
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 23 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ bitflags = "^2"
itertools = "^0.13"
libc = "^0.2"
memchr = "^2"
# MSRV(1.80): Use LazyLock.
once_cell = "^1"
# MSRV(1.65): Update to >=0.4.1 which uses let_else. 0.4.0 was broken.
open-enum = { version = "=0.3.0", optional = true }
rand = { version = "^0.8", optional = true }
Expand Down
7 changes: 3 additions & 4 deletions src/capi/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@ use std::{
error::Error as StdError,
ffi::CString,
ptr,
sync::Mutex,
sync::{LazyLock, Mutex},
};

use libc::{c_char, c_int};
use once_cell::sync::Lazy;
use rand::{self, Rng};

// TODO: Switch this to using a slab or similar structure, possibly using a less heavy-weight lock?
// MSRV(1.80): Use LazyLock.
static ERROR_MAP: Lazy<Mutex<HashMap<CReturn, Error>>> = Lazy::new(|| Mutex::new(HashMap::new()));
static ERROR_MAP: LazyLock<Mutex<HashMap<CReturn, Error>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));

pub(crate) fn store_error(err: Error) -> CReturn {
let mut err_map = ERROR_MAP.lock().unwrap();
Expand Down
7 changes: 3 additions & 4 deletions src/procfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ use std::{
io::Error as IOError,
os::unix::io::{AsFd, BorrowedFd, OwnedFd},
path::{Path, PathBuf},
sync::LazyLock,
};

use once_cell::sync::Lazy;
use rustix::fs::{self as rustix_fs, Access, AtFlags};

/// A `procfs` handle to which is used globally by libpathrs.
// MSRV(1.80): Use LazyLock.
pub(crate) static GLOBAL_PROCFS_HANDLE: Lazy<ProcfsHandle> =
Lazy::new(|| ProcfsHandle::new().expect("should be able to get some /proc handle"));
pub(crate) static GLOBAL_PROCFS_HANDLE: LazyLock<ProcfsHandle> =
LazyLock::new(|| ProcfsHandle::new().expect("should be able to get some /proc handle"));

/// Indicate what base directory should be used when doing `/proc/...`
/// operations with a [`ProcfsHandle`].
Expand Down
6 changes: 2 additions & 4 deletions src/resolvers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ use std::{
os::unix::io::{AsFd, OwnedFd},
path::{Path, PathBuf},
rc::Rc,
sync::LazyLock,
};

use once_cell::sync::Lazy;

/// `O_PATH`-based userspace resolver.
pub(crate) mod opath;
/// `openat2(2)`-based in-kernel resolver.
Expand Down Expand Up @@ -65,8 +64,7 @@ pub(crate) enum ResolverBackend {
// hyper-concerned users.
}

// MSRV(1.80): Use LazyLock.
static DEFAULT_RESOLVER_TYPE: Lazy<ResolverBackend> = Lazy::new(|| {
static DEFAULT_RESOLVER_TYPE: LazyLock<ResolverBackend> = LazyLock::new(|| {
if *syscalls::OPENAT2_IS_SUPPORTED {
ResolverBackend::KernelOpenat2
} else {
Expand Down
5 changes: 2 additions & 3 deletions src/resolvers/opath/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ use std::{
},
path::{Path, PathBuf},
rc::Rc,
sync::LazyLock,
};

use itertools::Itertools;
use once_cell::sync::Lazy;

/// Ensure that the expected path within the root matches the current fd.
fn check_current<RootFd: AsFd, Fd: AsFd, P: AsRef<Path>>(
Expand Down Expand Up @@ -135,8 +135,7 @@ fn check_current<RootFd: AsFd, Fd: AsFd, P: AsRef<Path>>(
// TODO: In theory this value could change during the lifetime of the
// program, but there's no nice way of detecting that, and the overhead of
// checking this for every symlink lookup is more likely to be an issue.
// MSRV(1.80): Use LazyLock.
static PROTECTED_SYMLINKS_SYSCTL: Lazy<u32> = Lazy::new(|| {
static PROTECTED_SYMLINKS_SYSCTL: LazyLock<u32> = LazyLock::new(|| {
utils::sysctl_read_parse(&GLOBAL_PROCFS_HANDLE, "fs.protected_symlinks")
.expect("should be able to parse fs.protected_symlinks")
});
Expand Down
10 changes: 4 additions & 6 deletions src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ use std::{
},
path::{Path, PathBuf},
ptr,
sync::LazyLock,
};

use libc::{c_int, c_uint, dev_t, mode_t, stat, statfs};
use once_cell::sync::Lazy;

// TODO: Figure out how we can put a backtrace here (it seems we can't use
// thiserror's backtrace support without nightly Rust because thiserror
Expand Down Expand Up @@ -555,8 +555,7 @@ pub(crate) fn renameat<Fd1: AsFd, P1: AsRef<Path>, Fd2: AsFd, P2: AsRef<Path>>(
}
}

// MSRV(1.80): Use LazyLock.
pub(crate) static RENAME_FLAGS_SUPPORTED: Lazy<bool> = Lazy::new(|| {
pub(crate) static RENAME_FLAGS_SUPPORTED: LazyLock<bool> = LazyLock::new(|| {
match renameat2(AT_FDCWD, ".", AT_FDCWD, ".", libc::RENAME_EXCHANGE) {
Ok(_) => true,
// We expect EBUSY, but just to be safe we only check for ENOSYS.
Expand Down Expand Up @@ -702,9 +701,8 @@ pub(crate) fn statx<Fd: AsFd, P: AsRef<Path>>(
}
}

// MSRV(1.80): Use LazyLock.
pub(crate) static OPENAT2_IS_SUPPORTED: Lazy<bool> =
Lazy::new(|| openat2(AT_FDCWD, ".", &Default::default()).is_ok());
pub(crate) static OPENAT2_IS_SUPPORTED: LazyLock<bool> =
LazyLock::new(|| openat2(AT_FDCWD, ".", &Default::default()).is_ok());

bitflags! {
/// Wrapper for the underlying `libc`'s `RESOLVE_*` flags.
Expand Down

0 comments on commit b4cf90b

Please sign in to comment.