From 5b4f186a4b4674befad84588c378b84c6ef17dd5 Mon Sep 17 00:00:00 2001 From: Steve Myers Date: Mon, 6 Jan 2025 14:08:43 -0600 Subject: [PATCH] ci(clippy): fix disallow ref to static mut https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html --- src/database/keyvalue.rs | 43 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/database/keyvalue.rs b/src/database/keyvalue.rs index 7c20b2194..6087fa670 100644 --- a/src/database/keyvalue.rs +++ b/src/database/keyvalue.rs @@ -405,12 +405,13 @@ impl BatchDatabase for Tree { #[cfg(test)] mod test { use lazy_static::lazy_static; + use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Condvar, Mutex, Once}; use std::time::{SystemTime, UNIX_EPOCH}; use sled::{Db, Tree}; - static mut COUNT: usize = 0; + static COUNT: AtomicUsize = AtomicUsize::new(0); lazy_static! { static ref DB: Arc<(Mutex>, Condvar)> = @@ -419,33 +420,31 @@ mod test { } fn get_tree() -> Tree { - unsafe { - let cloned = DB.clone(); - let (mutex, cvar) = &*cloned; + let cloned = DB.clone(); + let (mutex, cvar) = &*cloned; - INIT.call_once(|| { - let mut db = mutex.lock().unwrap(); + INIT.call_once(|| { + let mut db = mutex.lock().unwrap(); - let time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); - let mut dir = std::env::temp_dir(); - dir.push(format!("mbw_{}", time.as_nanos())); + let time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); + let mut dir = std::env::temp_dir(); + dir.push(format!("mbw_{}", time.as_nanos())); - *db = Some(sled::open(dir).unwrap()); - cvar.notify_all(); - }); + *db = Some(sled::open(dir).unwrap()); + cvar.notify_all(); + }); - let mut db = mutex.lock().unwrap(); - while !db.is_some() { - db = cvar.wait(db).unwrap(); - } + let mut db = mutex.lock().unwrap(); + while !db.is_some() { + db = cvar.wait(db).unwrap(); + } - COUNT += 1; + COUNT.fetch_add(1, Ordering::Relaxed); - db.as_ref() - .unwrap() - .open_tree(format!("tree_{}", COUNT)) - .unwrap() - } + db.as_ref() + .unwrap() + .open_tree(format!("tree_{}", COUNT.load(Ordering::Relaxed))) + .unwrap() } #[test]