Skip to content

Commit db6e79c

Browse files
refactor: Drop once_cell in favor of OnceLock/LazyLock
1 parent 7fe13c0 commit db6e79c

File tree

10 files changed

+36
-32
lines changed

10 files changed

+36
-32
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Dependencies
6+
7+
- Replace `once_cell` with `std::sync::{LazyLock, OnceLock}`
8+
39
## [Version 5.2.0](https://github.com/trishume/syntect/compare/v5.1.0...v5.2.0) (2024-02-07)
410

511
### Improvements

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ fnv = { version = "1.0", optional = true }
3131
serde = "1.0"
3232
serde_derive = "1.0"
3333
serde_json = "1.0"
34-
once_cell = "1.8"
3534
thiserror = "1.0"
3635

3736
[dev-dependencies]

examples/syntest.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ use std::fs::File;
1717
use std::io::{BufRead, BufReader};
1818
use std::path::Path;
1919
use std::str::FromStr;
20+
use std::sync::LazyLock;
2021
use std::time::Instant;
2122

2223
use getopts::Options;
23-
use once_cell::sync::Lazy;
2424
use regex::Regex;
2525
use walkdir::{DirEntry, WalkDir};
2626

@@ -36,7 +36,7 @@ pub enum SyntaxTestFileResult {
3636
Success(usize),
3737
}
3838

