#![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
#[cfg(feature = "tracking")]
use std::sync::atomic::AtomicUsize;
#[derive(Debug)]
pub struct Limit {
upper_bound: usize,
#[cfg(feature = "tracking")]
max: AtomicUsize,
}
impl Limit {
#[inline]
pub const fn new(upper_bound: usize) -> Self {
Self {
upper_bound,
#[cfg(feature = "tracking")]
max: AtomicUsize::new(0),
}
}
#[inline]
#[cfg(feature = "tracking")]
pub const fn new_tracking(upper_bound: usize) -> Self {
Self {
upper_bound,
#[cfg(feature = "tracking")]
max: AtomicUsize::new(1),
}
}
#[inline]
pub const fn inner(&self) -> usize {
self.upper_bound
}
#[inline]
pub fn check(&self, other: usize) -> Result<(), ()> {
if other > self.upper_bound {
Err(())
} else {
#[cfg(feature = "tracking")]
loop {
use std::sync::atomic::Ordering;
let old_max = self.max.load(Ordering::Relaxed);
if other <= old_max || old_max == 0 {
break;
}
if self
.max
.compare_exchange_weak(old_max, other, Ordering::Relaxed, Ordering::Relaxed)
.is_ok()
{
eprintln!("new max: {other}");
}
}
Ok(())
}
}
}