11//! limit defines a struct to enforce limits.
22
3+ use std:: sync:: atomic:: AtomicUsize ;
4+
35/// Represents a struct used to enforce a numerical limit.
46pub struct Limit {
57 upper_bound : usize ,
8+ #[ allow( unused) ]
9+ max : AtomicUsize ,
610}
711
812impl Limit {
913 /// Creates a new limit.
1014 #[ inline]
1115 pub const fn new ( upper_bound : usize ) -> Self {
12- Self { upper_bound }
16+ Self { upper_bound, max : AtomicUsize :: new ( 0 ) }
17+ }
18+
19+ /// Creates a new limit.
20+ #[ inline]
21+ #[ cfg( feature = "tracking" ) ]
22+ pub const fn new_tracking ( upper_bound : usize ) -> Self {
23+ Self { upper_bound, max : AtomicUsize :: new ( 1 ) }
1324 }
1425
1526 /// Gets the underlying numeric limit.
@@ -21,10 +32,26 @@ impl Limit {
2132 /// Checks whether the given value is below the limit.
2233 /// Returns `Ok` when `other` is below `self`, and `Err` otherwise.
2334 #[ inline]
24- pub const fn check ( & self , other : usize ) -> Result < ( ) , ( ) > {
35+ pub fn check ( & self , other : usize ) -> Result < ( ) , ( ) > {
2536 if other > self . upper_bound {
2637 Err ( ( ) )
2738 } else {
39+ #[ cfg( feature = "tracking" ) ]
40+ loop {
41+ use std:: sync:: atomic:: Ordering ;
42+ let old_max = self . max . load ( Ordering :: Relaxed ) ;
43+ if other <= old_max || old_max == 0 {
44+ break ;
45+ }
46+ if self
47+ . max
48+ . compare_exchange ( old_max, other, Ordering :: Relaxed , Ordering :: Relaxed )
49+ . is_ok ( )
50+ {
51+ eprintln ! ( "new max: {}" , other) ;
52+ }
53+ }
54+
2855 Ok ( ( ) )
2956 }
3057 }
0 commit comments