pub struct Andex<M, const SIZE: usize>(/* private fields */);Expand description
Array index generic type
This generic type receives a user-specified “marker” type as the
first type parameter to make it unique, and the size of the array
as a second const generic SIZE parameter.
Note: the maximum numerical value in the andex is SIZE - 1.
Recommended usage, with an empty type as a marker to create a type alias:
use andex::*;
enum MyIdxMarker {}
type MyIdx = Andex<MyIdxMarker, 12>;Implementations§
source§impl<M, const SIZE: usize> Andex<M, SIZE>
impl<M, const SIZE: usize> Andex<M, SIZE>
Andex-wide methods
Andex::new and Andex::iter are public, most other methods
are only used in traits, and thus private.
sourcepub const SIZE: usize = SIZE
pub const SIZE: usize = SIZE
The SIZE parameter, which is the size of the array that this
andex indexes.
sourcepub const fn new<const N: usize>() -> Self
pub const fn new<const N: usize>() -> Self
Create a new andex instance
We recomment using this method in const contexts, passing
the index as a const generic function parameter. That allows
the compiler to check the index against the array bounds at
compile time.
For instance, the following compiles:
use andex::*;
struct MyIdxMarker;
type MyIdx = Andex<MyIdxMarker, 12>;
const MYVALUE : MyIdx = MyIdx::new::<0>();While the following doesn’t:
use andex::*;
struct MyIdxMarker;
type MyIdx = Andex<MyIdxMarker, 13>;
const MYVALUE : MyIdx = MyIdx::new::<15>();sourcepub const fn pair(self) -> Self
pub const fn pair(self) -> Self
Returns the pair of the provided Andex.
The “pair” is the element that is at the same distance from
the center. This definition is useful in some contexts. For
instance, the pair of Self::FIRST is Self::LAST.
sourcepub fn next(self) -> Option<Self>
pub fn next(self) -> Option<Self>
Return the next Andex in sequence, or None if it’s the last one.
sourcepub fn iter() -> AndexIterator<M, SIZE> ⓘ
pub fn iter() -> AndexIterator<M, SIZE> ⓘ
Iterate all possible values of the index
Useful to loop over an array inside a struct, without
holding a reference to the whole struct in the loop.
§Example
This prints all numbers from 0 to 11:
use andex::*;
pub struct PlayerIdMarker;
type PlayerId = Andex<PlayerIdMarker, 12>;
for i in PlayerId::iter() {
println!("{}", i);
}Trait Implementations§
source§impl<A, Item, const SIZE: usize> Index<&Andex<A, SIZE>> for AndexableArray<Andex<A, SIZE>, Item, SIZE>
impl<A, Item, const SIZE: usize> Index<&Andex<A, SIZE>> for AndexableArray<Andex<A, SIZE>, Item, SIZE>
source§impl<A, Item, const SIZE: usize> Index<Andex<A, SIZE>> for AndexableArray<Andex<A, SIZE>, Item, SIZE>
impl<A, Item, const SIZE: usize> Index<Andex<A, SIZE>> for AndexableArray<Andex<A, SIZE>, Item, SIZE>
source§impl<A, Item, const SIZE: usize> IndexMut<&Andex<A, SIZE>> for AndexableArray<Andex<A, SIZE>, Item, SIZE>
impl<A, Item, const SIZE: usize> IndexMut<&Andex<A, SIZE>> for AndexableArray<Andex<A, SIZE>, Item, SIZE>
source§impl<A, Item, const SIZE: usize> IndexMut<Andex<A, SIZE>> for AndexableArray<Andex<A, SIZE>, Item, SIZE>
impl<A, Item, const SIZE: usize> IndexMut<Andex<A, SIZE>> for AndexableArray<Andex<A, SIZE>, Item, SIZE>
source§impl<M, const SIZE: usize> Ord for Andex<M, SIZE>
impl<M, const SIZE: usize> Ord for Andex<M, SIZE>
source§impl<M, const SIZE: usize> PartialEq for Andex<M, SIZE>
impl<M, const SIZE: usize> PartialEq for Andex<M, SIZE>
source§impl<M, const SIZE: usize> PartialOrd for Andex<M, SIZE>
impl<M, const SIZE: usize> PartialOrd for Andex<M, SIZE>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read more