pub trait Index:
Debug
+ Copy
+ PartialEq {
type Size: Size;
// Required methods
fn length(size: Self::Size) -> usize;
fn to_usize(self, size: Self::Size) -> usize;
fn from_usize(size: Self::Size, index: usize) -> (usize, Self);
// Provided methods
fn each(size: Self::Size, f: impl FnMut(Self)) { ... }
fn all(size: impl Isomorphic<Self::Size>) -> All<Self> { ... }
}Expand description
Implemented by types that can be used as an index for an Array.
You are encouraged to write new Index types. If Index::Size
is a compile-time constant, you can save some effort by implementing
StaticIndex instead.
Types that implement Index should implement Isomorphic. The simplest
and best way to achieve this for a non-tuple type is to implement
NonTuple.
Required Associated Types§
Sourcetype Size: Size
type Size: Size
The run-time representation of the size of an Array<Self, T>.
If the size is a compile-time constant, this will implement
Isomorphic<()>.
Required Methods§
Sourcefn to_usize(self, size: Self::Size) -> usize
fn to_usize(self, size: Self::Size) -> usize
Returns the index (in 0..length()) of Self.
Panics if Self is not a valid index into an Array of size size.
Sourcefn from_usize(size: Self::Size, index: usize) -> (usize, Self)
fn from_usize(size: Self::Size, index: usize) -> (usize, Self)
Returns index / self.length(size) and the Self for which
to_usize() returns index % self.length(size).
Provided Methods§
Sourcefn each(size: Self::Size, f: impl FnMut(Self))
fn each(size: Self::Size, f: impl FnMut(Self))
Equivalent to, but often more efficient than,
for i in 0..Self::length(size) { f(Self::from_usize(size, i).1); }Sourcefn all(size: impl Isomorphic<Self::Size>) -> All<Self>
fn all(size: impl Isomorphic<Self::Size>) -> All<Self>
Returns a View of the specified size that maps every Self to itself.
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<I: Index> Index for Option<I>
impl<I: Index> Index for Option<I>
Source§fn each(size: Self::Size, f: impl FnMut(Self))
fn each(size: Self::Size, f: impl FnMut(Self))
Equivalent to, but often more efficient than,
for i in 0..Self::length(size) { f(Self::from_usize(size, i).1); }