From a380e333b997f4a92e7aaeeb77131b410b1d8fc0 Mon Sep 17 00:00:00 2001 From: Andrei Damian Date: Wed, 17 Jan 2024 08:00:33 +0200 Subject: [PATCH 1/2] replace lazy_static with OnceLock --- Cargo.toml | 1 - src/control.rs | 10 ++++++---- src/lib.rs | 5 +---- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 97f9eb7..ee528a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,6 @@ rust-version = "1.70" no-color = [] [dependencies] -lazy_static = "1" [target.'cfg(windows)'.dependencies.windows-sys] version = "0.48" diff --git a/src/control.rs b/src/control.rs index da09888..c48a3f3 100644 --- a/src/control.rs +++ b/src/control.rs @@ -4,6 +4,7 @@ use std::default::Default; use std::env; use std::io::{self, IsTerminal}; use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::OnceLock; /// Sets a flag to the console to use a virtual terminal environment. /// @@ -69,18 +70,19 @@ pub struct ShouldColorize { /// Use this to force colored to ignore the environment and always/never colorize /// See example/control.rs pub fn set_override(override_colorize: bool) { - SHOULD_COLORIZE.set_override(override_colorize) + should_colorize_global().set_override(override_colorize) } /// Remove the manual override and let the environment decide if it's ok to colorize /// See example/control.rs pub fn unset_override() { - SHOULD_COLORIZE.unset_override() + should_colorize_global().unset_override() } -lazy_static! { /// The persistent [`ShouldColorize`]. - pub static ref SHOULD_COLORIZE: ShouldColorize = ShouldColorize::from_env(); +pub fn should_colorize_global() -> &'static ShouldColorize { + static SHOULD_COLORIZE: OnceLock = OnceLock::new(); + SHOULD_COLORIZE.get_or_init(ShouldColorize::from_env) } impl Default for ShouldColorize { diff --git a/src/lib.rs b/src/lib.rs index 6942868..b5a02f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,9 +29,6 @@ //! modify them. #![warn(missing_docs)] -#[macro_use] -extern crate lazy_static; - #[cfg(test)] extern crate rspec; @@ -491,7 +488,7 @@ impl ColoredString { #[cfg(not(feature = "no-color"))] fn has_colors(&self) -> bool { - control::SHOULD_COLORIZE.should_colorize() + control::should_colorize_global().should_colorize() } #[cfg(feature = "no-color")] From fcb147e31ca8b2812ea8017f0ac4a72e7bf6a2be Mon Sep 17 00:00:00 2001 From: Andrei Damian Date: Wed, 17 Jan 2024 09:04:51 +0200 Subject: [PATCH 2/2] update changelog for replacing lazy_static with OnceLock --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf9cda1..4e1e78d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ - Implemented bitwise operators `BitAnd`, `BitOr`, `BitXor`, and `Not` which all combine `Styles`\'s and output `Style`\'s. These can also take a `Style` as an operand. - Added additional testing for all of the above changes. - Added methods `with_style` and `with_color_and_style` to `Colorize`. +- Replaced lazy_static with `std::sync::OnceLock`. Soft breaking change by removing the global `SHOULD_COLORIZE`. # 2.1.0 * Impl From for ColoredString by @mahor1221 in https://github.com/colored-rs/colored/pull/126