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:
ix.in_range(min, max)
if and only ifIx::range(min, max).any(|x| x == ix)
- If
ix.in_range(min, max)
, thenIx::range(min, max).nth(ix.index(min, max)) == Some(ix)
Ix::range(min, max).map(|x| x.index(min, max))
yields equal items to0..Ix::range_size(min, max)
Ix::range(min, max).map(|x| x.index_checked(min, max))
ever yieldsNone
if and only ifIx::range_size_checked(min, max).is_none()
Ix::range_size(min, max) == Ix::range(min, max).count()
Ix::range_size_checked(min, max).is_none()
if and only ifIx::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§
Required Methods§
Sourcefn range(min: Self, max: Self) -> Self::Range
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
.
Sourcefn index_checked(self, min: Self, max: Self) -> Option<usize>
fn index_checked(self, min: Self, max: Self) -> Option<usize>
Sourcefn range_size_checked(min: Self, max: Self) -> Option<usize>
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§
Sourcefn index(self, min: Self, max: Self) -> usize
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
.
Sourcefn range_size(min: Self, max: Self) -> usize
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.