pub trait RangeExt<Idx = usize>: RangeStart<Idx>{
// Required methods
fn end_inclusive(&self) -> Idx;
fn end_exclusive(&self) -> Option<Idx>;
// Provided methods
fn clamp_to(&self, start: impl Into<Idx>, end: impl Into<Idx>) -> Range<Idx> { ... }
fn clamp_right(&self, end: impl Into<Idx>) -> Range<Idx> { ... }
}Expand description
A range bounded on both ends.
The upper bound is exposed in both of its forms:
end_exclusive returns None exactly when
the exclusive bound would exceed the maximum index value, that is, when
the range ends inclusively at the maximum, and
end_inclusive is meaningful only for a
non-empty range, so (0..0).end_inclusive() is 0. The clamping
operations consult only end_exclusive and
compare against bounds that are at most the maximum index value, so no
operation converts a bound it cannot represent.
§Examples
use ps_range::RangeExt;
assert_eq!((..8).clamp_right(6usize), 0..6);
assert_eq!((2..=8usize).end_exclusive(), Some(9));
assert_eq!((2..=u8::MAX).end_exclusive(), None);
assert_eq!((0..=u8::MAX).clamp_to(10, 200), 10..200);Required Methods§
Sourcefn end_inclusive(&self) -> Idx
fn end_inclusive(&self) -> Idx
Returns the inclusive upper bound.
The returned bound is meaningful only for a non-empty range: an empty
range has no last index, so (0..0).end_inclusive() returns 0.
Sourcefn end_exclusive(&self) -> Option<Idx>
fn end_exclusive(&self) -> Option<Idx>
Returns the exclusive upper bound, or None if it would exceed the
maximum index value, that is, if the range ends inclusively at the
maximum.
Provided Methods§
Sourcefn clamp_to(&self, start: impl Into<Idx>, end: impl Into<Idx>) -> Range<Idx>
fn clamp_to(&self, start: impl Into<Idx>, end: impl Into<Idx>) -> Range<Idx>
Restricts the range to the bounds start..end.
The result is slice-safe: its start does not exceed its end, and a window disjoint from the range yields an empty range anchored at the computed end.
Sourcefn clamp_right(&self, end: impl Into<Idx>) -> Range<Idx>
fn clamp_right(&self, end: impl Into<Idx>) -> Range<Idx>
Lowers the upper bound to at most end.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl<Idx, T> RangeExt<Idx> for &T
impl<Idx, T> RangeExt<Idx> for &T
fn end_inclusive(&self) -> Idx
fn end_exclusive(&self) -> Option<Idx>
Source§impl<Idx, T> RangeExt<Idx> for &mut T
impl<Idx, T> RangeExt<Idx> for &mut T
fn end_inclusive(&self) -> Idx
fn end_exclusive(&self) -> Option<Idx>
Source§impl<Idx> RangeExt<Idx> for Range<Idx>
impl<Idx> RangeExt<Idx> for Range<Idx>
fn end_inclusive(&self) -> Idx
fn end_exclusive(&self) -> Option<Idx>
Source§impl<Idx> RangeExt<Idx> for RangeInclusive<Idx>
The accessors collapse a drained range, whose endpoint values the
standard library leaves unspecified, to an empty range at its start, so
the clamping operations cannot resurrect it.
impl<Idx> RangeExt<Idx> for RangeInclusive<Idx>
The accessors collapse a drained range, whose endpoint values the standard library leaves unspecified, to an empty range at its start, so the clamping operations cannot resurrect it.