From 9aa4f6a1ce41d1ba0e302399942b77ddb5c0576e Mon Sep 17 00:00:00 2001 From: brooks Date: Fri, 10 Jan 2025 12:46:26 -0500 Subject: [PATCH] clippy: static_mut_refs --- metrics/src/counter.rs | 91 +++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 58 deletions(-) diff --git a/metrics/src/counter.rs b/metrics/src/counter.rs index a500ca8ed88194..0497775e04e6cd 100644 --- a/metrics/src/counter.rs +++ b/metrics/src/counter.rs @@ -214,20 +214,14 @@ mod tests { serial_test::serial, std::{ env, - sync::{atomic::Ordering, Once, RwLock}, + sync::{atomic::Ordering, LazyLock, RwLock}, }, }; fn get_env_lock() -> &'static RwLock<()> { - static mut ENV_LOCK: Option> = None; - static INIT_HOOK: Once = Once::new(); + static ENV_LOCK: LazyLock> = LazyLock::new(|| RwLock::new(())); - unsafe { - INIT_HOOK.call_once(|| { - ENV_LOCK = Some(RwLock::new(())); - }); - ENV_LOCK.as_ref().unwrap() - } + &ENV_LOCK } /// Try to initialize the logger with a filter level of INFO. @@ -251,29 +245,20 @@ mod tests { fn test_counter() { try_init_logger_at_level_info().ok(); let _readlock = get_env_lock().read(); - static mut COUNTER: Counter = create_counter!("test", 1000, 1); - unsafe { - COUNTER.init(); - } - let count = 1; - inc_counter!(COUNTER, Level::Info, count); - unsafe { - assert_eq!(COUNTER.counts.load(Ordering::Relaxed), 1); - assert_eq!(COUNTER.times.load(Ordering::Relaxed), 1); - assert_eq!(COUNTER.lograte.load(Ordering::Relaxed), 1000); - assert_eq!(COUNTER.lastlog.load(Ordering::Relaxed), 0); - assert_eq!(COUNTER.name, "test"); - } + let mut counter = create_counter!("test", 1000, 1); + counter.init(); + counter.inc(Level::Info, 1); + assert_eq!(counter.counts.load(Ordering::Relaxed), 1); + assert_eq!(counter.times.load(Ordering::Relaxed), 1); + assert_eq!(counter.lograte.load(Ordering::Relaxed), 1000); + assert_eq!(counter.lastlog.load(Ordering::Relaxed), 0); + assert_eq!(counter.name, "test"); for _ in 0..199 { - inc_counter!(COUNTER, Level::Info, 2); - } - unsafe { - assert_eq!(COUNTER.lastlog.load(Ordering::Relaxed), 397); - } - inc_counter!(COUNTER, Level::Info, 2); - unsafe { - assert_eq!(COUNTER.lastlog.load(Ordering::Relaxed), 399); + counter.inc(Level::Info, 2); } + assert_eq!(counter.lastlog.load(Ordering::Relaxed), 397); + counter.inc(Level::Info, 2); + assert_eq!(counter.lastlog.load(Ordering::Relaxed), 399); } #[test] @@ -282,14 +267,12 @@ mod tests { try_init_logger_at_level_info().ok(); let _readlock = get_env_lock().read(); env::remove_var("SOLANA_DEFAULT_METRICS_RATE"); - static mut COUNTER: Counter = create_counter!("test", 1000, 0); - unsafe { - COUNTER.init(); - assert_eq!( - COUNTER.metricsrate.load(Ordering::Relaxed), - DEFAULT_METRICS_RATE - ); - } + let mut counter = create_counter!("test", 1000, 0); + counter.init(); + assert_eq!( + counter.metricsrate.load(Ordering::Relaxed), + DEFAULT_METRICS_RATE + ); } #[test] @@ -298,11 +281,9 @@ mod tests { try_init_logger_at_level_info().ok(); let _writelock = get_env_lock().write(); env::set_var("SOLANA_DEFAULT_METRICS_RATE", "50"); - static mut COUNTER: Counter = create_counter!("test", 1000, 0); - unsafe { - COUNTER.init(); - assert_eq!(COUNTER.metricsrate.load(Ordering::Relaxed), 50); - } + let mut counter = create_counter!("test", 1000, 0); + counter.init(); + assert_eq!(counter.metricsrate.load(Ordering::Relaxed), 50); } #[test] @@ -328,11 +309,9 @@ mod tests { Counter::default_log_rate(), DEFAULT_LOG_RATE, ); - static mut COUNTER: Counter = create_counter!("test_lograte", 0, 1); - unsafe { - COUNTER.init(); - assert_eq!(COUNTER.lograte.load(Ordering::Relaxed), DEFAULT_LOG_RATE); - } + let mut counter = create_counter!("test_lograte", 0, 1); + counter.init(); + assert_eq!(counter.lograte.load(Ordering::Relaxed), DEFAULT_LOG_RATE); } #[test] @@ -341,18 +320,14 @@ mod tests { try_init_logger_at_level_info().ok(); assert_ne!(DEFAULT_LOG_RATE, 0); let _writelock = get_env_lock().write(); - static mut COUNTER: Counter = create_counter!("test_lograte_env", 0, 1); + let mut counter = create_counter!("test_lograte_env", 0, 1); env::set_var("SOLANA_DEFAULT_LOG_RATE", "50"); - unsafe { - COUNTER.init(); - assert_eq!(COUNTER.lograte.load(Ordering::Relaxed), 50); - } + counter.init(); + assert_eq!(counter.lograte.load(Ordering::Relaxed), 50); - static mut COUNTER2: Counter = create_counter!("test_lograte_env", 0, 1); + let mut counter2 = create_counter!("test_lograte_env", 0, 1); env::set_var("SOLANA_DEFAULT_LOG_RATE", "0"); - unsafe { - COUNTER2.init(); - assert_eq!(COUNTER2.lograte.load(Ordering::Relaxed), DEFAULT_LOG_RATE); - } + counter2.init(); + assert_eq!(counter2.lograte.load(Ordering::Relaxed), DEFAULT_LOG_RATE); } }