Skip to content

Commit

Permalink
Merge pull request #74 from triarius/triarius/remove-lazy-static
Browse files Browse the repository at this point in the history
Replace lazy-static with once_lock
  • Loading branch information
triarius authored Jul 6, 2024
2 parents 58551d8 + 25a41a7 commit ce9d36e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ license = "MIT"
clap = { version = "4.5.7", features = ["derive"] }
eyre = "0.6.12"
itertools = "0.13.0"
lazy_static = "1.5.0"
rand = "0.8.5"
regex = "1.10.5"

Expand Down
23 changes: 18 additions & 5 deletions src/words.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use eyre::Result;
use lazy_static::lazy_static;
use regex::Regex;
use std::{
fs::File,
io::{BufRead, BufReader},
path::Path,
sync::OnceLock,
};

lazy_static! {
static ref RE: Regex = Regex::new("^[a-z]{4,}$").unwrap();
fn re() -> &'static Regex {
static RE: OnceLock<Regex> = OnceLock::new();
RE.get_or_init(|| Regex::new("^[a-z]{4,}$").unwrap())
}

pub(crate) fn list(path: Option<impl AsRef<Path>>) -> Result<Vec<String>> {
Expand All @@ -29,7 +30,7 @@ impl Words for WordsFromFixture {
let bytes = include_bytes!("fixtures/words");
Ok(String::from_utf8_lossy(bytes)
.split('\n')
.filter(|w| RE.is_match(w))
.filter(|w| re().is_match(w))
.map(std::borrow::ToOwned::to_owned)
.collect())
}
Expand All @@ -45,7 +46,19 @@ impl<P: AsRef<Path>> Words for WordsFromFile<P> {
Ok(BufReader::new(file)
.lines()
.map_while(std::result::Result::ok)
.filter(|w| RE.is_match(w))
.filter(|w| re().is_match(w))
.collect())
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_re() {
let re = super::re();
assert!(re.is_match("abcd"));
assert!(!re.is_match("abc"));
assert!(!re.is_match("abc!"));
assert!(!re.is_match("1234"));
}
}

0 comments on commit ce9d36e

Please sign in to comment.