Open
Description
Introduce a macro that lazily initialise a static regex expression, call it lazy_regex
.
This is helpful whenever you want to avoid the compile-at-each-iteration pattern when using regexes across threads.
The code is simply,
macro_rules! lazy_regex_1 {
($re:literal $(,)?) => {{
static RE: SyncOnceCell<regex::Regex> = SyncOnceCell::new();
RE.get_or_init(|| Regex::new($re).unwrap())
}};
}
or
macro_rules! lazy_regex_2 {
($re:literal $(,)?) => {{
static RE: SyncLazy<regex::Regex> = SyncLazy::new(|| Regex::new($re).unwrap());
&RE
}};
}
Which is only a slight alteration from the one provided in the once_cell
documentation. There are many possible variations of this --- there are many ways to lazily initialise a static variable. I think this is something that is common enough that it should be included.
Thank you.