Index

Trait Index 

Source
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§

Source

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§

Source

fn length(size: Self::Size) -> usize

Returns the number of Ts in an Array<Self, T>.

Source

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.

Source

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§

Source

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); }
Source

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 Index for usize

Source§

type Size = usize

Source§

fn length(size: Self::Size) -> usize

Source§

fn to_usize(self, size: Self::Size) -> usize

Source§

fn from_usize(size: Self::Size, index: usize) -> (usize, Self)

Source§

fn each(size: Self::Size, f: impl FnMut(Self))

Source§

impl<I: Index> Index for Option<I>

Source§

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); }
Source§

type Size = <I as Index>::Size

Source§

fn length(size: Self::Size) -> usize

Source§

fn to_usize(self, size: Self::Size) -> usize

Source§

fn from_usize(size: Self::Size, index: usize) -> (usize, Self)

Source§

impl<I: Index> Index for (I,)

Source§

type Size = (<I as Index>::Size,)

Source§

fn length(size: Self::Size) -> usize

Source§

fn to_usize(self, size: Self::Size) -> usize

Source§

fn from_usize(size: Self::Size, index: usize) -> (usize, Self)

Source§

fn each(size: Self::Size, f: impl FnMut(Self))

Source§

impl<I: Index, J: Index> Index for (I, J)

Source§

type Size = (<I as Index>::Size, <J as Index>::Size)

Source§

fn length(size: Self::Size) -> usize

Source§

fn to_usize(self, size: Self::Size) -> usize

Source§

fn from_usize(size: Self::Size, index: usize) -> (usize, Self)

Source§

fn each(size: Self::Size, f: impl FnMut(Self))

Source§

impl<I: Index, J: Index, K: Index> Index for (I, J, K)

Source§

type Size = (<I as Index>::Size, <J as Index>::Size, <K as Index>::Size)

Source§

fn length(size: Self::Size) -> usize

Source§

fn to_usize(self, size: Self::Size) -> usize

Source§

fn from_usize(size: Self::Size, index: usize) -> (usize, Self)

Source§

fn each(size: Self::Size, f: impl FnMut(Self))

Implementors§

Source§

impl Index for Reversed

Source§

impl<I: Index> Index for Coated<I>

Source§

type Size = Coated<<I as Index>::Size>

Source§

impl<I: StaticIndex> Index for I

Source§

impl<const SIZE: usize> Index for Fixed<SIZE>