SafeIndex

Trait SafeIndex 

Source
pub trait SafeIndex<T> {
    // Required methods
    fn safe_get<'a, I>(
        &'a self,
        index: I,
    ) -> Result<&'a I::Output, ExecutionError>
       where I: SliceIndex<[T]>,
             T: 'a;
    fn safe_get_mut<'a, I>(
        &'a mut self,
        index: I,
    ) -> Result<&'a mut I::Output, ExecutionError>
       where I: SliceIndex<[T]>,
             T: 'a;
}
Expand description

A trait for safe indexing into collections that returns a ExecutionError as long as the collection implements AsRef<[T]>. This is useful for avoiding panics on out-of-bounds access, and instead returning a proper error.

Required Methods§

Source

fn safe_get<'a, I>(&'a self, index: I) -> Result<&'a I::Output, ExecutionError>
where I: SliceIndex<[T]>, T: 'a,

Get a reference to the element at the given index, or return invariant violation error if the index is out of bounds.

Source

fn safe_get_mut<'a, I>( &'a mut self, index: I, ) -> Result<&'a mut I::Output, ExecutionError>
where I: SliceIndex<[T]>, T: 'a,

Get a mutable reference to the element at the given index, or return invariant violation error if the index is out of bounds.

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§

Source§

impl<T, C> SafeIndex<T> for C
where C: AsRef<[T]> + AsMut<[T]>,