Enum Index

Source
pub enum Index {
    Num(usize),
    Next,
}
Expand description

Represents an abstract index into an array.

If provided an upper bound with Self::for_len or Self::for_len_incl, will produce a concrete numerical index.

Variants§

§

Num(usize)

A non-negative integer value

§

Next

The - token, the position of the next would-be item in the array

Implementations§

Source§

impl Index

Source

pub fn for_len(&self, length: usize) -> Result<usize, OutOfBoundsError>

Bounds the index for a given array length (exclusive).

The upper range is exclusive, so only indices that are less than the given length will be accepted as valid. This ensures that the resolved numerical index can be used to access an existing array element.

Self::Next, by consequence, is always considered invalid, since it resolves to the array length itself.

See also Self::for_len_incl for an alternative if you wish to accept Self::Next (or its numerical equivalent) as valid.

§Examples
assert_eq!(Index::Num(0).for_len(1), Ok(0));
assert!(Index::Num(1).for_len(1).is_err());
assert!(Index::Next.for_len(1).is_err());
§Errors

Returns OutOfBoundsError if the index is out of bounds.

Source

pub fn for_len_incl(&self, length: usize) -> Result<usize, OutOfBoundsError>

Bounds the index for a given array length (inclusive).

The upper range is inclusive, so an index pointing to the position after the last element will be considered valid. Be careful when using the resulting numerical index for accessing an array.

Self::Next is always considered valid.

See also Self::for_len for an alternative if you wish to ensure that the resolved index can be used to access an existing array element.

§Examples
assert_eq!(Index::Num(1).for_len_incl(1), Ok(1));
assert_eq!(Index::Next.for_len_incl(1), Ok(1));
assert!(Index::Num(2).for_len_incl(1).is_err());
§Errors

Returns OutOfBoundsError if the index is out of bounds.

Source

pub fn for_len_unchecked(&self, length: usize) -> usize

Resolves the index for a given array length.

No bound checking will take place. If you wish to ensure the index can be used to access an existing element in the array, use Self::for_len - or use Self::for_len_incl if you wish to accept Self::Next as valid as well.

§Examples
assert_eq!(Index::Num(42).for_len_unchecked(30), 42);
assert_eq!(Index::Next.for_len_unchecked(30), 30);

// no bounds checks
assert_eq!(Index::Num(34).for_len_unchecked(40), 34);
assert_eq!(Index::Next.for_len_unchecked(34), 34);

Trait Implementations§

Source§

impl Clone for Index

Source§

fn clone(&self) -> Index

Returns a duplicate 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 Debug for Index

Source§

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

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

impl Display for Index

Source§

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

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

impl From<usize> for Index

Source§

fn from(value: usize) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Index

Source§

type Err = ParseIndexError

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

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Index

Source§

fn cmp(&self, other: &Index) -> 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,

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

impl PartialEq for Index

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Index

Source§

fn partial_cmp(&self, other: &Index) -> 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

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

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

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl TryFrom<&String> for Index

Source§

type Error = ParseIndexError

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

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

Performs the conversion.
Source§

impl TryFrom<&Token<'_>> for Index

Source§

type Error = ParseIndexError

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

fn try_from(value: &Token<'_>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&str> for Index

Source§

type Error = ParseIndexError

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

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

Performs the conversion.
Source§

impl TryFrom<String> for Index

Source§

type Error = ParseIndexError

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

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

Performs the conversion.
Source§

impl TryFrom<Token<'_>> for Index

Source§

type Error = ParseIndexError

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

fn try_from(value: Token<'_>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Copy for Index

Source§

impl Eq for Index

Source§

impl StructuralPartialEq for Index

Auto Trait Implementations§

§

impl Freeze for Index

§

impl RefUnwindSafe for Index

§

impl Send for Index

§

impl Sync for Index

§

impl Unpin for Index

§

impl UnwindSafe for Index

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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,

Source§

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§

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>,

Source§

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>,

Source§

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.