Skip to main content

RangeExt

Trait RangeExt 

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

Source

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.

Source

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§

Source

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.

Source

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

Source§

fn end_inclusive(&self) -> Idx

Source§

fn end_exclusive(&self) -> Option<Idx>

Source§

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

Source§

fn end_inclusive(&self) -> Idx

Source§

fn end_exclusive(&self) -> Option<Idx>

Source§

impl<Idx> RangeExt<Idx> for Range<Idx>
where Idx: Clone + Ord + One + SaturatingSub,

Source§

fn end_inclusive(&self) -> Idx

Source§

fn end_exclusive(&self) -> Option<Idx>

Source§

impl<Idx> RangeExt<Idx> for RangeInclusive<Idx>
where Idx: Clone + Ord + One + CheckedAdd,

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.

Source§

fn end_inclusive(&self) -> Idx

Source§

fn end_exclusive(&self) -> Option<Idx>

Source§

impl<Idx> RangeExt<Idx> for RangeTo<Idx>
where Idx: Clone + Ord + Zero + One + SaturatingSub,

Source§

fn end_inclusive(&self) -> Idx

Source§

fn end_exclusive(&self) -> Option<Idx>

Source§

impl<Idx> RangeExt<Idx> for RangeToInclusive<Idx>
where Idx: Clone + Ord + Zero + One + CheckedAdd,

Source§

fn end_inclusive(&self) -> Idx

Source§

fn end_exclusive(&self) -> Option<Idx>

Implementors§

Source§

impl<Idx> RangeExt<Idx> for ps_range::Range<Idx>
where Idx: Clone + Ord + One + CheckedAdd + SaturatingSub,