Skip to content

Commit

Permalink
lazycell -> once_cell (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
bminixhofer authored Apr 17, 2021
1 parent a8c785f commit 6b59f2c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
2 changes: 1 addition & 1 deletion nlprule/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fs-err = "2.5"
aho-corasick = "0.7"
half = { version = "1.7", features = ["serde"] }
srx = { version = "^0.1.3", features = ["serde"] }
lazycell = "1"
once_cell = "1"
cfg-if = "1"

rayon-cond = "0.1"
Expand Down
21 changes: 8 additions & 13 deletions nlprule/src/utils/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Adapts the approach from https://github.com/trishume/syntect/pull/270 with feature flags for the
//! different backends.
use lazycell::AtomicLazyCell;
use once_cell::sync::OnceCell;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::hash::{Hash, Hasher};

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

impl Clone for Regex {
fn clone(&self) -> Self {
Regex {
regex_str: self.regex_str.clone(),
regex: AtomicLazyCell::new(),
regex: OnceCell::new(),
}
}
}
Expand Down Expand Up @@ -57,7 +57,7 @@ impl Regex {
pub fn new(regex_str: String) -> Self {
Self {
regex_str,
regex: AtomicLazyCell::new(),
regex: OnceCell::new(),
}
}

Expand All @@ -68,15 +68,10 @@ impl Regex {
}

fn regex(&self) -> &regex_impl::Regex {
if let Some(regex) = self.regex.borrow() {
regex
} else {
let regex = regex_impl::Regex::new(&self.regex_str).unwrap_or_else(|_| {
panic!("regex string should be pre-tested: {}", self.regex_str)
});
self.regex.fill(regex).ok();
self.regex.borrow().unwrap()
}
self.regex.get_or_init(|| {
regex_impl::Regex::new(&self.regex_str)
.unwrap_or_else(|_| panic!("regex string should be pre-tested: {}", self.regex_str))
})
}

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

0 comments on commit 6b59f2c

Please sign in to comment.