mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 20:42:04 +00:00
parent
fe00358888
commit
0b3d0cde8b
16 changed files with 84 additions and 24 deletions
31
crates/limit/src/lib.rs
Normal file
31
crates/limit/src/lib.rs
Normal file
|
@ -0,0 +1,31 @@
|
|||
//! limit defines a struct to enforce limits.
|
||||
|
||||
/// Represents a struct used to enforce a numerical limit.
|
||||
pub struct Limit {
|
||||
upper_bound: usize,
|
||||
}
|
||||
|
||||
impl Limit {
|
||||
/// Creates a new limit.
|
||||
#[inline]
|
||||
pub const fn new(upper_bound: usize) -> Self {
|
||||
Self { upper_bound }
|
||||
}
|
||||
|
||||
/// Gets the underlying numeric limit.
|
||||
#[inline]
|
||||
pub const fn inner(&self) -> usize {
|
||||
self.upper_bound
|
||||
}
|
||||
|
||||
/// 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<(), ()> {
|
||||
if other > self.upper_bound {
|
||||
Err(())
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue