diff --git a/CHANGELOG.md b/CHANGELOG.md index 251e699d36..831e2cac6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -122,6 +122,7 @@ - Update onig_sys dependency to 69.9.1 to fix a gcc build failure #3400 (@CosmicHorrorDev) - Add a cargo feature (`vendored-libgit2`) to build with vendored libgit2 version without depending on the system's one #3426 (@0x61nas) - Update syntect dependency to v5.3.0 to fix a few minor bugs, see #3410 (@keith-hall) +- Remove dependency on once_cell #3653 (@cyqsimon) ## Syntaxes diff --git a/Cargo.lock b/Cargo.lock index 2e777a2e6b..c2568d9278 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -135,7 +135,6 @@ dependencies = [ "minus", "nix", "nu-ansi-term", - "once_cell", "path_abs", "plist", "predicates", diff --git a/Cargo.toml b/Cargo.toml index 6c78333655..f908c28303 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,7 +47,6 @@ ansi_colours = "^1.2" bincode = "1.0" console = "0.16.2" flate2 = "1.1" -once_cell = "1.20" thiserror = "2.0" wild = { version = "2.2", optional = true } content_inspector = "0.2.4" @@ -110,7 +109,6 @@ nix = { version = "0.31", default-features = false, features = ["term"] } anyhow = "1.0.97" indexmap = { version = "2.13.0", features = ["serde"] } itertools = "0.14.0" -once_cell = "1.20" prettyplease = "0.2.37" proc-macro2 = "1.0.106" quote = "1.0.40" diff --git a/build/syntax_mapping.rs b/build/syntax_mapping.rs index b0db01eb80..db795a557f 100644 --- a/build/syntax_mapping.rs +++ b/build/syntax_mapping.rs @@ -3,12 +3,12 @@ use std::{ env, fs, path::{Path, PathBuf}, str::FromStr, + sync::LazyLock, }; use anyhow::{anyhow, bail}; use indexmap::IndexMap; use itertools::Itertools; -use once_cell::sync::Lazy; use proc_macro2::TokenStream; use quote::{quote, ToTokens, TokenStreamExt}; use regex::Regex; @@ -79,7 +79,7 @@ impl ToTokens for Case { /// A single matcher. /// -/// Codegen converts this into a `Lazy>`. +/// Codegen converts this into a `LazyLock>`. #[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize)] #[serde(try_from = "RawMatcher")] struct Matcher { @@ -105,7 +105,7 @@ struct Matcher { /// - 2024-02-20: allow `{` and `}` (glob brace expansion) fn parse_glob(s: &str) -> Result, anyhow::Error> { use MatcherSegment as Seg; - static VAR_REGEX: Lazy = Lazy::new(|| Regex::new(r"\$\{([\w\d_]+)\}").unwrap()); + static VAR_REGEX: LazyLock = LazyLock::new(|| Regex::new(r"\$\{([\w\d_]+)\}").unwrap()); let mut segments = vec![]; let mut text_start = 0; @@ -186,11 +186,11 @@ impl ToTokens for Matcher { let t = match self.segments.as_slice() { [] => unreachable!("0-length matcher should never be created"), [MatcherSegment::Text(text)] => { - quote! { Lazy::new(|| Some(build_matcher_fixed(#text, #case))) } + quote! { LazyLock::new(|| Some(build_matcher_fixed(#text, #case))) } } // parser logic ensures that this case can only happen when there are dynamic segments segs @ [_, ..] => { - quote! { Lazy::new(|| build_matcher_dynamic(&[ #(#segs),* ], #case)) } + quote! { LazyLock::new(|| build_matcher_dynamic(&[ #(#segs),* ], #case)) } } }; tokens.append_all(t); @@ -270,7 +270,7 @@ impl ToTokens for MappingList { let t = quote! { /// Generated by build script from /src/syntax_mapping/builtins/. - pub(crate) static BUILTIN_MAPPINGS: [(Lazy>, MappingTarget); #len] = [#(#array_items),*]; + pub(crate) static BUILTIN_MAPPINGS: [(LazyLock>, MappingTarget); #len] = [#(#array_items),*]; }; tokens.append_all(t); } diff --git a/src/assets.rs b/src/assets.rs index 29247bd7d8..9c272830c2 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -1,9 +1,8 @@ +use std::cell::OnceCell; use std::ffi::OsStr; use std::fs; use std::path::Path; -use once_cell::unsync::OnceCell; - use syntect::highlighting::Theme; use syntect::parsing::{SyntaxReference, SyntaxSet}; @@ -90,8 +89,14 @@ impl HighlightingAssets { /// Return the collection of syntect syntax definitions. pub fn get_syntax_set(&self) -> Result<&SyntaxSet> { - self.syntax_set_cell - .get_or_try_init(|| self.serialized_syntax_set.deserialize()) + // IMPRV: OnceCell::get_mut_or_init is preferable once stabalized + match self.syntax_set_cell.get() { + Some(set) => Ok(set), + None => { + let syntax_set = self.serialized_syntax_set.deserialize()?; + Ok(self.syntax_set_cell.get_or_init(|| syntax_set)) + } + } } /// Use [Self::get_syntaxes] instead diff --git a/src/assets/lazy_theme_set.rs b/src/assets/lazy_theme_set.rs index f3f3f69000..875964b414 100644 --- a/src/assets/lazy_theme_set.rs +++ b/src/assets/lazy_theme_set.rs @@ -5,8 +5,6 @@ use std::convert::TryFrom; use serde_derive::{Deserialize, Serialize}; -use once_cell::unsync::OnceCell; - use syntect::highlighting::{Theme, ThemeSet}; /// Same structure as a [`syntect::highlighting::ThemeSet`] but with themes @@ -31,10 +29,11 @@ impl LazyThemeSet { /// Lazily load the given theme pub fn get(&self, name: &str) -> Option<&Theme> { self.themes.get(name).and_then(|lazy_theme| { - lazy_theme - .deserialized - .get_or_try_init(|| lazy_theme.deserialize()) - .ok() + // IMPRV: OnceCell::get_mut_or_init is preferable once stabalized + lazy_theme.deserialized.get().or_else(|| { + let deserialized = lazy_theme.deserialize().ok()?; + Some(lazy_theme.deserialized.get_or_init(|| deserialized)) + }) }) } diff --git a/src/bin/bat/clap_app.rs b/src/bin/bat/clap_app.rs index 3636f08140..291db3aa17 100644 --- a/src/bin/bat/clap_app.rs +++ b/src/bin/bat/clap_app.rs @@ -1,11 +1,11 @@ use bat::style::StyleComponentList; use clap::{crate_name, crate_version, value_parser, Arg, ArgAction, ColorChoice, Command}; -use once_cell::sync::Lazy; use std::env; use std::path::{Path, PathBuf}; use std::str::FromStr; +use std::sync::LazyLock; -static VERSION: Lazy = Lazy::new(|| { +static VERSION: LazyLock = LazyLock::new(|| { #[cfg(feature = "bugreport")] let git_version = bugreport::git_version!(fallback = ""); #[cfg(not(feature = "bugreport"))] diff --git a/src/bin/bat/directories.rs b/src/bin/bat/directories.rs index f3fe4b98dc..abd2efac30 100644 --- a/src/bin/bat/directories.rs +++ b/src/bin/bat/directories.rs @@ -1,8 +1,8 @@ use std::env; use std::path::{Path, PathBuf}; +use std::sync::LazyLock; use etcetera::BaseStrategy; -use once_cell::sync::Lazy; /// Wrapper for 'etcetera' that checks BAT_CACHE_PATH and BAT_CONFIG_DIR and falls back to the /// Windows known folder locations on Windows & the XDG Base Directory Specification everywhere else. @@ -47,5 +47,5 @@ impl BatProjectDirs { } } -pub static PROJECT_DIRS: Lazy = - Lazy::new(|| BatProjectDirs::new().expect("Could not get home directory")); +pub static PROJECT_DIRS: LazyLock = + LazyLock::new(|| BatProjectDirs::new().expect("Could not get home directory")); diff --git a/src/syntax_mapping.rs b/src/syntax_mapping.rs index 584a5cb61a..dc2b096389 100644 --- a/src/syntax_mapping.rs +++ b/src/syntax_mapping.rs @@ -2,13 +2,12 @@ use std::{ path::Path, sync::{ atomic::{AtomicBool, Ordering}, - Arc, + Arc, LazyLock, }, thread, }; use globset::{Candidate, GlobBuilder, GlobMatcher}; -use once_cell::sync::Lazy; use crate::error::Result; use builtin::BUILTIN_MAPPINGS; @@ -93,7 +92,7 @@ impl<'a> SyntaxMapping<'a> { if halt.load(Ordering::Relaxed) { break; } - Lazy::force(matcher); + LazyLock::force(matcher); } }); // Note that this thread is not joined upon completion because there's diff --git a/src/syntax_mapping/builtin.rs b/src/syntax_mapping/builtin.rs index 79d298c3da..e0c41fa717 100644 --- a/src/syntax_mapping/builtin.rs +++ b/src/syntax_mapping/builtin.rs @@ -1,7 +1,6 @@ -use std::env; +use std::{env, sync::LazyLock}; use globset::GlobMatcher; -use once_cell::sync::Lazy; use crate::syntax_mapping::{make_glob_matcher, Case, MappingTarget}; @@ -22,7 +21,7 @@ include!(concat!( // ``` // enum BuiltinMatcher { // Fixed(&'static str), -// Dynamic(Lazy>), +// Dynamic(LazyLock>), // } // ``` // @@ -34,7 +33,7 @@ include!(concat!( // implementing the lazy matcher compilation logic, I realised that it's most // convenient for `BUILTIN_MAPPINGS` to have the following type: // -// `[(Lazy>, MappingTarget); N]` +// `[(LazyLock>, MappingTarget); N]` // // The benefit for this is that operations like listing all builtin mappings // would be effectively memoised. The caller would not have to compile another @@ -52,7 +51,7 @@ include!(concat!( /// /// A failure to compile is a fatal error. /// -/// Used internally by `Lazy>`'s lazy evaluation closure. +/// Used internally by `LazyLock>`'s lazy evaluation closure. fn build_matcher_fixed(from: &str, case: Case) -> GlobMatcher { make_glob_matcher(from, case).expect("A builtin fixed glob matcher failed to compile") } @@ -63,7 +62,7 @@ fn build_matcher_fixed(from: &str, case: Case) -> GlobMatcher { /// Returns `None` if any replacement fails, or if the joined glob string fails /// to compile. /// -/// Used internally by `Lazy>`'s lazy evaluation closure. +/// Used internally by `LazyLock>`'s lazy evaluation closure. fn build_matcher_dynamic(segs: &[MatcherSegment], case: Case) -> Option { // join segments let mut buf = String::new(); @@ -83,7 +82,7 @@ fn build_matcher_dynamic(segs: &[MatcherSegment], case: Case) -> Option>`'s lazy evaluation closure. +/// Used internally by `LazyLock>`'s lazy evaluation closure. #[derive(Clone, Debug)] enum MatcherSegment { Text(&'static str),