From 59f9acb9285d560f3b3203535a3b1af9311063dc Mon Sep 17 00:00:00 2001 From: mulhern Date: Fri, 26 Apr 2024 10:37:02 -0400 Subject: [PATCH] Use once_cell instead of lazy_static for lazy statics lazy_static is being deprecated and once_cell is now recommended. Signed-off-by: mulhern --- Cargo.lock | 1 - Cargo.toml | 5 ----- src/engine/shared.rs | 9 +++++---- src/engine/strat_engine/backstore/devices.rs | 6 +++--- src/engine/strat_engine/cmd.rs | 17 ++++++++++------- src/lib.rs | 4 ---- 6 files changed, 18 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f6aa129c22..7fb5591329 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1305,7 +1305,6 @@ dependencies = [ "futures", "iocuddle", "itertools 0.12.0", - "lazy_static", "libblkid-rs", "libc", "libcryptsetup-rs", diff --git a/Cargo.toml b/Cargo.toml index 990fedb95a..f5ad15fce9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -129,10 +129,6 @@ optional = true version = "0.12.0" optional = true -[dependencies.lazy_static] -version = "1.4.0" -optional = true - [dependencies.libblkid-rs] version = "0.3.2" optional = true @@ -261,7 +257,6 @@ engine = [ "futures", "iocuddle", "itertools", - "lazy_static", "libblkid-rs", "libc", "libcryptsetup-rs", diff --git a/src/engine/shared.rs b/src/engine/shared.rs index 537b4c73c6..e1a8a0c88f 100644 --- a/src/engine/shared.rs +++ b/src/engine/shared.rs @@ -15,6 +15,7 @@ use std::{ use chrono::{DateTime, LocalResult, TimeZone, Utc}; use nix::poll::{poll, PollFd, PollFlags}; +use once_cell::sync::Lazy; use regex::Regex; use devicemapper::{Bytes, Sectors, IEC, SECTOR_SIZE}; @@ -172,10 +173,10 @@ pub fn validate_name(name: &str) -> StratisResult<()> { "Provided string contains control characters: {name}" ))); } - lazy_static! { - static ref NAME_UDEVREGEX: Regex = - Regex::new(r"[[:ascii:]&&[^0-9A-Za-z#+-.:=@_/]]+").expect("regex is valid"); - } + + static NAME_UDEVREGEX: Lazy = + Lazy::new(|| Regex::new(r"[[:ascii:]&&[^0-9A-Za-z#+-.:=@_/]]+").expect("regex is valid")); + if NAME_UDEVREGEX.is_match(name) { return Err(StratisError::Msg(format!( "Provided string contains characters not allowed in udev symlinks: {name}" diff --git a/src/engine/strat_engine/backstore/devices.rs b/src/engine/strat_engine/backstore/devices.rs index c8cb8c451f..5ed501edfd 100644 --- a/src/engine/strat_engine/backstore/devices.rs +++ b/src/engine/strat_engine/backstore/devices.rs @@ -15,6 +15,7 @@ use std::{ use chrono::Utc; use itertools::Itertools; use nix::sys::stat::stat; +use once_cell::sync::Lazy; use devicemapper::{Bytes, Device, Sectors, IEC}; use libblkid_rs::{BlkidCache, BlkidProbe}; @@ -43,9 +44,8 @@ use crate::{ const MIN_DEV_SIZE: Bytes = Bytes(IEC::Gi as u128); -lazy_static! { - static ref BLOCKDEVS_IN_PROGRESS: Mutex> = Mutex::new(HashSet::new()); -} +static BLOCKDEVS_IN_PROGRESS: Lazy>> = + Lazy::new(|| Mutex::new(HashSet::new())); // Get information that can be obtained from udev for the block device // identified by devnode. Return an error if there was an error finding the diff --git a/src/engine/strat_engine/cmd.rs b/src/engine/strat_engine/cmd.rs index 68ed6fa7fe..7dc6fe2204 100644 --- a/src/engine/strat_engine/cmd.rs +++ b/src/engine/strat_engine/cmd.rs @@ -25,6 +25,7 @@ use std::{ use either::Either; use libc::c_uint; use libcryptsetup_rs::SafeMemHandle; +use once_cell::sync::Lazy; use semver::{Version, VersionReq}; use serde_json::Value; @@ -118,8 +119,8 @@ const CLEVIS_EXEC_NAMES: &[&str] = &[ MKTEMP, ]; -lazy_static! { - static ref EXECUTABLES: HashMap> = [ +static EXECUTABLES: Lazy>> = Lazy::new(|| { + [ (MKFS_XFS.to_string(), find_executable(MKFS_XFS)), (THIN_CHECK.to_string(), find_executable(THIN_CHECK)), (THIN_REPAIR.to_string(), find_executable(THIN_REPAIR)), @@ -129,20 +130,22 @@ lazy_static! { (XFS_GROWFS.to_string(), find_executable(XFS_GROWFS)), ( THIN_METADATA_SIZE.to_string(), - find_executable(THIN_METADATA_SIZE) + find_executable(THIN_METADATA_SIZE), ), ] .iter() .cloned() - .collect(); - static ref EXECUTABLES_PATHS: Vec = match std::option_env!("EXECUTABLES_PATHS") { + .collect() +}); + +static EXECUTABLES_PATHS: Lazy> = + Lazy::new(|| match std::option_env!("EXECUTABLES_PATHS") { Some(paths) => std::env::split_paths(paths).collect(), None => ["/usr/sbin", "/sbin", "/usr/bin", "/bin"] .iter() .map(|p| p.into()) .collect(), - }; -} + }); /// Verify that all executables that the engine might invoke are available at some /// path. Return an error if any are missing. Required to be called on engine diff --git a/src/lib.rs b/src/lib.rs index d56a3ac82c..83081473ac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,10 +23,6 @@ extern crate proptest; #[macro_use] extern crate assert_matches; -#[cfg(feature = "engine")] -#[macro_use] -extern crate lazy_static; - #[cfg(feature = "engine")] #[macro_use] extern crate serde_json;