AsArray

Trait AsArray 

Source
pub trait AsArray: AsSlice {
    const LENGTH: usize;

    // Required methods
    fn as_array(&self) -> &[Self::Elem; Self::LENGTH];
    fn as_array_mut(&mut self) -> &mut [Self::Elem; Self::LENGTH];
}

Required Associated Constants§

Source

const LENGTH: usize

Length of array as compile-time constant

§Example 1: Length

Array::LENGTH will always equal the actual length of the array.

use array_trait::*;
 
const L: usize = 4;
 
let array: [f32; L] = [1.0, 2.0, 3.0, 4.0];
 
assert_eq!(<[f32; L]>::LENGTH, L);
assert_eq!(<[f32; L]>::LENGTH, array.len());
§Example 2: Generic const-expression usage

This can be used in const-expressions as shown below.

#![feature(generic_const_exprs)]
#![feature(iter_array_chunks)]
 
use array_trait::*;
 
fn first_half<T: Array>(array: T) -> [<T as AsSlice>::Elem; T::LENGTH/2]
{
    array.into_iter().array_chunks().next().unwrap()
}
 
assert_eq!(first_half([1.0, 2.0, 3.0, 4.0]), [1.0, 2.0]);

Required Methods§

Source

fn as_array(&self) -> &[Self::Elem; Self::LENGTH]

Returns self as an array-slice

Similar to Array::into_array, but is passed by reference.

Useful in the case where a trait is implemented using a generic bound to the Array trait. In this case, the compiler does not automatically know that the type with the Array-trait is an actual array. This method lets you tell the compiler that you are now working with an actual array, and not just something which implements the trait Array.

Source

fn as_array_mut(&mut self) -> &mut [Self::Elem; Self::LENGTH]

Returns self as a mutable array-slice

Similar to Array::into_array, but is passed by mutable reference.

Useful in the case where a trait is implemented using a generic bound to the Array trait. In this case, the compiler does not automatically know that the type with the Array-trait is an actual array. This method lets you tell the compiler that you are now working with an actual array, and not just something which implements the trait Array.

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

Source§

const LENGTH: usize = LENGTH

Source§

fn as_array(&self) -> &[Self::Elem; Self::LENGTH]

Source§

fn as_array_mut(&mut self) -> &mut [Self::Elem; Self::LENGTH]

Source§

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

Available on crate feature alloc only.
Source§

const LENGTH: usize = N

Source§

fn as_array(&self) -> &[Self::Elem; Self::LENGTH]

Source§

fn as_array_mut(&mut self) -> &mut [Self::Elem; Self::LENGTH]

Implementors§