pub trait AsArray: AsSlice {
const LENGTH: usize;
// Required methods
const fn as_array(&self) -> &[Self::Item; Self::LENGTH];
const fn as_array_mut(&mut self) -> &mut [Self::Item; Self::LENGTH];
}Required Associated Constants§
Sourceconst LENGTH: usize
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 core::iter::IntoIterator>::Item; 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§
Sourceconst fn as_array(&self) -> &[Self::Item; Self::LENGTH]
const fn as_array(&self) -> &[Self::Item; 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.
Sourceconst fn as_array_mut(&mut self) -> &mut [Self::Item; Self::LENGTH]
const fn as_array_mut(&mut self) -> &mut [Self::Item; 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.