From 5c4259023f95b994fc44d4a7c0cbd20c0f330587 Mon Sep 17 00:00:00 2001 From: "Tony Arcieri (iqlusion)" Date: Mon, 14 Nov 2022 13:52:27 -0700 Subject: [PATCH] Switch from `lazy_static` to `once_cell` (#442) The latter will hopefully eventually be upstreamed into `std`. --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- cli/Cargo.toml | 2 +- cli/src/terminal.rs | 16 +++++++--------- tests/integration.rs | 13 ++++--------- 5 files changed, 15 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5836df5..54b0888 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1277,12 +1277,12 @@ dependencies = [ "elliptic-curve", "env_logger", "hmac", - "lazy_static", "log", "nom", "num-bigint-dig", "num-integer", "num-traits", + "once_cell", "p256", "p384", "pbkdf2", @@ -1307,8 +1307,8 @@ version = "0.6.0" dependencies = [ "clap", "env_logger", - "lazy_static", "log", + "once_cell", "sha2", "subtle-encoding", "termcolor", diff --git a/Cargo.toml b/Cargo.toml index c1b2f3f..7a2edd4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,7 +49,7 @@ zeroize = "1" [dev-dependencies] env_logger = "0.9" -lazy_static = "1" +once_cell = "1" rsa = { version = "0.7.1", features = ["hazmat"] } signature = { version = "1.6.4", features = ["hazmat-preview"] } diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 73b4eaf..1fe0720 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -17,8 +17,8 @@ rust-version = "1.56" [dependencies] clap = { version = "4", features = ["derive"] } env_logger = "0.9" -lazy_static = "1" log = "0.4" +once_cell = "1" sha2 = "0.10" subtle-encoding = "0.5" termcolor = "1" diff --git a/cli/src/terminal.rs b/cli/src/terminal.rs index 8b5a2db..5af2fed 100644 --- a/cli/src/terminal.rs +++ b/cli/src/terminal.rs @@ -1,7 +1,7 @@ //! Status messages -use lazy_static::lazy_static; use log::debug; +use once_cell::sync::Lazy; use sha2::{Digest, Sha256}; use std::{ io::{self, Write}, @@ -59,16 +59,14 @@ macro_rules! status_err { }; } -lazy_static! { - /// Color configuration - static ref COLOR_CHOICE: Mutex> = Mutex::new(None); +/// Color configuration +static COLOR_CHOICE: Lazy>> = Lazy::new(|| Mutex::new(None)); - /// Standard output - pub static ref STDOUT: StandardStream = StandardStream::stdout(get_color_choice()); +/// Standard output +pub static STDOUT: Lazy = Lazy::new(|| StandardStream::stdout(get_color_choice())); - /// Standard error - pub static ref STDERR: StandardStream = StandardStream::stderr(get_color_choice()); -} +/// Standard error +pub static STDERR: Lazy = Lazy::new(|| StandardStream::stderr(get_color_choice())); /// Obtain the color configuration. /// diff --git a/tests/integration.rs b/tests/integration.rs index 155e649..747f1b7 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -3,8 +3,8 @@ #![forbid(unsafe_code)] #![warn(missing_docs, rust_2018_idioms, trivial_casts, unused_qualifications)] -use lazy_static::lazy_static; use log::trace; +use once_cell::sync::Lazy; use rand_core::{OsRng, RngCore}; use rsa::pkcs1v15; use sha2::{Digest, Sha256}; @@ -18,13 +18,7 @@ use yubikey::{ Error, MgmKey, PinPolicy, Serial, TouchPolicy, YubiKey, }; -lazy_static! { - /// Provide thread-safe access to a YubiKey - static ref YUBIKEY: Mutex = init_yubikey(); -} - -/// One-time test initialization and setup -fn init_yubikey() -> Mutex { +static YUBIKEY: Lazy> = Lazy::new(|| { // Only show logs if `RUST_LOG` is set if env::var("RUST_LOG").is_ok() { env_logger::builder().format_timestamp(None).init(); @@ -36,11 +30,12 @@ fn init_yubikey() -> Mutex { } else { YubiKey::open().unwrap() }; + trace!("serial: {}", yubikey.serial()); trace!("version: {}", yubikey.version()); Mutex::new(yubikey) -} +}); // // CCCID support