Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
12 changes: 6 additions & 6 deletions build/syntax_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,7 +79,7 @@ impl ToTokens for Case {

/// A single matcher.
///
/// Codegen converts this into a `Lazy<Option<GlobMatcher>>`.
/// Codegen converts this into a `LazyLock<Option<GlobMatcher>>`.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize)]
#[serde(try_from = "RawMatcher")]
struct Matcher {
Expand All @@ -105,7 +105,7 @@ struct Matcher {
/// - 2024-02-20: allow `{` and `}` (glob brace expansion)
fn parse_glob(s: &str) -> Result<Vec<MatcherSegment>, anyhow::Error> {
use MatcherSegment as Seg;
static VAR_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\$\{([\w\d_]+)\}").unwrap());
static VAR_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\$\{([\w\d_]+)\}").unwrap());

let mut segments = vec![];
let mut text_start = 0;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<Option<GlobMatcher>>, MappingTarget); #len] = [#(#array_items),*];
pub(crate) static BUILTIN_MAPPINGS: [(LazyLock<Option<GlobMatcher>>, MappingTarget); #len] = [#(#array_items),*];
};
tokens.append_all(t);
}
Expand Down
13 changes: 9 additions & 4 deletions src/assets.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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))
}
}
Comment on lines -93 to +99
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works but obviously is less than ideal.

I'll leave this decision up to maintains, whether the benefit of removing an external dependency outweighs the drawback of having this workaround.

}

/// Use [Self::get_syntaxes] instead
Expand Down
11 changes: 5 additions & 6 deletions src/assets/lazy_theme_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
})
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/bin/bat/clap_app.rs
Original file line number Diff line number Diff line change
@@ -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<String> = Lazy::new(|| {
static VERSION: LazyLock<String> = LazyLock::new(|| {
#[cfg(feature = "bugreport")]
let git_version = bugreport::git_version!(fallback = "");
#[cfg(not(feature = "bugreport"))]
Expand Down
6 changes: 3 additions & 3 deletions src/bin/bat/directories.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -47,5 +47,5 @@ impl BatProjectDirs {
}
}

pub static PROJECT_DIRS: Lazy<BatProjectDirs> =
Lazy::new(|| BatProjectDirs::new().expect("Could not get home directory"));
pub static PROJECT_DIRS: LazyLock<BatProjectDirs> =
LazyLock::new(|| BatProjectDirs::new().expect("Could not get home directory"));
5 changes: 2 additions & 3 deletions src/syntax_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions src/syntax_mapping/builtin.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -22,7 +21,7 @@ include!(concat!(
// ```
// enum BuiltinMatcher {
// Fixed(&'static str),
// Dynamic(Lazy<Option<String>>),
// Dynamic(LazyLock<Option<String>>),
// }
// ```
//
Expand All @@ -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<Option<GlobMatcher>>, MappingTarget); N]`
// `[(LazyLock<Option<GlobMatcher>>, 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
Expand All @@ -52,7 +51,7 @@ include!(concat!(
///
/// A failure to compile is a fatal error.
///
/// Used internally by `Lazy<Option<GlobMatcher>>`'s lazy evaluation closure.
/// Used internally by `LazyLock<Option<GlobMatcher>>`'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")
}
Expand All @@ -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<Option<GlobMatcher>>`'s lazy evaluation closure.
/// Used internally by `LazyLock<Option<GlobMatcher>>`'s lazy evaluation closure.
fn build_matcher_dynamic(segs: &[MatcherSegment], case: Case) -> Option<GlobMatcher> {
// join segments
let mut buf = String::new();
Expand All @@ -83,7 +82,7 @@ fn build_matcher_dynamic(segs: &[MatcherSegment], case: Case) -> Option<GlobMatc

/// A segment of a dynamic builtin matcher.
///
/// Used internally by `Lazy<Option<GlobMatcher>>`'s lazy evaluation closure.
/// Used internally by `LazyLock<Option<GlobMatcher>>`'s lazy evaluation closure.
#[derive(Clone, Debug)]
enum MatcherSegment {
Text(&'static str),
Expand Down