Struct andex::Andex

source ·
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>

Andex-wide methods

Andex::new and Andex::iter are public, most other methods are only used in traits, and thus private.

source

pub const SIZE: usize = SIZE

The SIZE parameter, which is the size of the array that this andex indexes.

source

pub const FIRST: Andex<M, SIZE> = _

The first possible value.

source

pub const LAST: Andex<M, SIZE> = _

The last possible value.

source

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>();
source

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.

source

pub fn next(self) -> Option<Self>

Return the next Andex in sequence, or None if it’s the last one.

source

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<M, const SIZE: usize> Clone for Andex<M, SIZE>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<M, const SIZE: usize> Debug for Andex<M, SIZE>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<M, const SIZE: usize> Default for Andex<M, SIZE>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<M, const SIZE: usize> Display for Andex<M, SIZE>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<M, const SIZE: usize> From<&Andex<M, SIZE>> for usize

source§

fn from(andex: &Andex<M, SIZE>) -> Self

Converts to this type from the input type.
source§

impl<M, const SIZE: usize> From<Andex<M, SIZE>> for usize

source§

fn from(andex: Andex<M, SIZE>) -> Self

Converts to this type from the input type.
source§

impl<M, const SIZE: usize> FromStr for Andex<M, SIZE>

§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl<A, Item, const SIZE: usize> Index<&Andex<A, SIZE>> for AndexableArray<Andex<A, SIZE>, Item, SIZE>

§

type Output = Item

The returned type after indexing.
source§

fn index(&self, index: &Andex<A, SIZE>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<A, Item, const SIZE: usize> Index<Andex<A, SIZE>> for AndexableArray<Andex<A, SIZE>, Item, SIZE>

§

type Output = Item

The returned type after indexing.
source§

fn index(&self, index: Andex<A, SIZE>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<A, Item, const SIZE: usize> IndexMut<&Andex<A, SIZE>> for AndexableArray<Andex<A, SIZE>, Item, SIZE>

source§

fn index_mut(&mut self, index: &Andex<A, SIZE>) -> &mut Item

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<A, Item, const SIZE: usize> IndexMut<Andex<A, SIZE>> for AndexableArray<Andex<A, SIZE>, Item, SIZE>

source§

fn index_mut(&mut self, index: Andex<A, SIZE>) -> &mut Item

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<M, const SIZE: usize> Ord for Andex<M, SIZE>

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<M, const SIZE: usize> PartialEq for Andex<M, SIZE>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<M, const SIZE: usize> PartialOrd for Andex<M, SIZE>

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<M, const SIZE: usize> TryFrom<usize> for Andex<M, SIZE>

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: usize) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<M, const SIZE: usize> Copy for Andex<M, SIZE>

source§

impl<M, const SIZE: usize> Eq for Andex<M, SIZE>

Auto Trait Implementations§

§

impl<M, const SIZE: usize> Freeze for Andex<M, SIZE>

§

impl<M, const SIZE: usize> RefUnwindSafe for Andex<M, SIZE>
where M: RefUnwindSafe,

§

impl<M, const SIZE: usize> Send for Andex<M, SIZE>
where M: Send,

§

impl<M, const SIZE: usize> Sync for Andex<M, SIZE>
where M: Sync,

§

impl<M, const SIZE: usize> Unpin for Andex<M, SIZE>
where M: Unpin,

§

impl<M, const SIZE: usize> UnwindSafe for Andex<M, SIZE>
where M: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.