From 0abebb25688710cc144c58349c4e951e1ba8a08e Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Sun, 4 Aug 2024 15:54:59 +1000 Subject: [PATCH] *: migrate from once_cell 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 --- Cargo.toml | 2 -- src/capi/error.rs | 7 +++---- src/procfs.rs | 7 +++---- src/resolvers/mod.rs | 6 ++---- src/resolvers/opath/impl.rs | 5 ++--- src/syscalls.rs | 10 ++++------ 6 files changed, 14 insertions(+), 23 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 366db49..b2f9e4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/capi/error.rs b/src/capi/error.rs index 6c3de4a..8bfb79d 100644 --- a/src/capi/error.rs +++ b/src/capi/error.rs @@ -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>> = Lazy::new(|| Mutex::new(HashMap::new())); +static ERROR_MAP: LazyLock>> = + LazyLock::new(|| Mutex::new(HashMap::new())); pub(crate) fn store_error(err: Error) -> CReturn { let mut err_map = ERROR_MAP.lock().unwrap(); diff --git a/src/procfs.rs b/src/procfs.rs index f34c25d..369b7fe 100644 --- a/src/procfs.rs +++ b/src/procfs.rs @@ -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 = - Lazy::new(|| ProcfsHandle::new().expect("should be able to get some /proc handle")); +pub(crate) static GLOBAL_PROCFS_HANDLE: LazyLock = + 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`]. diff --git a/src/resolvers/mod.rs b/src/resolvers/mod.rs index 80b9af1..21b7ef7 100644 --- a/src/resolvers/mod.rs +++ b/src/resolvers/mod.rs @@ -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. @@ -65,8 +64,7 @@ pub(crate) enum ResolverBackend { // hyper-concerned users. } -// MSRV(1.80): Use LazyLock. -static DEFAULT_RESOLVER_TYPE: Lazy = Lazy::new(|| { +static DEFAULT_RESOLVER_TYPE: LazyLock = LazyLock::new(|| { if *syscalls::OPENAT2_IS_SUPPORTED { ResolverBackend::KernelOpenat2 } else { diff --git a/src/resolvers/opath/impl.rs b/src/resolvers/opath/impl.rs index f1c91bf..2a92c44 100644 --- a/src/resolvers/opath/impl.rs +++ b/src/resolvers/opath/impl.rs @@ -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>( @@ -135,8 +135,7 @@ fn check_current>( // 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 = Lazy::new(|| { +static PROTECTED_SYMLINKS_SYSCTL: LazyLock = LazyLock::new(|| { utils::sysctl_read_parse(&GLOBAL_PROCFS_HANDLE, "fs.protected_symlinks") .expect("should be able to parse fs.protected_symlinks") }); diff --git a/src/syscalls.rs b/src/syscalls.rs index 14c6999..c5b3826 100644 --- a/src/syscalls.rs +++ b/src/syscalls.rs @@ -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 @@ -555,8 +555,7 @@ pub(crate) fn renameat, Fd2: AsFd, P2: AsRef>( } } -// MSRV(1.80): Use LazyLock. -pub(crate) static RENAME_FLAGS_SUPPORTED: Lazy = Lazy::new(|| { +pub(crate) static RENAME_FLAGS_SUPPORTED: LazyLock = 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. @@ -702,9 +701,8 @@ pub(crate) fn statx>( } } -// MSRV(1.80): Use LazyLock. -pub(crate) static OPENAT2_IS_SUPPORTED: Lazy = - Lazy::new(|| openat2(AT_FDCWD, ".", &Default::default()).is_ok()); +pub(crate) static OPENAT2_IS_SUPPORTED: LazyLock = + LazyLock::new(|| openat2(AT_FDCWD, ".", &Default::default()).is_ok()); bitflags! { /// Wrapper for the underlying `libc`'s `RESOLVE_*` flags.