Array

Trait Array 

Source
pub unsafe trait Array
where Self: SealedArray + AsMut<[Self::Scalar]> + AsRef<[Self::Scalar]> + Borrow<[Self::Scalar]> + BorrowMut<[Self::Scalar]> + IntoIterator<Item = Self::Scalar> + Sized,
{ type Scalar: Sized; const LEN: usize; // Required methods fn from_fn<F: FnMut(usize) -> Self::Scalar>(f: F) -> Self; fn each_ref(&self) -> impl Array<Scalar = &Self::Scalar>; fn each_mut(&mut self) -> impl Array<Scalar = &mut Self::Scalar>; fn map<F: FnMut(Self::Scalar) -> U, U>( self, op: F, ) -> impl Array<Scalar = U>; fn as_slice(&self) -> &[Self::Scalar]; fn as_mut_slice(&mut self) -> &mut [Self::Scalar]; }
Expand description

Denotes an array.

This trait allows abstracting unsized arrays. It provides the methods and functions provided by both [_; _] and [_].

§Safety

Self must always be [Self::Scalar; Self::LEN].

Furthermore, all items must behave exactly as their standard counterparts.

§Examples

Random-access arrays:

use multitype::Array;

fn get_4th_element<T: Array<Scalar: Copy>>(data: T) -> Option<T::Scalar> {
    data.as_slice().get(0x4).copied()
}

assert_eq!(
    get_4th_element([0, 1, 1, 2, 3, 5]),
    Some(3),
);

assert_eq!(
    get_4th_element([3, 1, 4, 1, 6]),
    Some(6),
);

assert_eq!(
    get_4th_element([1, 6, 1, 8]),
    None,
);

Required Associated Constants§

Source

const LEN: usize

The length of the array, in count of elements.

Required Associated Types§

Source

type Scalar: Sized

The scalar (element) type of the array.

Required Methods§

Source

fn from_fn<F: FnMut(usize) -> Self::Scalar>(f: F) -> Self

Source

fn each_ref(&self) -> impl Array<Scalar = &Self::Scalar>

Source

fn each_mut(&mut self) -> impl Array<Scalar = &mut Self::Scalar>

Source

fn map<F: FnMut(Self::Scalar) -> U, U>(self, op: F) -> impl Array<Scalar = U>

Source

fn as_slice(&self) -> &[Self::Scalar]

Source

fn as_mut_slice(&mut self) -> &mut [Self::Scalar]

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

Source§

const LEN: usize = N

Source§

type Scalar = T

Source§

fn from_fn<F: FnMut(usize) -> T>(f: F) -> Self

Source§

fn each_ref(&self) -> [&T; N]

Source§

fn each_mut(&mut self) -> [&mut T; N]

Source§

fn map<F: FnMut(Self::Scalar) -> U, U>(self, op: F) -> [U; N]

Source§

fn as_slice(&self) -> &[Self::Scalar]

Source§

fn as_mut_slice(&mut self) -> &mut [Self::Scalar]

Implementors§