Trait Ix

Source
pub trait Ix: PartialOrd + Sized {
    type Range: Iterator<Item = Self>;

    // Required methods
    fn range(min: Self, max: Self) -> Self::Range;
    fn index_checked(self, min: Self, max: Self) -> Option<usize>;
    fn in_range(self, min: Self, max: Self) -> bool;
    fn range_size_checked(min: Self, max: Self) -> Option<usize>;

    // Provided methods
    fn index(self, min: Self, max: Self) -> usize { ... }
    fn range_size(min: Self, max: Self) -> usize { ... }
}
Expand description

A trait for values that permit contiguous subranges.

Implementations that override the provided functions must ensure their custom implementations are equivalent to the provided ones.

Implementations must uphold the following invariants:

  1. ix.in_range(min, max) if and only if Ix::range(min, max).any(|x| x == ix)
  2. If ix.in_range(min, max), then Ix::range(min, max).nth(ix.index(min, max)) == Some(ix)
  3. Ix::range(min, max).map(|x| x.index(min, max)) yields equal items to 0..Ix::range_size(min, max)
  4. Ix::range(min, max).map(|x| x.index_checked(min, max)) ever yields None if and only if Ix::range_size_checked(min, max).is_none()
  5. Ix::range_size(min, max) == Ix::range(min, max).count()
  6. Ix::range_size_checked(min, max).is_none() if and only if Ix::range(min, max).count() overflows or panics

Note that, for these properties, if one side of the equality panics or overflows the equality can be considered to hold.

§Examples

for (ix, i) in Ix::range(-45i128, 483).zip(0..) {
    assert_eq!(ix.index(-45, 483), i);
}
assert!(!(-30289i16).in_range(-746, 15564));
assert_eq!(
    2410117514u32.in_range(2073922791, 3401563124),
    Ix::range(2073922791u32, 3401563124).any(|x| x == 2410117514)
); // Property 1
assert!(20i32.in_range(17, 5432));
assert_eq!(Ix::range(17i32, 5432).nth(20i32.index(17, 5432)).unwrap(), 20);
// Property 2
assert!(0.in_range(-31597i16, 16417));
assert_eq!(Ix::range(-31597i16, 16417).nth(0.index(-31597i16, 16417)).unwrap(), 0);
// Property 2
assert!(
    Ix::range(-633i32, 151)
        .map(|x| x.index(-633, 151))
        .eq(0..Ix::range_size(-633, 151))
) // Property 3
assert_eq!(Ix::range(8079u32, 1836091).count(), Ix::range_size(8079u32, 1836091))
// Property 5

Required Associated Types§

Source

type Range: Iterator<Item = Self>

An iterator over the elements in a range of the implementing type.

Required Methods§

Source

fn range(min: Self, max: Self) -> Self::Range

Generate an iterator over a range starting from min and stopping at max. The resulting iterator must produce min and max at some point, each.

§Panics

Should panic if min is greater than max.

Source

fn index_checked(self, min: Self, max: Self) -> Option<usize>

Get the position of a value inside a range. If this would overflow the range of usize, returns None. Checked version of index.

§Panics

Should panic if min is greater than max.

Should panic if the value is not in the range (as determined by in_range).

Source

fn in_range(self, min: Self, max: Self) -> bool

Check if a given value is inside a range.

§Panics

Should panic if min is greater than max.

Source

fn range_size_checked(min: Self, max: Self) -> Option<usize>

Get the length of a range. If this would overflow the range of usize, returns None. Checked version of range_size.

§Panics

Should panic if min is greater than max.

Provided Methods§

Source

fn index(self, min: Self, max: Self) -> usize

Get the position of a value inside a range.

§Panics

Should panic if min is greater than max.

Should panic if the value is not in the range (as determined by in_range).

Panics if the resulting index is not representable as a usize value. The default implementation does this by unwrapping the return value of index_checked.

Source

fn range_size(min: Self, max: Self) -> usize

Get the length of a range.

§Panics

Should panic if min is greater than max.

Panics if the resulting size is not representable as a usize value. The default implementation does this by unwrapping the return value of range_size_checked.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Ix for i8

Source§

type Range = RangeInclusive<i8>

Source§

fn range(min: Self, max: Self) -> Self::Range

Source§

fn index_checked(self, min: Self, max: Self) -> Option<usize>

