Skip to main content

PartialRangeExt

Trait PartialRangeExt 

Source
pub trait PartialRangeExt<Idx = usize>: RangeStart<Idx>
where Idx: Clone + Ord,
{ // 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§

Source

fn end_bound(&self) -> Option<RangeEnd<Idx>>

Returns the upper bound, or None if the range is unbounded above.

Source

fn is_empty(&self) -> bool

Returns true if the range contains no indices.

Source

fn clamp_exclusive( &self, start: impl Into<Idx>, exclusive_end: impl Into<Idx>, ) -> Range<Idx>
where Idx: Add<Output = Idx> + One,

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.

Source

fn clamp_inclusive( &self, start: impl Into<Idx>, inclusive_end: impl Into<Idx>, ) -> Range<Idx>

Restricts the range to the window start..=inclusive_end.

The result is always bounded, so it is expressed as a Range. An empty input yields an empty exclusive 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.

Source

fn clamp_right_inclusive(&self, inclusive_end: impl Into<Idx>) -> Range<Idx>

Caps the upper bound at inclusive_end.

Source

fn clamp_right_exclusive(&self, exclusive_end: impl Into<Idx>) -> Range<Idx>
where Idx: Add<Output = Idx> + One,

Caps the upper bound at exclusive_end.

Source

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.

Source

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
where Idx: Clone + Ord, T: PartialRangeExt<Idx>,

Source§

fn end_bound(&self) -> Option<RangeEnd<Idx>>

Source§

fn is_empty(&self) -> bool

Source§

impl<Idx, T> PartialRangeExt<Idx> for &mut T
where Idx: Clone + Ord, T: PartialRangeExt<Idx>,

Source§

fn end_bound(&self) -> Option<RangeEnd<Idx>>

Source§

fn is_empty(&self) -> bool

Source§

impl<Idx, T> PartialRangeExt<Idx> for Option<T>
where Idx: Clone + Ord + Zero, T: PartialRangeExt<Idx>,

None is the empty range, anchored at Idx::zero(), so a possibly-absent range remains usable as a range.

Source§

fn end_bound(&self) -> Option<RangeEnd<Idx>>

Source§

fn is_empty(&self) -> bool

Source§

impl<Idx: Clone + Ord + Zero> PartialRangeExt<Idx> for RangeFull

Source§

impl<Idx: Clone + Ord + Zero> PartialRangeExt<Idx> for RangeTo<Idx>

Source§

fn end_bound(&self) -> Option<RangeEnd<Idx>>

Source§

impl<Idx: Clone + Ord + Zero> PartialRangeExt<Idx> for RangeToInclusive<Idx>

Source§

fn end_bound(&self) -> Option<RangeEnd<Idx>>

Source§

impl<Idx: Clone + Ord> PartialRangeExt<Idx> for Range<Idx>

Source§

fn end_bound(&self) -> Option<RangeEnd<Idx>>

Source§

impl<Idx: Clone + Ord> PartialRangeExt<Idx> for RangeFrom<Idx>

Source§

impl<Idx: Clone + Ord> PartialRangeExt<Idx> for RangeInclusive<Idx>

Source§

fn end_bound(&self) -> Option<RangeEnd<Idx>>

Source§

fn is_empty(&self) -> bool

Implementors§

Source§

impl<Idx: Clone + Ord + Zero> PartialRangeExt<Idx> for PartialRange<Idx>

Source§

impl<Idx: Clone + Ord> PartialRangeExt<Idx> for ps_range::Range<Idx>