Skip to content

Commit 6b59f2c

Browse files
authored
lazycell -> once_cell (#69)
1 parent a8c785f commit 6b59f2c

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

nlprule/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fs-err = "2.5"
2727
aho-corasick = "0.7"
2828
half = { version = "1.7", features = ["serde"] }
2929
srx = { version = "^0.1.3", features = ["serde"] }
30-
lazycell = "1"
30+
once_cell = "1"
3131
cfg-if = "1"
3232

3333
rayon-cond = "0.1"

nlprule/src/utils/regex.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! Adapts the approach from https://github.com/trishume/syntect/pull/270 with feature flags for the
44
//! different backends.
55
6-
use lazycell::AtomicLazyCell;
6+
use once_cell::sync::OnceCell;
77
use serde::{Deserialize, Deserializer, Serialize, Serializer};
88
use std::hash::{Hash, Hasher};
99

@@ -12,14 +12,14 @@ pub use regex_impl::{CaptureMatches, Captures, Match, Matches};
1212
#[derive(Debug)]
1313
pub struct Regex {
1414
regex_str: String,
15-
regex: AtomicLazyCell<regex_impl::Regex>,
15+
regex: OnceCell<regex_impl::Regex>,
1616
}
1717

1818
impl Clone for Regex {
1919
fn clone(&self) -> Self {
2020
Regex {
2121
regex_str: self.regex_str.clone(),
22-
regex: AtomicLazyCell::new(),
22+
regex: OnceCell::new(),
2323
}
2424
}
2525
}
@@ -57,7 +57,7 @@ impl Regex {
5757
pub fn new(regex_str: String) -> Self {
5858
Self {
5959
regex_str,
60-
regex: AtomicLazyCell::new(),
60+
regex: OnceCell::new(),
6161
}
6262
}
6363

@@ -68,15 +68,10 @@ impl Regex {
6868
}
6969

7070
fn regex(&self) -> &regex_impl::Regex {
71-
if let Some(regex) = self.regex.borrow() {
72-
regex
73-
} else {
74-
let regex = regex_impl::Regex::new(&self.regex_str).unwrap_or_else(|_| {
75-
panic!("regex string should be pre-tested: {}", self.regex_str)
76-
});
77-
self.regex.fill(regex).ok();
78-
self.regex.borrow().unwrap()
79-
}
71+
self.regex.get_or_init(|| {
72+
regex_impl::Regex::new(&self.regex_str)
73+
.unwrap_or_else(|_| panic!("regex string should be pre-tested: {}", self.regex_str))
74+
})
8075
}
8176

8277
pub fn is_match(&self, text: &str) -> bool {

0 commit comments

Comments
 (0)