From 8e9f0dfa7992cc7475959f05c29dfba24d590b3a Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 2 Sep 2022 11:18:37 -0700 Subject: [PATCH] Replace lazy_static dependency with once_cell --- Cargo.toml | 2 +- src/lib.rs | 83 +++++++++++++++++++++++++----------------------------- 2 files changed, 39 insertions(+), 46 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ab0da2c4..b45faa65 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ license = "Apache-2.0/MIT" exclude = ["book/*"] [dependencies] -lazy_static = "1.4" +once_cell = "1.14" criterion-plot = { path = "plot", version = "0.4.5" } itertools = "0.10" serde = "1.0" diff --git a/src/lib.rs b/src/lib.rs index 42643719..7de8319d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,9 +36,6 @@ extern crate quickcheck; use clap::value_t; use regex::Regex; -#[macro_use] -extern crate lazy_static; - #[cfg(feature = "real_blackbox")] extern crate test; @@ -86,6 +83,7 @@ use std::sync::{Mutex, MutexGuard}; use std::time::Duration; use criterion_plot::{Version, VersionError}; +use once_cell::sync::Lazy; use crate::benchmark::BenchmarkConfig; use crate::benchmark::NamedRoutine; @@ -106,49 +104,44 @@ pub use crate::bencher::Bencher; pub use crate::benchmark::{Benchmark, BenchmarkDefinition, ParameterizedBenchmark}; pub use crate::benchmark_group::{BenchmarkGroup, BenchmarkId}; -lazy_static! { - static ref DEBUG_ENABLED: bool = std::env::var_os("CRITERION_DEBUG").is_some(); - static ref GNUPLOT_VERSION: Result = criterion_plot::version(); - static ref DEFAULT_PLOTTING_BACKEND: PlottingBackend = { - match &*GNUPLOT_VERSION { - Ok(_) => PlottingBackend::Gnuplot, - Err(e) => { - match e { - VersionError::Exec(_) => println!("Gnuplot not found, using plotters backend"), - e => println!( - "Gnuplot not found or not usable, using plotters backend\n{}", - e - ), - }; - PlottingBackend::Plotters - } - } - }; - static ref CARGO_CRITERION_CONNECTION: Option> = { - match std::env::var("CARGO_CRITERION_PORT") { - Ok(port_str) => { - let port: u16 = port_str.parse().ok()?; - let stream = TcpStream::connect(("localhost", port)).ok()?; - Some(Mutex::new(Connection::new(stream).ok()?)) - } - Err(_) => None, - } - }; - static ref DEFAULT_OUTPUT_DIRECTORY: PathBuf = { - // Set criterion home to (in descending order of preference): - // - $CRITERION_HOME (cargo-criterion sets this, but other users could as well) - // - $CARGO_TARGET_DIR/criterion - // - the cargo target dir from `cargo metadata` - // - ./target/criterion - if let Some(value) = env::var_os("CRITERION_HOME") { - PathBuf::from(value) - } else if let Some(path) = cargo_target_directory() { - path.join("criterion") - } else { - PathBuf::from("target/criterion") +static DEBUG_ENABLED: Lazy = Lazy::new(|| std::env::var_os("CRITERION_DEBUG").is_some()); +static GNUPLOT_VERSION: Lazy> = Lazy::new(criterion_plot::version); +static DEFAULT_PLOTTING_BACKEND: Lazy = Lazy::new(|| match &*GNUPLOT_VERSION { + Ok(_) => PlottingBackend::Gnuplot, + Err(e) => { + match e { + VersionError::Exec(_) => println!("Gnuplot not found, using plotters backend"), + e => println!( + "Gnuplot not found or not usable, using plotters backend\n{}", + e + ), + }; + PlottingBackend::Plotters + } +}); +static CARGO_CRITERION_CONNECTION: Lazy>> = + Lazy::new(|| match std::env::var("CARGO_CRITERION_PORT") { + Ok(port_str) => { + let port: u16 = port_str.parse().ok()?; + let stream = TcpStream::connect(("localhost", port)).ok()?; + Some(Mutex::new(Connection::new(stream).ok()?)) } - }; -} + Err(_) => None, + }); +static DEFAULT_OUTPUT_DIRECTORY: Lazy = Lazy::new(|| { + // Set criterion home to (in descending order of preference): + // - $CRITERION_HOME (cargo-criterion sets this, but other users could as well) + // - $CARGO_TARGET_DIR/criterion + // - the cargo target dir from `cargo metadata` + // - ./target/criterion + if let Some(value) = env::var_os("CRITERION_HOME") { + PathBuf::from(value) + } else if let Some(path) = cargo_target_directory() { + path.join("criterion") + } else { + PathBuf::from("target/criterion") + } +}); fn debug_enabled() -> bool { *DEBUG_ENABLED