Trait SubArray

Source
pub trait SubArray {
    type Item;

    // Required methods
    fn sub_array_ref<const N: usize>(&self, offset: usize) -> &[Self::Item; N];
    fn sub_array_mut<const N: usize>(
        &mut self,
        offset: usize,
    ) -> &mut [Self::Item; N];
}
Expand description

Array that can be slice into a smaller sub-array

Also see the crate level reference.

Required Associated Types§

Source

type Item

The value type of this array.

This is the T in [T; N] on regular arrays.

Required Methods§

Source

fn sub_array_ref<const N: usize>(&self, offset: usize) -> &[Self::Item; N]

Get a reference to a sub-array of length N starting at offset.

§Panics

Panics if offset + N exceeds the length of this array.

§Example
use sub_array::SubArray;

let arr: [u8; 5] = [9, 8, 7, 6, 5];

// Get a sub-array starting at offset 3
let sub: &[u8; 2] = arr.sub_array_ref(3);
assert_eq!(sub, &[6, 5]);
Source

fn sub_array_mut<const N: usize>( &mut self, offset: usize, ) -> &mut [Self::Item; N]

Get a mutable reference to a sub-array of length N starting at offset.

§Panics

Panics if offset + N exceeds the length of this array.

§Example
use sub_array::SubArray;

let mut arr: [u8; 5] = [9, 8, 7, 6, 5];

// Get a mutable sub-array starting at offset 0
let sub: &mut [u8; 2] = arr.sub_array_mut(0);
assert_eq!(sub, &mut [9, 8]);

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.

Implementations on Foreign Types§

Source§

impl<T> SubArray for &mut T
where T: SubArray,

Implementation on mutable references

Source§

type Item = <T as SubArray>::Item

Source§

fn sub_array_ref<const N: usize>(&self, offset: usize) -> &[Self::Item; N]

Source§

fn sub_array_mut<const N: usize>( &mut self, offset: usize, ) -> &mut [Self::Item; N]

Source§

impl<T> SubArray for [T]

Implementation on slices

Source§

type Item = T

Source§

fn sub_array_ref<const N: usize>(&self, offset: usize) -> &[Self::Item; N]

Source§

fn sub_array_mut<const N: usize>( &mut self, offset: usize, ) -> &mut [Self::Item; N]

Source§

impl<T, const M: usize> SubArray for [T; M]

Implementation on regular arrays

Source§

type Item = T

Source§

fn sub_array_ref<const N: usize>(&self, offset: usize) -> &[Self::Item; N]

Source§

fn sub_array_mut<const N: usize>( &mut self, offset: usize, ) -> &mut [Self::Item; N]

Implementors§