CheckedIndex

Trait CheckedIndex 

Source
pub trait CheckedIndex<T>:
    From<T>
    + Into<Option<T>>
    + Add<Output = Self>
    + Add<T, Output = Self>
    + Sub<Output = Self>
    + Sub<T, Output = Self>
    + Mul<T, Output = Self>
    + Div<T, Output = Self>
    + Rem<T, Output = Self> {
    // Required methods
    fn invalid() -> Self;
    fn is_valid(&self) -> bool;
    fn unwrap(self) -> T;
    fn expect(self, msg: &str) -> T;
    fn map<F: FnOnce(T) -> T>(self, f: F) -> Self;
    fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U;
    fn into_inner(self) -> T;
}
Expand description

A trait that defines what it means for an index to be a checked index. In other words implementers are indices that are checked for validity in some way.

Required Methods§

Source

fn invalid() -> Self

Construct an invalid index.

Source

fn is_valid(&self) -> bool

Check if the index is valid.

Source

fn unwrap(self) -> T

Convert the index into its underlying integer type. Panic if index is invalid.

Source

fn expect(self, msg: &str) -> T

Convert the index into its underlying integer type. In case of failure, report the given message.

Source

fn map<F: FnOnce(T) -> T>(self, f: F) -> Self

Map the underlying integer type. This allows restricting operations to valid indices only.

Source

fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U

Map the underlying integer type. If the index is invalid, output the provided default.

Source

fn into_inner(self) -> T

Produce the raw inner type without checking.

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.

Implementors§