mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
internal: add API to check what are the max limits in practice
This commit is contained in:
parent
a423b307e6
commit
f952dc61d1
3 changed files with 35 additions and 4 deletions
|
@ -1,15 +1,26 @@
|
|||
//! limit defines a struct to enforce limits.
|
||||
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
|
||||
/// Represents a struct used to enforce a numerical limit.
|
||||
pub struct Limit {
|
||||
upper_bound: usize,
|
||||
#[allow(unused)]
|
||||
max: AtomicUsize,
|
||||
}
|
||||
|
||||
impl Limit {
|
||||
/// Creates a new limit.
|
||||
#[inline]
|
||||
pub const fn new(upper_bound: usize) -> Self {
|
||||
Self { upper_bound }
|
||||
Self { upper_bound, max: AtomicUsize::new(0) }
|
||||
}
|
||||
|
||||
/// Creates a new limit.
|
||||
#[inline]
|
||||
#[cfg(feature = "tracking")]
|
||||
pub const fn new_tracking(upper_bound: usize) -> Self {
|
||||
Self { upper_bound, max: AtomicUsize::new(1) }
|
||||
}
|
||||
|
||||
/// Gets the underlying numeric limit.
|
||||
|
@ -21,10 +32,26 @@ impl Limit {
|
|||
/// Checks whether the given value is below the limit.
|
||||
/// Returns `Ok` when `other` is below `self`, and `Err` otherwise.
|
||||
#[inline]
|
||||
pub const fn check(&self, other: usize) -> Result<(), ()> {
|
||||
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(old_max, other, Ordering::Relaxed, Ordering::Relaxed)
|
||||
.is_ok()
|
||||
{
|
||||
eprintln!("new max: {}", other);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue