Skip to content

Commit

Permalink
feat: replace lazy_static with once_cell
Browse files Browse the repository at this point in the history
  • Loading branch information
xrelkd committed May 19, 2024
1 parent f5ce9a9 commit 8519bfc
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 62 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
33 changes: 16 additions & 17 deletions crates/base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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<semver::Version> = 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";
Expand Down Expand Up @@ -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<PathBuf> = 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<PathBuf> {
Expand Down
10 changes: 5 additions & 5 deletions crates/metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 12 additions & 11 deletions crates/metrics/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Mime> = Lazy::new(|| {
Mime::from_str("application/openmetrics-text; version=1.0.0; charset=utf-8")
.expect("is valid mime type; qed")
});
static ENCODER: Lazy<TextEncoder> = Lazy::new(TextEncoder::new);

async fn metrics<Metrics>(Extension(metrics): Extension<Metrics>) -> Response<Body>
where
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion crates/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 7 additions & 9 deletions crates/server/src/grpc/system.rs
Original file line number Diff line number Diff line change
@@ -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<proto::GetSystemVersionResponse> =
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 {}

Expand Down
19 changes: 10 additions & 9 deletions crates/server/src/metrics/dbus.rs
Original file line number Diff line number Diff line change
@@ -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<IntCounter> = Lazy::new(|| {
IntCounter::new("dbus_requests_total", "Total number of request from D-Bus")
.expect("setup metrics")
});
pub static REQUEST_DURATION_SECONDS: Lazy<Histogram> = 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")
});
11 changes: 5 additions & 6 deletions crates/server/src/metrics/grpc.rs
Original file line number Diff line number Diff line change
@@ -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<IntCounter> = Lazy::new(|| {
IntCounter::new("grpc_requests_total", "Total number of request from gRPC")
.expect("setup metrics")
});

0 comments on commit 8519bfc

Please sign in to comment.