From 8519bfcbd71809f392291d9172420afdfbd9816f Mon Sep 17 00:00:00 2001 From: xrelkd <46590321+xrelkd@users.noreply.github.com> Date: Sun, 19 May 2024 12:43:57 +0800 Subject: [PATCH] feat: replace `lazy_static` with `once_cell` --- Cargo.lock | 6 +++--- crates/base/Cargo.toml | 2 +- crates/base/src/lib.rs | 33 +++++++++++++++---------------- crates/metrics/Cargo.toml | 10 +++++----- crates/metrics/src/server.rs | 23 ++++++++++----------- crates/server/Cargo.toml | 2 +- crates/server/src/grpc/system.rs | 16 +++++++-------- crates/server/src/metrics/dbus.rs | 19 +++++++++--------- crates/server/src/metrics/grpc.rs | 11 +++++------ 9 files changed, 60 insertions(+), 62 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1e921435..2ff33e5f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -744,8 +744,8 @@ dependencies = [ "http 1.1.0", "humansize", "image", - "lazy_static", "mime", + "once_cell", "regex", "semver", "serde", @@ -857,8 +857,8 @@ dependencies = [ "async-trait", "axum 0.7.5", "bytes", - "lazy_static", "mime", + "once_cell", "prometheus", "snafu 0.8.2", "tokio", @@ -911,10 +911,10 @@ dependencies = [ "futures", "hex", "humansize", - "lazy_static", "mime", "notify", "notify-rust", + "once_cell", "parking_lot", "prometheus", "regex", diff --git a/crates/base/Cargo.toml b/crates/base/Cargo.toml index ce297982..58e34fdd 100644 --- a/crates/base/Cargo.toml +++ b/crates/base/Cargo.toml @@ -23,8 +23,8 @@ bytes = "1" directories = "5" humansize = "2" image = "0.25" -lazy_static = "1" mime = "0.3" +once_cell = "1" regex = "1" semver = "1" sha2 = "0.10" diff --git a/crates/base/src/lib.rs b/crates/base/src/lib.rs index b8dac711..21f4749b 100644 --- a/crates/base/src/lib.rs +++ b/crates/base/src/lib.rs @@ -15,7 +15,7 @@ use std::{ use bytes::Bytes; use directories::ProjectDirs; -use lazy_static::lazy_static; +use once_cell::sync::Lazy; pub use self::{ entry::{Entry as ClipEntry, Error as ClipEntryError, Metadata as ClipEntryMetadata}, @@ -32,16 +32,15 @@ pub const DBUS_SYSTEM_OBJECT_PATH: &str = "/org/clipcat/clipcat/system"; pub const DBUS_WATCHER_OBJECT_PATH: &str = "/org/clipcat/clipcat/watcher"; pub const DBUS_MANAGER_OBJECT_PATH: &str = "/org/clipcat/clipcat/manager"; -lazy_static! { - pub static ref PROJECT_SEMVER: semver::Version = semver::Version::parse(PROJECT_VERSION) - .unwrap_or(semver::Version { - major: 0, - minor: 0, - patch: 0, - pre: semver::Prerelease::EMPTY, - build: semver::BuildMetadata::EMPTY - }); -} +pub static PROJECT_SEMVER: Lazy = Lazy::new(|| { + semver::Version::parse(PROJECT_VERSION).unwrap_or(semver::Version { + major: 0, + minor: 0, + patch: 0, + pre: semver::Prerelease::EMPTY, + build: semver::BuildMetadata::EMPTY, + }) +}); pub const PROJECT_NAME: &str = "clipcat"; pub const PROJECT_NAME_WITH_INITIAL_CAPITAL: &str = "Clipcat"; @@ -70,12 +69,12 @@ pub const DEFAULT_METRICS_HOST: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST); pub const DEFAULT_MENU_PROMPT: &str = "Clipcat"; -lazy_static::lazy_static! { -pub static ref PROJECT_CONFIG_DIR: PathBuf = ProjectDirs::from("", PROJECT_NAME, PROJECT_NAME) - .expect("Creating `ProjectDirs` should always success") - .config_dir() - .to_path_buf(); -} +pub static PROJECT_CONFIG_DIR: Lazy = Lazy::new(|| { + ProjectDirs::from("", PROJECT_NAME, PROJECT_NAME) + .expect("Creating `ProjectDirs` should always success") + .config_dir() + .to_path_buf() +}); #[must_use] pub fn fallback_project_config_directories() -> Vec { diff --git a/crates/metrics/Cargo.toml b/crates/metrics/Cargo.toml index 98ca906e..142f4492 100644 --- a/crates/metrics/Cargo.toml +++ b/crates/metrics/Cargo.toml @@ -20,11 +20,11 @@ axum = "0.7" tower = { version = "0.4", features = ["timeout"] } tower-http = { version = "0.5", features = ["trace"] } -bytes = "1" -lazy_static = "1" -mime = "0.3" -prometheus = "0.13" -snafu = "0.8" +bytes = "1" +mime = "0.3" +once_cell = "1" +prometheus = "0.13" +snafu = "0.8" [lints] workspace = true diff --git a/crates/metrics/src/server.rs b/crates/metrics/src/server.rs index 5e58eab6..0478c5ed 100644 --- a/crates/metrics/src/server.rs +++ b/crates/metrics/src/server.rs @@ -8,8 +8,8 @@ use axum::{ routing, Router, }; use bytes::{BufMut, BytesMut}; -use lazy_static::lazy_static; use mime::Mime; +use once_cell::sync::Lazy; use prometheus::{Encoder, TextEncoder}; use snafu::ResultExt; use tokio::net::TcpListener; @@ -19,12 +19,13 @@ use crate::{ traits, }; -lazy_static! { - static ref OPENMETRICS_TEXT: Mime = - Mime::from_str("application/openmetrics-text; version=1.0.0; charset=utf-8") - .expect("is valid mime type; qed"); - static ref ENCODER: TextEncoder = TextEncoder::new(); -} +// FIXME: use `OPENMETRICS_TEXT` +#[allow(dead_code)] +static OPENMETRICS_TEXT: Lazy = Lazy::new(|| { + Mime::from_str("application/openmetrics-text; version=1.0.0; charset=utf-8") + .expect("is valid mime type; qed") +}); +static ENCODER: Lazy = Lazy::new(TextEncoder::new); async fn metrics(Extension(metrics): Extension) -> Response where @@ -79,14 +80,14 @@ where #[cfg(test)] mod tests { - use lazy_static::initialize; + use once_cell::sync::Lazy; use crate::server::{ENCODER, OPENMETRICS_TEXT}; #[test] - fn test_lazy_static() { - initialize(&OPENMETRICS_TEXT); - initialize(&ENCODER); + fn test_once_cell_lazy() { + let _ = Lazy::get(&OPENMETRICS_TEXT).unwrap(); + let _ = Lazy::get(&ENCODER).unwrap(); } #[test] diff --git a/crates/server/Cargo.toml b/crates/server/Cargo.toml index bde5f499..2b39ebb7 100644 --- a/crates/server/Cargo.toml +++ b/crates/server/Cargo.toml @@ -37,10 +37,10 @@ zvariant = "4" hex = "0.4" humansize = "2" -lazy_static = "1" mime = "0.3" notify = "6" notify-rust = "4" +once_cell = "1" parking_lot = "0.12" prometheus = "0.13" regex = "1" diff --git a/crates/server/src/grpc/system.rs b/crates/server/src/grpc/system.rs index 43b54c78..3b89fa5a 100644 --- a/crates/server/src/grpc/system.rs +++ b/crates/server/src/grpc/system.rs @@ -1,15 +1,13 @@ use clipcat_proto as proto; -use lazy_static::lazy_static; +use once_cell::sync::Lazy; use tonic::{Request, Response, Status}; -lazy_static! { - static ref GET_SYSTEM_VERSION_RESPONSE: proto::GetSystemVersionResponse = - proto::GetSystemVersionResponse { - major: clipcat_base::PROJECT_SEMVER.major, - minor: clipcat_base::PROJECT_SEMVER.minor, - patch: clipcat_base::PROJECT_SEMVER.patch - }; -} +static GET_SYSTEM_VERSION_RESPONSE: Lazy = + Lazy::new(|| proto::GetSystemVersionResponse { + major: clipcat_base::PROJECT_SEMVER.major, + minor: clipcat_base::PROJECT_SEMVER.minor, + patch: clipcat_base::PROJECT_SEMVER.patch, + }); pub struct SystemService {} diff --git a/crates/server/src/metrics/dbus.rs b/crates/server/src/metrics/dbus.rs index d85ed4d3..fb9392bd 100644 --- a/crates/server/src/metrics/dbus.rs +++ b/crates/server/src/metrics/dbus.rs @@ -1,13 +1,14 @@ -use lazy_static::lazy_static; +use once_cell::sync::Lazy; use prometheus::{Histogram, HistogramOpts, IntCounter}; -lazy_static! { - pub static ref REQUESTS_TOTAL: IntCounter = - IntCounter::new("dbus_requests_total", "Total number of request from D-Bus") - .expect("setup metrics"); - pub static ref REQUEST_DURATION_SECONDS: Histogram = Histogram::with_opts(HistogramOpts::new( +pub static REQUESTS_TOTAL: Lazy = Lazy::new(|| { + IntCounter::new("dbus_requests_total", "Total number of request from D-Bus") + .expect("setup metrics") +}); +pub static REQUEST_DURATION_SECONDS: Lazy = Lazy::new(|| { + Histogram::with_opts(HistogramOpts::new( "dbus_request_duration_seconds", - "Latencies of handling request with D-Bus in seconds" + "Latencies of handling request with D-Bus in seconds", )) - .expect("setup metrics"); -} + .expect("setup metrics") +}); diff --git a/crates/server/src/metrics/grpc.rs b/crates/server/src/metrics/grpc.rs index 1414fc45..ce8c9364 100644 --- a/crates/server/src/metrics/grpc.rs +++ b/crates/server/src/metrics/grpc.rs @@ -1,8 +1,7 @@ -use lazy_static::lazy_static; +use once_cell::sync::Lazy; use prometheus::IntCounter; -lazy_static! { - pub static ref REQUESTS_TOTAL: IntCounter = - IntCounter::new("grpc_requests_total", "Total number of request from gRPC") - .expect("setup metrics"); -} +pub static REQUESTS_TOTAL: Lazy = Lazy::new(|| { + IntCounter::new("grpc_requests_total", "Total number of request from gRPC") + .expect("setup metrics") +});