From f82dccf331f4c0a1f1734e7cc0d69d386b0e4624 Mon Sep 17 00:00:00 2001 From: Rafael Bachmann Date: Mon, 5 Aug 2024 14:31:55 +0200 Subject: [PATCH] Replace lazy_static with std::sync::OnceLock --- nats-server/Cargo.toml | 3 +-- nats-server/src/lib.rs | 14 +++++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/nats-server/Cargo.toml b/nats-server/Cargo.toml index 36234f764..0115d5e81 100644 --- a/nats-server/Cargo.toml +++ b/nats-server/Cargo.toml @@ -7,7 +7,6 @@ license = "Apache-2.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -lazy_static = "1.4.0" regex = { version = "1.7.1", default-features = false, features = ["std", "unicode-perl"] } url = "2" serde_json = "1.0.104" @@ -17,5 +16,5 @@ tokio-retry = "0.3.0" [dev-dependencies] async-nats = "0.31" -tokio = { version = "1", features = ["full"] } +tokio = { version = "1", features = ["full"] } futures = "0.3" diff --git a/nats-server/src/lib.rs b/nats-server/src/lib.rs index 639984f63..14ab4fa81 100644 --- a/nats-server/src/lib.rs +++ b/nats-server/src/lib.rs @@ -19,9 +19,7 @@ use std::process::{Child, Command}; use std::{env, fs}; use std::{thread, time::Duration}; -use lazy_static::lazy_static; use rand::Rng; -use regex::Regex; use serde_json::{self, Value}; pub struct Server { @@ -37,9 +35,11 @@ struct Inner { pidfile: PathBuf, } -lazy_static! { - static ref SD_RE: Regex = Regex::new(r#".+\sStore Directory:\s+"([^"]+)""#).unwrap(); - static ref CLIENT_RE: Regex = Regex::new(r".+\sclient connections on\s+(\S+)").unwrap(); +macro_rules! regex { + ($re:literal $(,)?) => {{ + static RE: std::sync::OnceLock = std::sync::OnceLock::new(); + RE.get_or_init(|| regex::Regex::new($re).unwrap()) + }}; } impl Drop for Server { @@ -48,7 +48,7 @@ impl Drop for Server { self.inner.child.wait().unwrap(); if let Ok(log) = fs::read_to_string(self.inner.logfile.as_os_str()) { // Check if we had JetStream running and if so cleanup the storage directory. - if let Some(caps) = SD_RE.captures(&log) { + if let Some(caps) = regex!(r#".+\sStore Directory:\s+"([^"]+)""#).captures(&log) { let sd = caps.get(1).map_or("", |m| m.as_str()); fs::remove_dir_all(sd).ok(); } @@ -120,7 +120,7 @@ impl Server { for _ in 0..100 { match fs::read_to_string(self.inner.logfile.as_os_str()) { Ok(l) => { - if let Some(cre) = CLIENT_RE.captures(&l) { + if let Some(cre) = regex!(r".+\sclient connections on\s+(\S+)").captures(&l) { return cre.get(1).unwrap().as_str().replace("0.0.0.0", "localhost"); } else { thread::sleep(Duration::from_millis(500));