oxipng/src/atomicmin.rs
Ingvar Stepanyan 7167a5e44d
Use the new stabilised fetch_min (#328)
A minor simplification using the corresponding new standard library function.

Bumps minimum Rust version to 1.45.0
2020-10-11 18:53:00 -04:00

33 lines
710 B
Rust

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<usize>) -> Self {
Self {
val: AtomicUsize::new(init.unwrap_or(usize::max_value())),
}
}
pub fn get(&self) -> Option<usize> {
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);
}
}