Skip to content

Commit

Permalink
Merge pull request #3600 from mulkieran/issue_project_698
Browse files Browse the repository at this point in the history
Use once_cell instead of lazy_static for lazy statics
  • Loading branch information
mulkieran authored Apr 29, 2024
2 parents a285710 + 59f9acb commit 5de7e8b
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 24 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.

5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -261,7 +257,6 @@ engine = [
"futures",
"iocuddle",
"itertools",
"lazy_static",
"libblkid-rs",
"libc",
"libcryptsetup-rs",
Expand Down
9 changes: 5 additions & 4 deletions src/engine/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Regex> =
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}"
Expand Down
6 changes: 3 additions & 3 deletions src/engine/strat_engine/backstore/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -43,9 +44,8 @@ use crate::{

const MIN_DEV_SIZE: Bytes = Bytes(IEC::Gi as u128);

lazy_static! {
static ref BLOCKDEVS_IN_PROGRESS: Mutex<HashSet<PathBuf>> = Mutex::new(HashSet::new());
}
static BLOCKDEVS_IN_PROGRESS: Lazy<Mutex<HashSet<PathBuf>>> =
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
Expand Down
17 changes: 10 additions & 7 deletions src/engine/strat_engine/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -118,8 +119,8 @@ const CLEVIS_EXEC_NAMES: &[&str] = &[
MKTEMP,
];

lazy_static! {
static ref EXECUTABLES: HashMap<String, Option<PathBuf>> = [
static EXECUTABLES: Lazy<HashMap<String, Option<PathBuf>>> = 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)),
Expand All @@ -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<PathBuf> = match std::option_env!("EXECUTABLES_PATHS") {
.collect()
});

static EXECUTABLES_PATHS: Lazy<Vec<PathBuf>> =
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
Expand Down
4 changes: 0 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5de7e8b

Please sign in to comment.