1 //! limit defines a struct to enforce limits.
3 use std::sync::atomic::AtomicUsize;
5 /// Represents a struct used to enforce a numerical limit.
13 /// Creates a new limit.
15 pub const fn new(upper_bound: usize) -> Self {
16 Self { upper_bound, max: AtomicUsize::new(0) }
19 /// Creates a new limit.
21 #[cfg(feature = "tracking")]
22 pub const fn new_tracking(upper_bound: usize) -> Self {
23 Self { upper_bound, max: AtomicUsize::new(1) }
26 /// Gets the underlying numeric limit.
28 pub const fn inner(&self) -> usize {
32 /// Checks whether the given value is below the limit.
33 /// Returns `Ok` when `other` is below `self`, and `Err` otherwise.
35 pub fn check(&self, other: usize) -> Result<(), ()> {
36 if other > self.upper_bound {
39 #[cfg(feature = "tracking")]
41 use std::sync::atomic::Ordering;
42 let old_max = self.max.load(Ordering::Relaxed);
43 if other <= old_max || old_max == 0 {
48 .compare_exchange_weak(old_max, other, Ordering::Relaxed, Ordering::Relaxed)
51 eprintln!("new max: {}", other);