From a155e9a7db6a1ac91caaa6551994f3b2bdfd2439 Mon Sep 17 00:00:00 2001 From: frozenlib Date: Fri, 1 Dec 2023 22:38:54 +0900 Subject: [PATCH] Use std::sync::OnceLock instead of once_cell crate in parse_display_derive. --- parse-display-derive/Cargo.toml | 1 - parse-display-derive/src/format_syntax.rs | 10 ++++------ parse-display-derive/src/lib.rs | 20 +++++++++----------- parse-display-derive/src/regex_utils.rs | 9 +++++---- 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/parse-display-derive/Cargo.toml b/parse-display-derive/Cargo.toml index df6929d..cf1f5d8 100644 --- a/parse-display-derive/Cargo.toml +++ b/parse-display-derive/Cargo.toml @@ -20,5 +20,4 @@ quote = "1.0.29" proc-macro2 = "1.0.64" regex = "1.9.1" regex-syntax = "0.7.4" -once_cell = "1.18.0" structmeta = "0.2.0" diff --git a/parse-display-derive/src/format_syntax.rs b/parse-display-derive/src/format_syntax.rs index d14960a..6672cb2 100644 --- a/parse-display-derive/src/format_syntax.rs +++ b/parse-display-derive/src/format_syntax.rs @@ -1,10 +1,8 @@ -use once_cell::sync::Lazy; -use proc_macro2::Span; -use regex::*; - use std::fmt::{Display, Formatter}; use std::str::FromStr; +use proc_macro2::Span; + #[derive(Debug, PartialEq, Eq)] pub enum Sign { Plus, @@ -96,7 +94,7 @@ impl<'a> FormatSpec<'a> { } pub fn parse(s: &'a str) -> std::result::Result { - static RE: Lazy = lazy_regex!( + let re = regex!( "^\ ((?.)?\ (?[<>^]))??\ @@ -116,7 +114,7 @@ impl<'a> FormatSpec<'a> { $" ); - let c = RE.captures(s).ok_or(FormatParseError)?; + let c = re.captures(s).ok_or(FormatParseError)?; let fill = c.name("fill").map(|m| m.as_str().chars().next().unwrap()); let align = c.name("align").map(|m| m.as_str().parse().unwrap()); let sign = c.name("sign").map(|m| match m.as_str() { diff --git a/parse-display-derive/src/lib.rs b/parse-display-derive/src/lib.rs index c1d239f..2dd4e89 100644 --- a/parse-display-derive/src/lib.rs +++ b/parse-display-derive/src/lib.rs @@ -14,7 +14,6 @@ mod syn_utils; mod format_syntax; use crate::{format_syntax::*, regex_utils::*, syn_utils::*}; -use once_cell::sync::Lazy; use proc_macro2::{Span, TokenStream}; use quote::{format_ident, quote}; use regex::{Captures, Regex}; @@ -329,9 +328,8 @@ impl<'a> ParserBuilder<'a> { } fn push_regex(&mut self, s: &LitStr, context: &DisplayContext) -> Result<()> { - static REGEX_NUMBER: Lazy = lazy_regex!("^[0-9]+$"); - static REGEX_CAPTURE: Lazy = - lazy_regex!(r"(?\\*)\(\?(?

P?)<(?[_0-9a-zA-Z.]*)>"); + let regex_number = regex!("^[0-9]+$"); + let regex_capture = regex!(r"(?\\*)\(\?(?

P?)<(?[_0-9a-zA-Z.]*)>"); const IDX_ESC: usize = 1; const IDX_P: usize = 2; const IDX_KEY: usize = 3; @@ -340,7 +338,7 @@ impl<'a> ParserBuilder<'a> { } let text = s.value(); - let text_debug = REGEX_CAPTURE.replace_all(&text, |c: &Captures| { + let text_debug = regex_capture.replace_all(&text, |c: &Captures| { let esc = &c[IDX_ESC]; if is_escaped(esc) { return c[0].to_owned(); @@ -351,7 +349,7 @@ impl<'a> ParserBuilder<'a> { } else { key.replace('.', "_") }; - let key = REGEX_NUMBER.replace(&key, "_$0"); + let key = regex_number.replace(&key, "_$0"); format!("{esc}(?<{key}>") }); if let Err(e) = regex_syntax::ast::parse::Parser::new().parse(&text_debug) { @@ -361,7 +359,7 @@ impl<'a> ParserBuilder<'a> { let mut has_capture = false; let mut has_capture_empty = false; let mut p = ""; - let mut text = try_replace_all(®EX_CAPTURE, &text, |c: &Captures| -> Result { + let mut text = try_replace_all(regex_capture, &text, |c: &Captures| -> Result { let esc = &c[IDX_ESC]; if is_escaped(esc) { return Ok(c[0].to_owned()); @@ -989,8 +987,8 @@ impl DisplayFormat { Self::parse(&s.value(), s.span()) } fn parse(mut s: &str, span: Span) -> Result { - static REGEX_STR: Lazy = lazy_regex!(r"^[^{}]+"); - static REGEX_VAR: Lazy = lazy_regex!(r"^\{([^:{}]*)(?::([^}]*))?\}"); + let regex_str = regex!(r"^[^{}]+"); + let regex_var = regex!(r"^\{([^:{}]*)(?::([^}]*))?\}"); let mut parts = Vec::new(); while !s.is_empty() { if s.starts_with("{{") { @@ -1003,12 +1001,12 @@ impl DisplayFormat { s = &s[2..]; continue; } - if let Some(m) = REGEX_STR.find(s) { + if let Some(m) = regex_str.find(s) { parts.push(DisplayFormatPart::Str(m.as_str().into())); s = &s[m.end()..]; continue; } - if let Some(c) = REGEX_VAR.captures(s) { + if let Some(c) = regex_var.captures(s) { let arg = c.get(1).unwrap().as_str().into(); let format_spec = c.get(2).map_or("", |x| x.as_str()).into(); parts.push(DisplayFormatPart::Var { arg, format_spec }); diff --git a/parse-display-derive/src/regex_utils.rs b/parse-display-derive/src/regex_utils.rs index 3883c14..b725c5c 100644 --- a/parse-display-derive/src/regex_utils.rs +++ b/parse-display-derive/src/regex_utils.rs @@ -89,8 +89,9 @@ pub fn try_replace_all, E>( Ok(s) } -macro_rules! lazy_regex { - ($re:expr) => { - once_cell::sync::Lazy::new(|| regex::Regex::new($re).unwrap()) - }; +macro_rules! regex { + ($s:expr) => {{ + static RE: ::std::sync::OnceLock = ::std::sync::OnceLock::new(); + RE.get_or_init(|| ::regex::Regex::new($s).unwrap()) + }}; }