ArrayExt

Trait ArrayExt 

Source
pub trait ArrayExt {
    type Item;

    // Required methods
    fn sub_array<const OFFSET: usize, const LEN: usize>(
        &self,
    ) -> &[Self::Item; LEN];
    fn split_array<const LEFT: usize, const RIGHT: usize>(
        &self,
    ) -> (&[Self::Item; LEFT], &[Self::Item; RIGHT]);

    // Provided methods
    fn get_static<const INDEX: usize>(&self) -> &Self::Item { ... }
    fn first(&self) -> &Self::Item { ... }
    fn split_first<const RIGHT: usize>(
        &self,
    ) -> (&Self::Item, &[Self::Item; RIGHT]) { ... }
    fn split_last<const LEFT: usize>(
        &self,
    ) -> (&Self::Item, &[Self::Item; LEFT]) { ... }
}
Expand description

Extension trait for arrays.

Required Associated Types§

Source

type Item

The item type the array is storing.

Required Methods§

Source

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

Just like the slicing operation, this returns an array LEN items long at position OFFSET.

The correctness of this operation is compile-time checked.

Note that unlike slicing where the second number is the end index, here the second number is array length!

Source

fn split_array<const LEFT: usize, const RIGHT: usize>( &self, ) -> (&[Self::Item; LEFT], &[Self::Item; RIGHT])

Splits the array into two, non-overlapping smaller arrays covering the entire range.

This is almost equivalent to just calling sub_array twice, except it also checks that the arrays don’t overlap and that they cover the full range. This is very useful for demonstrating correctness, especially when chained. Using this technique even revealed a bug in the past. (#4195)

Provided Methods§

Source

fn get_static<const INDEX: usize>(&self) -> &Self::Item

Returns an item at given statically-known index.

This is just like normal indexing except the check happens at compile time.

Source

fn first(&self) -> &Self::Item

Returns the first item in an array.

Fails to compile if the array is empty.

Note that this method’s name intentionally shadows the std’s first method which returns Option. The rationale is that given the known length of the array, we always know that this will not return None so trying to keep the std method around is pointless. Importing the trait will also cause compile failures - that’s also intentional to expose the places where useless checks are made.

Source

fn split_first<const RIGHT: usize>(&self) -> (&Self::Item, &[Self::Item; RIGHT])

Splits the array into the first element and the remaining, one element shorter, array.

Fails to compile if the array is empty.

Note that this method’s name intentionally shadows the std’s split_first method which returns Option. The rationale is that given the known length of the array, we always know that this will not return None so trying to keep the std method around is pointless. Importing the trait will also cause compile failures - that’s also intentional to expose the places where useless checks are made.

Source

fn split_last<const LEFT: usize>(&self) -> (&Self::Item, &[Self::Item; LEFT])

Splits the array into the last element and the remaining, one element shorter, array.

Fails to compile if the array is empty.

Note that this method’s name intentionally shadows the std’s split_last method which returns Option. The rationale is that given the known length of the array, we always know that this will not return None so trying to keep the std method around is pointless. Importing the trait will also cause compile failures - that’s also intentional to expose the places where useless checks are made.

The returned tuple is also reversed just as std for consistency and simpler diffs when migrating.

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<const N: usize, T> ArrayExt for [T; N]

Source§

type Item = T

Source§

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

Source§

fn split_array<const LEFT: usize, const RIGHT: usize>( &self, ) -> (&[Self::Item; LEFT], &[Self::Item; RIGHT])

Implementors§