pub trait PartialRangeExt<Idx = usize>: RangeStart<Idx>{
// Provided methods
fn end_bound(&self) -> Option<RangeEnd<Idx>> { ... }
fn is_empty(&self) -> bool { ... }
fn clamp_exclusive(
&self,
start: impl Into<Idx>,
exclusive_end: impl Into<Idx>,
) -> Range<Idx>
where Idx: Add<Output = Idx> + One { ... }
fn clamp_inclusive(
&self,
start: impl Into<Idx>,
inclusive_end: impl Into<Idx>,
) -> Range<Idx> { ... }
fn clamp_right_inclusive(&self, inclusive_end: impl Into<Idx>) -> Range<Idx> { ... }
fn clamp_right_exclusive(&self, exclusive_end: impl Into<Idx>) -> Range<Idx>
where Idx: Add<Output = Idx> + One { ... }
fn clamp_left(&self, start: impl Into<Idx>) -> PartialRange<Idx> ⓘ { ... }
fn intersection<R>(&self, other: &R) -> PartialRange<Idx> ⓘ
where R: PartialRangeExt<Idx> { ... }
}Expand description
A range that may be unbounded above or empty.
Implementors provide start via the
RangeStart supertrait and override
end_bound when the range is bounded; the
default returns None, describing a range unbounded above. An
implementor whose emptiness is not derivable from those two accessors,
such as a type with hidden exhaustion state like
std::ops::RangeInclusive, must also override
is_empty; every derived operation consults
is_empty before the bounds. The standard
library leaves the endpoint values of a drained
std::ops::RangeInclusive unspecified, so the position of an empty
result derived from one is likewise unspecified; only emptiness is
guaranteed.
The operations interpret Idx as a discrete domain in which x + one is
the successor of x, and convert a bound between its inclusive and
exclusive forms only where the converted bound is provably at most the
window’s end. Empty results from the PartialRange-returning
operations anchor at the raised or joint start; the
std::ops::Range- and Range-returning operations instead lower the
start to the computed end, so their results are always slice-safe.
§Examples
use ps_range::{PartialRange, PartialRangeExt, Range};
assert_eq!((5usize..=10).clamp_exclusive(0usize, 7usize), 5..7);
assert_eq!((5usize..).clamp_inclusive(0usize, 7usize), Range::inclusive(5, 7));
assert!((5usize..=10).clamp_exclusive(20usize, 25usize).is_empty());
assert_eq!(
(5usize..=10).intersection(&(8usize..)),
PartialRange::Inclusive { start: 8, end: 10 }
);Provided Methods§
Sourcefn end_bound(&self) -> Option<RangeEnd<Idx>>
fn end_bound(&self) -> Option<RangeEnd<Idx>>
Returns the upper bound, or None if the range is unbounded above.
Sourcefn clamp_exclusive(
&self,
start: impl Into<Idx>,
exclusive_end: impl Into<Idx>,
) -> Range<Idx>
fn clamp_exclusive( &self, start: impl Into<Idx>, exclusive_end: impl Into<Idx>, ) -> Range<Idx>
Restricts the range to the window start..exclusive_end.
The result is slice-safe: its start does not exceed its end, and its end does not exceed the window’s. An empty input yields an empty range at its start clamped into the window; a disjoint input yields one at the computed end, which may fall below the window’s start.
Sourcefn clamp_inclusive(
&self,
start: impl Into<Idx>,
inclusive_end: impl Into<Idx>,
) -> Range<Idx>
fn clamp_inclusive( &self, start: impl Into<Idx>, inclusive_end: impl Into<Idx>, ) -> Range<Idx>
Sourcefn clamp_right_inclusive(&self, inclusive_end: impl Into<Idx>) -> Range<Idx>
fn clamp_right_inclusive(&self, inclusive_end: impl Into<Idx>) -> Range<Idx>
Caps the upper bound at inclusive_end.
Sourcefn clamp_right_exclusive(&self, exclusive_end: impl Into<Idx>) -> Range<Idx>
fn clamp_right_exclusive(&self, exclusive_end: impl Into<Idx>) -> Range<Idx>
Caps the upper bound at exclusive_end.
Sourcefn clamp_left(&self, start: impl Into<Idx>) -> PartialRange<Idx> ⓘ
fn clamp_left(&self, start: impl Into<Idx>) -> PartialRange<Idx> ⓘ
Raises the lower bound to at least start, preserving boundedness
above.
If the range is empty, or start is disjoint from it, the result is
the Empty variant anchored at the raised
start.
Sourcefn intersection<R>(&self, other: &R) -> PartialRange<Idx> ⓘwhere
R: PartialRangeExt<Idx>,
fn intersection<R>(&self, other: &R) -> PartialRange<Idx> ⓘwhere
R: PartialRangeExt<Idx>,
Returns the overlap between this range and other.
The result is unbounded above only when both ranges are. If either
range is empty, or the ranges are disjoint, the result is the
Empty variant anchored at the joint start.
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> PartialRangeExt<Idx> for &T
impl<Idx, T> PartialRangeExt<Idx> for &T
Source§impl<Idx, T> PartialRangeExt<Idx> for &mut T
impl<Idx, T> PartialRangeExt<Idx> for &mut T
Source§impl<Idx, T> PartialRangeExt<Idx> for Option<T>
None is the empty range, anchored at Idx::zero(), so
a possibly-absent range remains usable as a range.
impl<Idx, T> PartialRangeExt<Idx> for Option<T>
None is the empty range, anchored at Idx::zero(), so
a possibly-absent range remains usable as a range.