Skip to content

Commit 70d34f6

Browse files
paolobarboliniJarema
authored andcommittedMar 14, 2023
Replace remaining uses of lazy_static with once_cell
once_cell is the correct way forward, as an API similar to it is also some day going to be stabilized in the Rust std. As some regexes were already using once_cell, this migrates the remaining ones.
1 parent 212010d commit 70d34f6

File tree

5 files changed

+18
-23
lines changed

5 files changed

+18
-23
lines changed
 

‎async-nats/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ time = { version = "0.3.20", features = ["parsing", "formatting", "serde", "serd
3636
rustls-native-certs = "0.6.2"
3737
tracing = "0.1"
3838
thiserror = "1.0"
39-
lazy_static = "1.4"
4039
base64 = "0.13"
4140
tokio-retry = "0.3"
4241
ring = "0.16"

‎async-nats/src/client.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use super::{header::HeaderMap, status::StatusCode, Command, Message, Subscriber}
1818
use bytes::Bytes;
1919
use futures::future::TryFutureExt;
2020
use futures::stream::StreamExt;
21-
use lazy_static::lazy_static;
21+
use once_cell::sync::Lazy;
2222
use regex::Regex;
2323
use std::fmt::Display;
2424
use std::sync::atomic::{AtomicU64, Ordering};
@@ -28,9 +28,8 @@ use thiserror::Error;
2828
use tokio::sync::mpsc;
2929
use tracing::trace;
3030

31-
lazy_static! {
32-
static ref VERSION_RE: Regex = Regex::new(r#"\Av?([0-9]+)\.?([0-9]+)?\.?([0-9]+)?"#).unwrap();
33-
}
31+
static VERSION_RE: Lazy<Regex> =
32+
Lazy::new(|| Regex::new(r#"\Av?([0-9]+)\.?([0-9]+)?\.?([0-9]+)?"#).unwrap());
3433

3534
/// An error returned from the [`Client::publish`], [`Client::publish_with_headers`],
3635
/// [`Client::publish_with_reply`] or [`Client::publish_with_reply_and_headers`] functions.

‎async-nats/src/jetstream/kv/mod.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::{
2121
use crate::{HeaderValue, StatusCode};
2222
use bytes::Bytes;
2323
use futures::StreamExt;
24-
use lazy_static::lazy_static;
24+
use once_cell::sync::Lazy;
2525
use regex::Regex;
2626
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
2727

@@ -50,10 +50,8 @@ fn kv_operation_from_maybe_headers(maybe_headers: Option<&String>) -> Operation
5050
fn kv_operation_from_stream_message(message: &RawMessage) -> Operation {
5151
kv_operation_from_maybe_headers(message.headers.as_ref())
5252
}
53-
lazy_static! {
54-
static ref VALID_BUCKET_RE: Regex = Regex::new(r#"\A[a-zA-Z0-9_-]+\z"#).unwrap();
55-
static ref VALID_KEY_RE: Regex = Regex::new(r#"\A[-/_=\.a-zA-Z0-9]+\z"#).unwrap();
56-
}
53+
static VALID_BUCKET_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r#"\A[a-zA-Z0-9_-]+\z"#).unwrap());
54+
static VALID_KEY_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r#"\A[-/_=\.a-zA-Z0-9]+\z"#).unwrap());
5755

5856
pub(crate) const MAX_HISTORY: i64 = 64;
5957
const ALL_KEYS: &str = ">";

‎async-nats/src/jetstream/object_store/mod.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ use std::{
2222

2323
use crate::{HeaderMap, HeaderValue};
2424
use base64::URL_SAFE;
25+
use once_cell::sync::Lazy;
2526
use ring::digest::SHA256;
2627
use tokio::io::AsyncReadExt;
2728

2829
use base64_url::base64;
2930
use futures::{Stream, StreamExt};
30-
use lazy_static::lazy_static;
3131
use regex::Regex;
3232
use serde::{Deserialize, Serialize};
3333
use tracing::{debug, trace};
@@ -41,10 +41,8 @@ const DEFAULT_CHUNK_SIZE: usize = 128 * 1024;
4141
const NATS_ROLLUP: &str = "Nats-Rollup";
4242
const ROLLUP_SUBJECT: &str = "sub";
4343

44-
lazy_static! {
45-
static ref BUCKET_NAME_RE: Regex = Regex::new(r#"\A[a-zA-Z0-9_-]+\z"#).unwrap();
46-
static ref OBJECT_NAME_RE: Regex = Regex::new(r#"\A[-/_=\.a-zA-Z0-9]+\z"#).unwrap();
47-
}
44+
static BUCKET_NAME_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r#"\A[a-zA-Z0-9_-]+\z"#).unwrap());
45+
static OBJECT_NAME_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r#"\A[-/_=\.a-zA-Z0-9]+\z"#).unwrap());
4846

4947
pub(crate) fn is_valid_bucket_name(bucket_name: &str) -> bool {
5048
BUCKET_NAME_RE.is_match(bucket_name)

‎async-nats/src/service/mod.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use futures::{
2727
stream::{self, SelectAll},
2828
Future, Stream, StreamExt,
2929
};
30-
use lazy_static::lazy_static;
30+
use once_cell::sync::Lazy;
3131
use regex::Regex;
3232
use serde::{Deserialize, Serialize};
3333
use serde_json::json;
@@ -43,13 +43,14 @@ const QUEUE_GROUP: &str = "q";
4343
pub const NATS_SERVICE_ERROR: &str = "Nats-Service-Error";
4444
pub const NATS_SERVICE_ERROR_CODE: &str = "Nats-Service-Error-Code";
4545

46-
lazy_static! {
47-
// uses recommended semver validation expression from
48-
// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
49-
static ref SEMVER: Regex = Regex::new(r#"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"#).unwrap();
50-
// From ADR-33: Name can only have A-Z, a-z, 0-9, dash, underscore.
51-
static ref NAME: Regex = Regex::new(r#"^[A-Za-z0-9\-_]+$"#).unwrap();
52-
}
46+
// uses recommended semver validation expression from
47+
// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
48+
static SEMVER: Lazy<Regex> = Lazy::new(|| {
49+
Regex::new(r#"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"#)
50+
.unwrap()
51+
});
52+
// From ADR-33: Name can only have A-Z, a-z, 0-9, dash, underscore.
53+
static NAME: Lazy<Regex> = Lazy::new(|| Regex::new(r#"^[A-Za-z0-9\-_]+$"#).unwrap());
5354

5455
/// Represents stats for all endpoints.
5556
#[derive(Debug, Clone, Serialize, Deserialize)]

0 commit comments

Comments
 (0)
Please sign in to comment.