From a155a6b353cdd7f3c468007117fffe56ef6f2094 Mon Sep 17 00:00:00 2001 From: Max Verevkin <34583604+MaxVerevkin@users.noreply.github.com> Date: Tue, 13 Aug 2024 22:50:45 +0300 Subject: [PATCH] switch to LazyLock from std (Rust 1.80) (#2074) * fix clippy lint * switch to LazyLock from std --- Cargo.lock | 1 - Cargo.toml | 1 - src/blocks/packages/pacman.rs | 4 ++-- src/blocks/prelude.rs | 3 +-- src/blocks/privacy/pipewire.rs | 2 +- src/blocks/rofication.rs | 2 +- src/blocks/sound/pulseaudio.rs | 4 ++-- src/blocks/weather/met_no.rs | 4 ++-- src/formatting/formatter/datetime.rs | 6 +++--- src/lib.rs | 7 +++---- src/util.rs | 2 +- 11 files changed, 16 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ed58d24d62..0554e89401 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1256,7 +1256,6 @@ dependencies = [ "nix 0.28.0", "nom", "notmuch", - "once_cell", "pipewire", "regex", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index 353ed207f4..86255703ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/src/blocks/packages/pacman.rs b/src/blocks/packages/pacman.rs index 0f74dc9635..fc4da0935f 100644 --- a/src/blocks/packages/pacman.rs +++ b/src/blocks/packages/pacman.rs @@ -10,7 +10,7 @@ use crate::util::has_command; make_log_macro!(debug, "pacman"); -pub static PACMAN_UPDATES_DB: Lazy = Lazy::new(|| { +pub static PACMAN_UPDATES_DB: LazyLock = LazyLock::new(|| { let path = match env::var_os("CHECKUPDATES_DB") { Some(val) => val.into(), None => { @@ -27,7 +27,7 @@ pub static PACMAN_UPDATES_DB: Lazy = Lazy::new(|| { path }); -pub static PACMAN_DB: Lazy = Lazy::new(|| { +pub static PACMAN_DB: LazyLock = LazyLock::new(|| { let path = env::var_os("DBPath") .map(Into::into) .unwrap_or_else(|| PathBuf::from("/var/lib/pacman/")); diff --git a/src/blocks/prelude.rs b/src/blocks/prelude.rs index d5f9ec3c14..3b587000b8 100644 --- a/src/blocks/prelude.rs +++ b/src/blocks/prelude.rs @@ -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}; @@ -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; diff --git a/src/blocks/privacy/pipewire.rs b/src/blocks/privacy/pipewire.rs index ce575f9b59..fac7909016 100644 --- a/src/blocks/privacy/pipewire.rs +++ b/src/blocks/privacy/pipewire.rs @@ -13,7 +13,7 @@ use tokio::sync::Notify; use super::*; -static CLIENT: Lazy> = Lazy::new(Client::new); +static CLIENT: LazyLock> = LazyLock::new(Client::new); #[derive(Debug)] struct Node { diff --git a/src/blocks/rofication.rs b/src/blocks/rofication.rs index a93b18b89f..3666514640 100644 --- a/src/blocks/rofication.rs +++ b/src/blocks/rofication.rs @@ -95,7 +95,7 @@ async fn rofication_status(socket_path: &str) -> Result<(usize, usize)> { // Response must be two integers: regular and critical, separated either by a comma or a \n let (num, crit) = response - .split_once(|x| x == ',' || x == '\n') + .split_once([',', '\n']) .error("Incorrect response")?; Ok(( num.parse().error("Incorrect response")?, diff --git a/src/blocks/sound/pulseaudio.rs b/src/blocks/sound/pulseaudio.rs index e61ad68939..c0e5334e3d 100644 --- a/src/blocks/sound/pulseaudio.rs +++ b/src/blocks/sound/pulseaudio.rs @@ -20,9 +20,9 @@ use tokio::sync::Notify; use super::super::prelude::*; use super::{DeviceKind, SoundDevice}; -static CLIENT: Lazy> = Lazy::new(Client::new); +static CLIENT: LazyLock> = LazyLock::new(Client::new); static EVENT_LISTENER: Mutex>> = Mutex::new(Vec::new()); -static DEVICES: Lazy>> = Lazy::new(default); +static DEVICES: LazyLock>> = LazyLock::new(default); // Default device names pub(super) static DEFAULT_SOURCE: Mutex> = diff --git a/src/blocks/weather/met_no.rs b/src/blocks/weather/met_no.rs index f670b33af3..bb6cd51e1a 100644 --- a/src/blocks/weather/met_no.rs +++ b/src/blocks/weather/met_no.rs @@ -171,8 +171,8 @@ struct ForecastTimeInstant { relative_humidity: Option, } -static LEGENDS: Lazy> = - Lazy::new(|| serde_json::from_str(include_str!("met_no_legends.json")).ok()); +static LEGENDS: LazyLock> = + 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"; diff --git a/src/formatting/formatter/datetime.rs b/src/formatting/formatter/datetime.rs index aaf3c11399..e296dd878f 100644 --- a/src/formatting/formatter/datetime.rs +++ b/src/formatting/formatter/datetime.rs @@ -1,9 +1,9 @@ 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::*; @@ -11,8 +11,8 @@ make_log_macro!(error, "datetime"); const DEFAULT_DATETIME_FORMAT: &str = "%a %d/%m %R"; -pub static DEFAULT_DATETIME_FORMATTER: Lazy = - Lazy::new(|| DatetimeFormatter::new(Some(DEFAULT_DATETIME_FORMAT), None).unwrap()); +pub static DEFAULT_DATETIME_FORMATTER: LazyLock = + LazyLock::new(|| DatetimeFormatter::new(Some(DEFAULT_DATETIME_FORMAT), None).unwrap()); #[derive(Debug)] pub enum DatetimeFormatter { diff --git a/src/lib.rs b/src/lib.rs index 2e4ca93b32..e9166cb623 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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}; @@ -51,7 +50,7 @@ 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 = Lazy::new(|| { +static REQWEST_CLIENT: LazyLock = LazyLock::new(|| { reqwest::Client::builder() .user_agent(APP_USER_AGENT) .timeout(REQWEST_TIMEOUT) @@ -59,7 +58,7 @@ static REQWEST_CLIENT: Lazy = Lazy::new(|| { .unwrap() }); -static REQWEST_CLIENT_IPV4: Lazy = Lazy::new(|| { +static REQWEST_CLIENT_IPV4: LazyLock = LazyLock::new(|| { reqwest::Client::builder() .user_agent(APP_USER_AGENT) .local_address(Some(std::net::Ipv4Addr::UNSPECIFIED.into())) diff --git a/src/util.rs b/src/util.rs index 562b9e0e41..6b1a669900 100644 --- a/src/util.rs +++ b/src/util.rs @@ -185,7 +185,7 @@ pub use map; macro_rules! regex { ($re:literal $(,)?) => {{ - static RE: once_cell::sync::OnceCell = once_cell::sync::OnceCell::new(); + static RE: std::sync::OnceLock = std::sync::OnceLock::new(); RE.get_or_init(|| regex::Regex::new($re).unwrap()) }}; }