use std::sync::atomic::AtomicUsize; use std::sync::atomic::Ordering::SeqCst; #[derive(Debug)] pub struct AtomicMin { val: AtomicUsize, } impl AtomicMin { pub fn new(init: Option) -> Self { Self { val: AtomicUsize::new(init.unwrap_or(usize::max_value())), } } pub fn get(&self) -> Option { let val = self.val.load(SeqCst); if val == usize::max_value() { None } else { Some(val) } } /// Unset value is usize_max pub fn as_atomic_usize(&self) -> &AtomicUsize { &self.val } pub fn set_min(&self, new_val: usize) { self.val.fetch_min(new_val, SeqCst); } }