39-
pub static SYNTAX_TEST_HEADER_PATTERN: Lazy<Regex> = Lazy::new(|| {
39+
pub static SYNTAX_TEST_HEADER_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
4040
Regex::new(
4141
r#"(?xm)
4242
^(?P<testtoken_start>\s*\S+)
@@ -47,7 +47,7 @@ pub static SYNTAX_TEST_HEADER_PATTERN: Lazy<Regex> = Lazy::new(|| {
4747
)
4848
.unwrap()
4949
});
50-
pub static SYNTAX_TEST_ASSERTION_PATTERN: Lazy<Regex> = Lazy::new(|| {
50+
pub static SYNTAX_TEST_ASSERTION_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
5151
Regex::new(
5252
r#"(?xm)
5353
\s*(?:

src/html.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use crate::easy::{HighlightFile, HighlightLines};
33
use crate::escape::Escape;
44
use crate::highlighting::{Color, FontStyle, Style, Theme};
55
use crate::parsing::{
6-
BasicScopeStackOp, ParseState, Scope, ScopeStack, ScopeStackOp, SyntaxReference, SyntaxSet,
7-
SCOPE_REPO,
6+
scope_repo, BasicScopeStackOp, ParseState, Scope, ScopeStack, ScopeStackOp, SyntaxReference,
7+
SyntaxSet,
88
};
99
use crate::util::LinesWithEndings;
1010
use crate::Error;
@@ -241,7 +241,7 @@ pub enum ClassStyle {
241241
}
242242

243243
fn scope_to_classes(s: &mut String, scope: Scope, style: ClassStyle) {
244-
let repo = SCOPE_REPO.lock().unwrap();
244+
let repo = scope_repo().lock().unwrap();
245245
for i in 0..(scope.len()) {
246246
let atom = scope.atom_at(i as usize);
247247
let atom_s = repo.atom_str(atom);
@@ -259,7 +259,7 @@ fn scope_to_classes(s: &mut String, scope: Scope, style: ClassStyle) {
259259
}
260260

261261
fn scope_to_selector(s: &mut String, scope: Scope, style: ClassStyle) {
262-
let repo = SCOPE_REPO.lock().unwrap();
262+
let repo = scope_repo().lock().unwrap();
263263
for i in 0..(scope.len()) {
264264
let atom = scope.atom_at(i as usize);
265265
let atom_s = repo.atom_str(atom);

src/parsing/regex.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use once_cell::sync::OnceCell;
21
use serde::de::{Deserialize, Deserializer};
32
use serde::ser::{Serialize, Serializer};
43
use std::error::Error;
4+
use std::sync::OnceLock;
55

66
/// An abstraction for regex patterns.
77
///
@@ -11,7 +11,7 @@ use std::error::Error;
1111
#[derive(Debug)]
1212
pub struct Regex {
1313
regex_str: String,
14-
regex: OnceCell<regex_impl::Regex>,
14+
regex: OnceLock<regex_impl::Regex>,
1515
}
1616

1717
/// A region contains text positions for capture groups in a match result.
@@ -28,7 +28,7 @@ impl Regex {
2828
pub fn new(regex_str: String) -> Self {
2929
Self {
3030
regex_str,
31-
regex: OnceCell::new(),
31+
regex: OnceLock::new(),
3232
}
3333
}
3434

@@ -76,7 +76,7 @@ impl Clone for Regex {
7676
fn clone(&self) -> Self {
7777
Regex {
7878
regex_str: self.regex_str.clone(),
79-
regex: OnceCell::new(),
79+
regex: OnceLock::new(),
8080
}
8181
}
8282
}

src/parsing/scope.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ use std::collections::HashMap;
44
use std::fmt;
55
use std::mem;
66
use std::str::FromStr;
7-
use std::sync::Mutex;
7+
use std::sync::{Mutex, OnceLock};
88
use std::u16;
99
use std::u64;
1010

11-
use once_cell::sync::Lazy;
1211
use serde::de::{Deserialize, Deserializer, Error, Visitor};
1312
use serde::ser::{Serialize, Serializer};
1413
use serde_derive::{Deserialize, Serialize};
@@ -29,11 +28,13 @@ pub const ATOM_LEN_BITS: u16 = 3;
2928

3029
/// The global scope repo, exposed in case you want to minimize locking and unlocking.
3130
///
32-
/// Ths shouldn't be necessary for you to use. See the [`ScopeRepository`] docs.
31+
/// This shouldn't be necessary for you to use. See the [`ScopeRepository`] docs.
3332
///
3433
/// [`ScopeRepository`]: struct.ScopeRepository.html
35-
pub static SCOPE_REPO: Lazy<Mutex<ScopeRepository>> =
36-
Lazy::new(|| Mutex::new(ScopeRepository::new()));
34+
pub fn scope_repo() -> &'static Mutex<ScopeRepository> {
35+
static SCOPE_REPO: OnceLock<Mutex<ScopeRepository>> = OnceLock::new();
36+
SCOPE_REPO.get_or_init(|| Mutex::new(ScopeRepository::new()))
37+
}
3738

3839
/// A hierarchy of atoms with semi-standardized names used to accord semantic information to a
3940
/// specific piece of text.
@@ -73,12 +74,11 @@ pub enum ParseScopeError {
7374
/// The structure used to keep track of the mapping between scope atom numbers and their string
7475
/// names
7576
///
76-
/// It is only exposed in case you want to lock [`SCOPE_REPO`] and then allocate a bunch of scopes
77+
/// It is only exposed in case you want to lock [`scope_repo()`] and then allocate a bunch of scopes
7778
/// at once without thrashing the lock. In general, you should just use [`Scope::new()`].
7879
///
7980
/// Only [`Scope`]s created by the same repository have valid comparison results.
8081
///
81-
/// [`SCOPE_REPO`]: struct.SCOPE_REPO.html
8282
/// [`Scope::new()`]: struct.Scope.html#method.new
8383
/// [`Scope`]: struct.Scope.html
8484
#[derive(Debug)]
@@ -223,7 +223,7 @@ impl Scope {
223223
///
224224
/// Example: `Scope::new("meta.rails.controller")`
225225
pub fn new(s: &str) -> Result<Scope, ParseScopeError> {
226-
let mut repo = SCOPE_REPO.lock().unwrap();
226+
let mut repo = scope_repo().lock().unwrap();
227227
repo.build(s.trim())
228228
}
229229

@@ -268,7 +268,7 @@ impl Scope {
268268
///
269269
/// This requires locking a global repo and shouldn't be done frequently.
270270
pub fn build_string(self) -> String {
271-
let repo = SCOPE_REPO.lock().unwrap();
271+
let repo = scope_repo().lock().unwrap();
272272
repo.to_string(self)
273273
}
274274

src/parsing/syntax_set.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ use std::fs::File;
1313
use std::io::{self, BufRead, BufReader};
1414
use std::mem;
1515
use std::path::Path;
16+
use std::sync::OnceLock;
1617

1718
use super::regex::Regex;
1819
use crate::parsing::syntax_definition::ContextId;
19-
use once_cell::sync::OnceCell;
2020
use serde_derive::{Deserialize, Serialize};
2121

2222
/// A syntax set holds multiple syntaxes that have been linked together.
@@ -34,8 +34,8 @@ pub struct SyntaxSet {
3434
/// Stores the syntax index for every path that was loaded
3535
path_syntaxes: Vec<(String, usize)>,
3636

37-
#[serde(skip_serializing, skip_deserializing, default = "OnceCell::new")]
38-
first_line_cache: OnceCell<FirstLineCache>,
37+
#[serde(skip_serializing, skip_deserializing, default = "OnceLock::new")]
38+
first_line_cache: OnceLock<FirstLineCache>,
3939
/// Metadata, e.g. indent and commenting information.
4040
///
4141
/// NOTE: if serializing, you should handle metadata manually; that is, you should serialize and
@@ -58,7 +58,7 @@ pub struct SyntaxReference {
5858
#[serde(serialize_with = "ordered_map")]
5959
pub variables: HashMap<String, String>,
6060
#[serde(skip)]
61-
pub(crate) lazy_contexts: OnceCell<LazyContexts>,
61+
pub(crate) lazy_contexts: OnceLock<LazyContexts>,
6262
pub(crate) serialized_lazy_contexts: Vec<u8>,
6363
}
6464

@@ -114,7 +114,7 @@ impl Clone for SyntaxSet {
114114
syntaxes: self.syntaxes.clone(),
115115
path_syntaxes: self.path_syntaxes.clone(),
116116
// Will need to be re-initialized
117-
first_line_cache: OnceCell::new(),
117+
first_line_cache: OnceLock::new(),
118118
#[cfg(feature = "metadata")]
119119
metadata: self.metadata.clone(),
120120
}
@@ -126,7 +126,7 @@ impl Default for SyntaxSet {
126126
SyntaxSet {
127127
syntaxes: Vec::new(),
128128
path_syntaxes: Vec::new(),
129-
first_line_cache: OnceCell::new(),
129+
first_line_cache: OnceLock::new(),
130130
#[cfg(feature = "metadata")]
131131
metadata: Metadata::default(),
132132
}
@@ -627,7 +627,7 @@ impl SyntaxSetBuilder {
627627
first_line_match,
628628
hidden,
629629
variables,
630-
lazy_contexts: OnceCell::new(),
630+
lazy_contexts: OnceLock::new(),
631631
serialized_lazy_contexts: Vec::new(), // initialized in the last step
632632
};
633633
syntaxes.push(syntax);
@@ -711,7 +711,7 @@ impl SyntaxSetBuilder {
711711
SyntaxSet {
712712
syntaxes,
713713
path_syntaxes,
714-
first_line_cache: OnceCell::new(),
714+
first_line_cache: OnceLock::new(),
715715
#[cfg(feature = "metadata")]
716716
metadata,
717717
}

src/parsing/yaml_load.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl SyntaxDefinition {
9595
return Err(ParseSyntaxError::EmptyFile);
9696
}
9797
let doc = &docs[0];
98-
let mut scope_repo = SCOPE_REPO.lock().unwrap();
98+
let mut scope_repo = scope_repo().lock().unwrap();
9999
SyntaxDefinition::parse_top_level(
100100
doc,
101101
scope_repo.deref_mut(),

tests/snapshots/public_api__public_api.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,7 @@ impl core::marker::Unpin for syntect::parsing::SyntaxSetBuilder
12191219
impl core::panic::unwind_safe::RefUnwindSafe for syntect::parsing::SyntaxSetBuilder
12201220
impl core::panic::unwind_safe::UnwindSafe for syntect::parsing::SyntaxSetBuilder
12211221
pub const syntect::parsing::ATOM_LEN_BITS: u16 = 3u16
1222-
pub static syntect::parsing::SCOPE_REPO: once_cell::sync::Lazy<std::sync::mutex::Mutex<syntect::parsing::ScopeRepository>>
1222+
pub fn syntect::parsing::scope_repo() -> &'static std::sync::mutex::Mutex<syntect::parsing::ScopeRepository>
12231223
pub mod syntect::util
12241224
pub struct syntect::util::LinesWithEndings<'a>
12251225
impl<'a> syntect::util::LinesWithEndings<'a>

0 commit comments

Comments
 (0)