Skip to content

Commit a0aa27e

Browse files
committed
Simplify (cached) regex creation
1 parent ff1158c commit a0aa27e

File tree

1 file changed

+1
-6
lines changed

1 file changed

+1
-6
lines changed

src/regex.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,7 @@ static CACHE: LazyLock<Cache<String, Regex>> = LazyLock::new(|| Cache::new(REGEX
2727
impl Regex {
2828
/// Creates a new cached regex or retrieves it from the cache if it already exists.
2929
pub fn new(_: &Lua, re: &str) -> StdResult<Self, regex::Error> {
30-
if let Some(re) = CACHE.get(re) {
31-
return Ok(re);
32-
}
33-
let regex = Self(regex::bytes::Regex::new(re)?);
34-
CACHE.insert(re.to_string(), regex.clone());
35-
Ok(regex)
30+
CACHE.get_or_insert_with(re, || regex::bytes::Regex::new(re).map(Self))
3631
}
3732
}
3833

0 commit comments

Comments
 (0)