Trait SubArray

Source
pub unsafe trait SubArray {
    type Item;

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

Array that can be slice into a smaller sub-array.

See the crate level reference.

§Safety

Implementation guarantees to generate compile-time errors when the requested sub-array length + offset combinations exceed the size of the array that the sub-array is being extracted from.

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 OFFSET: usize, const M: usize>(&self) -> &[Self::Item; M]

Get a reference to a sub-array of length M starting at OFFSET.

§Safety

Implementation guarantees to generate a compile-time error when M + OFFSET is greater than the size of the array that sub-array is being extracted from.

§Example
use const_sub_array::SubArray;

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

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

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

Get a mutable reference to a sub-array of length M starting at OFFSET.

§Example
use const_sub_array::SubArray;

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

// Get a mutable sub-array starting at offset 0 and size 2.
let sub: &mut [u8; 2] = arr.sub_array_mut::<0, 2>();
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 OFFSET: usize, const N: usize>(&self) -> &[Self::Item; N]

Source§

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

Source§

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

Implementation on regular arrays.

Source§

type Item = T

Source§

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

Source§

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

Implementors§