pub struct Lim<T: PartialOrd + Clone + Debug> { /* private fields */ }Expand description
A strictly typed bounding limit that guarantees a value stays within defined constraints.
Lim encapsulates a value along with its min and max bounds.
Bounds can be configured as inclusive or exclusive. Any mutation
to the value or the bounds is strictly validated to maintain the
mathematical invariants of the range.
Implementations§
Source§impl<T: Clone + PartialOrd + Debug> Lim<T>
impl<T: Clone + PartialOrd + Debug> Lim<T>
Sourcepub fn new(
value: T,
min: T,
max: T,
include_min: bool,
include_max: bool,
) -> Result<Self, LimError<T>>
pub fn new( value: T, min: T, max: T, include_min: bool, include_max: bool, ) -> Result<Self, LimError<T>>
Creates a new Lim instance.
§Arguments
value- The initial value.min- The lower bound.max- The upper bound.include_min- Whether the lower bound is inclusive (>=) or exclusive (>).include_max- Whether the upper bound is inclusive (<=) or exclusive (<).
§Errors
Returns LimError::InvalidBounds if min > max.
Returns LimError::OutOfBounds if value does not satisfy the bounds.
Sourcepub fn include_min(&self) -> bool
pub fn include_min(&self) -> bool
Returns true if the lower bound is inclusive.
Sourcepub fn include_max(&self) -> bool
pub fn include_max(&self) -> bool
Returns true if the upper bound is inclusive.
Sourcepub fn contains(&self, value: &T) -> bool
pub fn contains(&self, value: &T) -> bool
Checks if a given value satisfies the current bounds.
Sourcepub fn set_value(&mut self, value: T) -> Result<(), LimError<T>>
pub fn set_value(&mut self, value: T) -> Result<(), LimError<T>>
Updates the current value.
§Errors
Returns LimError::OutOfBounds if the new value does not satisfy the current bounds.
Sourcepub fn set_min(
&mut self,
min: T,
include_min: bool,
clamp: bool,
) -> Result<(), LimError<T>>
pub fn set_min( &mut self, min: T, include_min: bool, clamp: bool, ) -> Result<(), LimError<T>>
Updates the lower bound.
§Arguments
min- The new lower bound.include_min- Whether the new bound is inclusive or exclusive.clamp- Iftrueand the current value falls outside the new bound, the value will be clamped to the new bound (if mathematically possible). Iffalse, an error is returned if the value is out of bounds.
§Errors
LimError::InvalidBounds: Ifmin > current_max.LimError::OutOfBounds: If the current value is out of bounds andclampis false.LimError::ExclusiveBoundClamp: Ifclampis true, but the new bound is exclusive and the value cannot be safely clamped to it.LimError::AmbiguousClamp: If the current value exactly equals the new exclusive bound.
Sourcepub fn set_max(
&mut self,
max: T,
include_max: bool,
clamp: bool,
) -> Result<(), LimError<T>>
pub fn set_max( &mut self, max: T, include_max: bool, clamp: bool, ) -> Result<(), LimError<T>>
Updates the upper bound.
§Arguments
max- The new upper bound.include_max- Whether the new bound is inclusive or exclusive.clamp- Iftrueand the current value falls outside the new bound, the value will be clamped to the new bound (if mathematically possible). Iffalse, an error is returned if the value is out of bounds.
§Errors
LimError::InvalidBounds: Ifmax < current_min.LimError::OutOfBounds: If the current value is out of bounds andclampis false.LimError::ExclusiveBoundClamp: Ifclampis true, but the new bound is exclusive and the value cannot be safely clamped to it.LimError::AmbiguousClamp: If the current value exactly equals the new exclusive bound.
Sourcepub fn set_bounds(
&mut self,
min: T,
max: T,
include_min: bool,
include_max: bool,
clamp: bool,
) -> Result<(), LimError<T>>
pub fn set_bounds( &mut self, min: T, max: T, include_min: bool, include_max: bool, clamp: bool, ) -> Result<(), LimError<T>>
Updates both the lower and upper bounds simultaneously.
§Arguments
min- The new lower bound.max- The new upper bound.include_min- Whether the new lower bound is inclusive or exclusive.include_max- Whether the new upper bound is inclusive or exclusive.clamp- Iftrue, the current value will be clamped to fit within the new bounds.
§Errors
Returns specific LimError variants if the bounds are invalid, if clamping is
impossible due to exclusive bounds, or if the value is out of bounds and clamp is false.
Trait Implementations§
impl<T: Clone + Copy + PartialOrd + Debug> Copy for Lim<T>
Source§impl<T: PartialOrd + Clone + Debug> Deref for Lim<T>
Allows transparent access to the underlying value.
impl<T: PartialOrd + Clone + Debug> Deref for Lim<T>
Allows transparent access to the underlying value.
Source§impl<'de, T> Deserialize<'de> for Lim<T>
Implements deserialization, strictly validating the bounds and value
invariants during the parsing process.
impl<'de, T> Deserialize<'de> for Lim<T>
Implements deserialization, strictly validating the bounds and value invariants during the parsing process.