Skip to content

Commit

Permalink
switch to LazyLock from std
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxVerevkin committed Aug 13, 2024
1 parent 23014f3 commit 2b8413a
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 19 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ neli-wifi = { version = "0.6", features = ["async"] }
nix = { version = "0.28", features = ["fs", "process"] }
nom = "7.1.2"
notmuch = { version = "0.8", optional = true }
once_cell = "1"
pipewire = { version = "0.8", default-features = false, optional = true }
regex = "1.5"
reqwest = { version = "0.11", features = ["json"] }
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/packages/pacman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::util::has_command;

make_log_macro!(debug, "pacman");

pub static PACMAN_UPDATES_DB: Lazy<PathBuf> = Lazy::new(|| {
pub static PACMAN_UPDATES_DB: LazyLock<PathBuf> = LazyLock::new(|| {
let path = match env::var_os("CHECKUPDATES_DB") {
Some(val) => val.into(),
None => {
Expand All @@ -27,7 +27,7 @@ pub static PACMAN_UPDATES_DB: Lazy<PathBuf> = Lazy::new(|| {
path
});

pub static PACMAN_DB: Lazy<PathBuf> = Lazy::new(|| {
pub static PACMAN_DB: LazyLock<PathBuf> = LazyLock::new(|| {
let path = env::var_os("DBPath")
.map(Into::into)
.unwrap_or_else(|| PathBuf::from("/var/lib/pacman/"));
Expand Down
3 changes: 1 addition & 2 deletions src/blocks/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub use std::borrow::Cow;
pub use std::collections::HashMap;
pub use std::fmt::Write;
pub use std::pin::Pin;
pub use std::sync::LazyLock;
pub use std::time::Duration;

pub use tokio::io::{AsyncBufRead, AsyncBufReadExt, AsyncReadExt, AsyncWriteExt};
Expand All @@ -25,8 +26,6 @@ pub use tokio::time::sleep;

pub use futures::{Stream, StreamExt};

pub use once_cell::sync::Lazy;

pub use smart_default::SmartDefault;

pub use async_trait::async_trait;
2 changes: 1 addition & 1 deletion src/blocks/privacy/pipewire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tokio::sync::Notify;

use super::*;

static CLIENT: Lazy<Result<Client>> = Lazy::new(Client::new);
static CLIENT: LazyLock<Result<Client>> = LazyLock::new(Client::new);

#[derive(Debug)]
struct Node {
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/sound/pulseaudio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use tokio::sync::Notify;
use super::super::prelude::*;
use super::{DeviceKind, SoundDevice};

static CLIENT: Lazy<Result<Client>> = Lazy::new(Client::new);
static CLIENT: LazyLock<Result<Client>> = LazyLock::new(Client::new);
static EVENT_LISTENER: Mutex<Vec<Weak<Notify>>> = Mutex::new(Vec::new());
static DEVICES: Lazy<Mutex<HashMap<(DeviceKind, String), VolInfo>>> = Lazy::new(default);
static DEVICES: LazyLock<Mutex<HashMap<(DeviceKind, String), VolInfo>>> = LazyLock::new(default);

// Default device names
pub(super) static DEFAULT_SOURCE: Mutex<Cow<'static, str>> =
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/weather/met_no.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ struct ForecastTimeInstant {
relative_humidity: Option<f64>,
}

static LEGENDS: Lazy<Option<LegendsStore>> =
Lazy::new(|| serde_json::from_str(include_str!("met_no_legends.json")).ok());
static LEGENDS: LazyLock<Option<LegendsStore>> =
LazyLock::new(|| serde_json::from_str(include_str!("met_no_legends.json")).ok());

const FORECAST_URL: &str = "https://api.met.no/weatherapi/locationforecast/2.0/compact";

Expand Down
6 changes: 3 additions & 3 deletions src/formatting/formatter/datetime.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use chrono::format::{Fixed, Item, StrftimeItems};
use chrono::{DateTime, Datelike, Local, LocalResult, Locale, TimeZone, Timelike};
use chrono_tz::{OffsetName, Tz};
use once_cell::sync::Lazy;

use std::fmt::Display;
use std::sync::LazyLock;

use super::*;

make_log_macro!(error, "datetime");

const DEFAULT_DATETIME_FORMAT: &str = "%a %d/%m %R";

pub static DEFAULT_DATETIME_FORMATTER: Lazy<DatetimeFormatter> =
Lazy::new(|| DatetimeFormatter::new(Some(DEFAULT_DATETIME_FORMAT), None).unwrap());
pub static DEFAULT_DATETIME_FORMATTER: LazyLock<DatetimeFormatter> =
LazyLock::new(|| DatetimeFormatter::new(Some(DEFAULT_DATETIME_FORMAT), None).unwrap());

#[derive(Debug)]
pub enum DatetimeFormatter {
Expand Down
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ pub use tokio;
use std::borrow::Cow;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::{Arc, LazyLock};
use std::time::Duration;

use futures::stream::{FuturesUnordered, StreamExt};
use futures::Stream;
use once_cell::sync::Lazy;
use tokio::process::Command;
use tokio::sync::{mpsc, Notify};

Expand All @@ -51,15 +50,15 @@ use crate::widget::{State, Widget};
const APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);
const REQWEST_TIMEOUT: Duration = Duration::from_secs(10);

static REQWEST_CLIENT: Lazy<reqwest::Client> = Lazy::new(|| {
static REQWEST_CLIENT: LazyLock<reqwest::Client> = LazyLock::new(|| {
reqwest::Client::builder()
.user_agent(APP_USER_AGENT)
.timeout(REQWEST_TIMEOUT)
.build()
.unwrap()
});

static REQWEST_CLIENT_IPV4: Lazy<reqwest::Client> = Lazy::new(|| {
static REQWEST_CLIENT_IPV4: LazyLock<reqwest::Client> = LazyLock::new(|| {
reqwest::Client::builder()
.user_agent(APP_USER_AGENT)
.local_address(Some(std::net::Ipv4Addr::UNSPECIFIED.into()))
Expand Down
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub use map;

macro_rules! regex {
($re:literal $(,)?) => {{
static RE: once_cell::sync::OnceCell<regex::Regex> = once_cell::sync::OnceCell::new();
static RE: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();
RE.get_or_init(|| regex::Regex::new($re).unwrap())
}};
}
Expand Down

0 comments on commit 2b8413a

Please sign in to comment.