sync: once_cell::sync::OnceCell -> std::sync::OnceLock

Rust 1.70 stabilized `std::sync::OnceLock`, which replaces
`once_cell::sync::OnceCell`.

With this, we only have a single remaining direct dependency on
`once_cell`: `VAR_DISPATCH_TABLE` in `src/env_dispatch.rs`, where we use
`Lazy::get`. This can be replaced with `LazyLock::get` once our MSRV
reaches 1.94, where the function is stabilized.

At the moment, `serial_test` depends on `once_cell`, so even if we
eliminate it as a direct dependency, it will remain a transitive
dependency.

Closes #12289
This commit is contained in:
Daniel Rainer
2026-01-06 23:34:05 +01:00
committed by Johannes Altmanninger
parent 80e1942980
commit e16ea8df11
12 changed files with 29 additions and 33 deletions

1
Cargo.lock generated
View File

@@ -209,7 +209,6 @@ version = "0.0.0"
dependencies = [
"libc",
"nix",
"once_cell",
]
[[package]]

View File

@@ -9,7 +9,6 @@ license.workspace = true
[dependencies]
libc.workspace = true
nix.workspace = true
once_cell.workspace = true
[lints]
workspace = true

View File

@@ -1,7 +1,7 @@
use libc::STDIN_FILENO;
use once_cell::sync::OnceCell;
use std::env;
use std::os::unix::ffi::OsStrExt;
use std::sync::OnceLock;
// These are in the Unicode private-use range. We really shouldn't use this
// range but have little choice in the matter given how our lexer/parser works.
@@ -39,7 +39,7 @@ pub fn subslice_position<T: Eq>(a: &[T], b: &[T]) -> Option<usize> {
/// session. We err on the side of assuming it's not a console session. This approach isn't
/// bullet-proof and that's OK.
pub fn is_console_session() -> bool {
static IS_CONSOLE_SESSION: OnceCell<bool> = OnceCell::new();
static IS_CONSOLE_SESSION: OnceLock<bool> = OnceLock::new();
// TODO(terminal-workaround)
*IS_CONSOLE_SESSION.get_or_init(|| {
nix::unistd::ttyname(unsafe { std::os::fd::BorrowedFd::borrow_raw(STDIN_FILENO) })

View File

@@ -20,7 +20,6 @@
use fish_fallback::fish_wcwidth;
use fish_wchar::{decode_byte_from_char, encode_byte_to_char};
use libc::{SIG_IGN, SIGTTOU, STDIN_FILENO};
use once_cell::sync::OnceCell;
use std::cell::{Cell, RefCell};
use std::env;
use std::ffi::{CStr, CString, OsString};
@@ -29,7 +28,7 @@
use std::ops::{Deref, DerefMut};
use std::os::unix::prelude::*;
use std::sync::atomic::{AtomicI32, AtomicU32, Ordering};
use std::sync::{Arc, MutexGuard};
use std::sync::{Arc, MutexGuard, OnceLock};
use std::time;
pub const BUILD_DIR: &str = env!("FISH_RESOLVED_BUILD_DIR");
@@ -1041,7 +1040,7 @@ pub fn get_obfuscation_read_char() -> char {
pub static PROFILING_ACTIVE: RelaxedAtomicBool = RelaxedAtomicBool::new(false);
/// Name of the current program. Should be set at startup. Used by the debug function.
pub static PROGRAM_NAME: OnceCell<&'static wstr> = OnceCell::new();
pub static PROGRAM_NAME: OnceLock<&'static wstr> = OnceLock::new();
pub fn get_program_name() -> &'static wstr {
PROGRAM_NAME.get().unwrap()

View File

@@ -1,10 +1,10 @@
use crate::common::{BUILD_DIR, get_program_name};
use crate::{flog, flogf};
use fish_build_helper::workspace_root;
use once_cell::sync::OnceCell;
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
/// A struct of configuration directories, determined in main() that fish will optionally pass to
/// env_init.
@@ -163,7 +163,7 @@ pub enum FishPath {
LookUpInPath,
}
static FISH_PATH: OnceCell<FishPath> = OnceCell::new();
static FISH_PATH: OnceLock<FishPath> = OnceLock::new();
/// Get the absolute path to the fish executable itself
pub fn get_fish_path() -> &'static FishPath {

View File

@@ -28,13 +28,12 @@
use crate::wutil::{fish_wcstol, wgetcwd};
use libc::{c_int, uid_t};
use once_cell::sync::OnceCell;
use std::collections::HashMap;
use std::ffi::CStr;
use std::mem::MaybeUninit;
use std::os::unix::prelude::*;
use std::path::PathBuf;
use std::sync::{Arc, LazyLock};
use std::sync::{Arc, LazyLock, OnceLock};
/// Set when a universal variable has been modified but not yet been written to disk via sync().
static UVARS_LOCALLY_MODIFIED: RelaxedAtomicBool = RelaxedAtomicBool::new(false);
@@ -624,7 +623,7 @@ fn setup_path(global_exported_mode: EnvSetMode) {
/// The originally inherited variables and their values.
/// This is a simple key->value map and not e.g. cut into paths.
pub static INHERITED_VARS: OnceCell<HashMap<WString, WString>> = OnceCell::new();
pub static INHERITED_VARS: OnceLock<HashMap<WString, WString>> = OnceLock::new();
pub fn env_init(paths: Option<&ConfigPaths>, do_uvars: bool, default_paths: bool) {
let vars = EnvStack::globals();

View File

@@ -1,11 +1,13 @@
use std::{
panic::{UnwindSafe, set_hook, take_hook},
sync::atomic::{AtomicBool, Ordering},
sync::{
OnceLock,
atomic::{AtomicBool, Ordering},
},
time::Duration,
};
use libc::STDIN_FILENO;
use once_cell::sync::OnceCell;
use crate::{
common::{get_program_name, read_blocked},
@@ -13,7 +15,7 @@
threads::{asan_maybe_exit, is_main_thread},
};
pub static AT_EXIT: OnceCell<Box<dyn Fn() + Send + Sync>> = OnceCell::new();
pub static AT_EXIT: OnceLock<Box<dyn Fn() + Send + Sync>> = OnceLock::new();
pub fn panic_handler(main: impl FnOnce() -> i32 + UnwindSafe) -> ! {
// The isatty() check will stop us from hanging in most fish tests, but not those

View File

@@ -42,7 +42,7 @@
#[cfg(target_has_atomic = "64")]
use std::sync::atomic::AtomicU64;
use std::sync::atomic::{AtomicI32, AtomicU8, AtomicU32, Ordering};
use std::sync::{Arc, LazyLock, Mutex, MutexGuard};
use std::sync::{Arc, LazyLock, Mutex, MutexGuard, OnceLock};
use std::time::{Duration, Instant};
use errno::{Errno, errno};
@@ -179,8 +179,7 @@ enum ExitState {
/// The valid terminal modes on startup.
/// Warning: this is read from the SIGTERM handler! Hence the raw global.
static TERMINAL_MODE_ON_STARTUP: once_cell::sync::OnceCell<libc::termios> =
once_cell::sync::OnceCell::new();
static TERMINAL_MODE_ON_STARTUP: OnceLock<libc::termios> = OnceLock::new();
/// Mode we use to execute programs.
static TTY_MODES_FOR_EXTERNAL_CMDS: LazyLock<Mutex<libc::termios>> =

View File

@@ -9,7 +9,6 @@
use crate::text_face::{TextFace, TextStyling, UnderlineStyle};
use crate::threads::MainThread;
use bitflags::bitflags;
use once_cell::sync::OnceCell;
use std::cell::{RefCell, RefMut};
use std::env;
use std::ffi::{CStr, CString};
@@ -17,9 +16,8 @@
use std::os::fd::RawFd;
use std::os::unix::ffi::OsStrExt;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::{Arc, Mutex, OnceLock};
bitflags! {
#[derive(Copy, Clone, Default)]
@@ -397,7 +395,7 @@ fn osc_133_prompt_start(out: &mut impl Output) -> bool {
if !future_feature_flags::test(FeatureFlag::MarkPrompt) {
return false;
}
static TEST_BALLOON: OnceCell<()> = OnceCell::new();
static TEST_BALLOON: OnceLock<()> = OnceLock::new();
if TEST_BALLOON.set(()).is_ok() {
write_to_output!(out, "\x1b]133;A;click_events=1\x1b\\");
} else {

View File

@@ -9,16 +9,16 @@
use crate::topic_monitor::topic_monitor_init;
use crate::wutil::wgetcwd;
use crate::{env::EnvStack, proc::proc_init};
use once_cell::sync::OnceCell;
use std::cell::RefCell;
use std::collections::HashMap;
use std::env::set_current_dir;
use std::path::PathBuf;
use std::sync::OnceLock;
pub use serial_test::serial;
pub fn test_init() -> impl ScopeGuarding<Target = ()> {
static DONE: OnceCell<()> = OnceCell::new();
static DONE: OnceLock<()> = OnceLock::new();
DONE.get_or_init(|| {
// If we are building with `cargo build` and have build w/ `cmake`, this might not
// yet exist.

View File

@@ -20,19 +20,21 @@
use crate::wutil::{perror, wcstoi};
use fish_wchar::ToWString;
use libc::{EINVAL, ENOTTY, EPERM, STDIN_FILENO, WNOHANG};
use once_cell::sync::OnceCell;
use std::mem::MaybeUninit;
use std::sync::atomic::{AtomicBool, AtomicPtr, Ordering};
use std::sync::{
OnceLock,
atomic::{AtomicBool, AtomicPtr, Ordering},
};
/// Whether kitty keyboard protocol support is present in the TTY.
static KITTY_KEYBOARD_SUPPORTED: OnceCell<bool> = OnceCell::new();
static KITTY_KEYBOARD_SUPPORTED: OnceLock<bool> = OnceLock::new();
/// Set that the TTY supports the kitty keyboard protocol.
pub fn maybe_set_kitty_keyboard_capability() {
KITTY_KEYBOARD_SUPPORTED.get_or_init(|| true);
}
pub(crate) static SCROLL_CONTENT_UP_SUPPORTED: OnceCell<bool> = OnceCell::new();
pub(crate) static SCROLL_CONTENT_UP_SUPPORTED: OnceLock<bool> = OnceLock::new();
pub(crate) const SCROLL_CONTENT_UP_TERMINFO_CODE: &str = "indn";
// Get the support capability for kitty keyboard protocol.
@@ -47,10 +49,10 @@ pub fn maybe_set_scroll_content_up_capability() {
});
}
pub static TERMINAL_OS_NAME: OnceCell<Option<WString>> = OnceCell::new();
pub static TERMINAL_OS_NAME: OnceLock<Option<WString>> = OnceLock::new();
pub(crate) const XTGETTCAP_QUERY_OS_NAME: &str = "query-os-name";
pub static XTVERSION: OnceCell<WString> = OnceCell::new();
pub static XTVERSION: OnceLock<WString> = OnceLock::new();
pub fn xtversion() -> Option<&'static wstr> {
XTVERSION.get().as_ref().map(|s| s.as_utfstr())

View File

@@ -1,5 +1,4 @@
use once_cell::sync::OnceCell;
use std::os::fd::RawFd;
use std::{os::fd::RawFd, sync::OnceLock};
#[cfg(apple)]
mod notifyd;
@@ -69,7 +68,7 @@ pub fn create_notifier() -> Box<dyn UniversalNotifier> {
}
// Default instance. Other instances are possible for testing.
static DEFAULT_NOTIFIER: OnceCell<Box<dyn UniversalNotifier>> = OnceCell::new();
static DEFAULT_NOTIFIER: OnceLock<Box<dyn UniversalNotifier>> = OnceLock::new();
pub fn default_notifier() -> &'static dyn UniversalNotifier {
DEFAULT_NOTIFIER.get_or_init(create_notifier).as_ref()