Skip to main content

ComposeRange

Trait ComposeRange 

Source
pub trait ComposeRange: RangeBounds<usize> + Debug {
    // Required methods
    fn is_valid(&self, len: usize) -> bool;
    fn compose(&self, base: Range<usize>) -> Range<usize>;
}
Expand description

A range that can check whether it is within the bounds of a slice, and intersect itself with another range.

This trait is implemented for the six Rust range types in core::ops, making it possible to treat them uniformly in implementations, and in particular in procedural macros.

Required Methods§

Source

fn is_valid(&self, len: usize) -> bool

Returns true if the range is within the bounds of a slice of given length

Source

fn compose(&self, base: Range<usize>) -> Range<usize>

Returns a new range that is the composition of base with the range.

The resulting range is guaranteed to be contained in base if self is valid for base.len().

use value_traits::slices::ComposeRange;

assert_eq!((2..5).compose(10..20),  12..15);
assert_eq!((2..=5).compose(10..20), 12..16);
assert_eq!((..5).compose(10..20),   10..15);
assert_eq!((..=5).compose(10..20),  10..16);
assert_eq!((2..).compose(10..20),   12..20);
assert_eq!((..).compose(10..20),    10..20);

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 ComposeRange for Range<usize>

Source§

fn is_valid(&self, len: usize) -> bool

Source§

fn compose(&self, base: Range<usize>) -> Range<usize>

Source§

impl ComposeRange for RangeFrom<usize>

Source§

fn is_valid(&self, len: usize) -> bool

Source§

fn compose(&self, base: Range<usize>) -> Range<usize>

Source§

impl ComposeRange for RangeFull

Source§

fn is_valid(&self, _len: usize) -> bool

Source§

fn compose(&self, base: Range<usize>) -> Range<usize>

Source§

impl ComposeRange for RangeInclusive<usize>

Source§

fn is_valid(&self, len: usize) -> bool

Source§

fn compose(&self, base: Range<usize>) -> Range<usize>

Source§

impl ComposeRange for RangeTo<usize>

Source§

fn is_valid(&self, len: usize) -> bool

Source§

fn compose(&self, base: Range<usize>) -> Range<usize>

Source§

impl ComposeRange for RangeToInclusive<usize>

Source§

fn is_valid(&self, len: usize) -> bool

Source§

fn compose(&self, base: Range<usize>) -> Range<usize>

Implementors§