Source§

fn in_range(self, min: Self, max: Self) -> bool

Source§

fn range_size_checked(min: Self, max: Self) -> Option<usize>

Source§

impl Ix for i16

Source§

type Range = RangeInclusive<i16>

Source§

fn range(min: Self, max: Self) -> Self::Range

Source§

fn index_checked(self, min: Self, max: Self) -> Option<usize>

Source§

fn in_range(self, min: Self, max: Self) -> bool

Source§

fn range_size_checked(min: Self, max: Self) -> Option<usize>

Source§

impl Ix for i32

Source§

type Range = RangeInclusive<i32>

Source§

fn range(min: Self, max: Self) -> Self::Range

Source§

fn index_checked(self, min: Self, max: Self) -> Option<usize>

Source§

fn in_range(self, min: Self, max: Self) -> bool

Source§

fn range_size_checked(min: Self, max: Self) -> Option<usize>

Source§

impl Ix for i64

Source§

type Range = RangeInclusive<i64>

Source§

fn range(min: Self, max: Self) -> Self::Range

Source§

fn index_checked(self, min: Self, max: Self) -> Option<usize>

Source§

fn in_range(self, min: Self, max: Self) -> bool

Source§

fn range_size_checked(min: Self, max: Self) -> Option<usize>

Source§

impl Ix for i128

Source§

type Range = RangeInclusive<i128>

Source§

fn range(min: Self, max: Self) -> Self::Range

Source§

fn index_checked(self, min: Self, max: Self) -> Option<usize>

Source§

fn in_range(self, min: Self, max: Self) -> bool

Source§

fn range_size_checked(min: Self, max: Self) -> Option<usize>

Source§

impl Ix for isize

Source§

type Range = RangeInclusive<isize>

Source§

fn range(min: Self, max: Self) -> Self::Range

Source§

fn index_checked(self, min: Self, max: Self) -> Option<usize>

Source§

fn in_range(self, min: Self, max: Self) -> bool

Source§

fn range_size_checked(min: Self, max: Self) -> Option<usize>

Source§

impl Ix for u8

Source§

type Range = RangeInclusive<u8>

Source§

fn range(min: Self, max: Self) -> Self::Range

Source§

fn index_checked(self, min: Self, max: Self) -> Option<usize>

Source§

fn in_range(self, min: Self, max: Self) -> bool

Source§

fn range_size_checked(min: Self, max: Self) -> Option<usize>

Source§

impl Ix for u16

Source§

type Range = RangeInclusive<u16>

Source§

fn range(min: Self, max: Self) -> Self::Range

Source§

fn index_checked(self, min: Self, max: Self) -> Option<usize>

Source§

fn in_range(self, min: Self, max: Self) -> bool

Source§

fn range_size_checked(min: Self, max: Self) -> Option<usize>

Source§

impl Ix for u32

Source§

type Range = RangeInclusive<u32>

Source§

fn range(min: Self, max: Self) -> Self::Range

Source§

fn index_checked(self, min: Self, max: Self) -> Option<usize>

Source§

fn in_range(self, min: Self, max: Self) -> bool

Source§

fn range_size_checked(min: Self, max: Self) -> Option<usize>

Source§

impl Ix for u64

Source§

type Range = RangeInclusive<u64>

Source§

fn range(min: Self, max: Self) -> Self::Range

Source§

fn index_checked(self, min: Self, max: Self) -> Option<usize>

Source§

fn in_range(self, min: Self, max: Self) -> bool

Source§

fn range_size_checked(min: Self, max: Self) -> Option<usize>

Source§

impl Ix for u128

Source§

type Range = RangeInclusive<u128>

Source§

fn range(min: Self, max: Self) -> Self::Range

Source§

fn index_checked(self, min: Self, max: Self) -> Option<usize>

Source§

fn in_range(self, min: Self, max: Self) -> bool

Source§

fn range_size_checked(min: Self, max: Self) -> Option<usize>

Source§

impl Ix for usize

Source§

type Range = RangeInclusive<usize>

Source§

fn range(min: Self, max: Self) -> Self::Range

Source§

fn index_checked(self, min: Self, max: Self) -> Option<usize>

Source§

fn in_range(self, min: Self, max: Self) -> bool

Source§

fn range_size_checked(min: Self, max: Self) -> Option<usize>

Implementors§