Array

Trait Array 

Source
pub unsafe trait Array
where Self: Array + AsMut<[Self::Scalar]> + AsRef<[Self::Scalar]> + Borrow<[Self::Scalar]> + BorrowMut<[Self::Scalar]> + IntoIterator<Item = Self::Scalar> + Sized + StructuralPartialEq + Thin + Index<usize, Output = Self::Scalar> + Index<Range<usize>, Output = [Self::Scalar]> + Index<RangeFrom<usize>, Output = [Self::Scalar]> + Index<RangeFull, Output = [Self::Scalar]> + Index<RangeInclusive<usize>, Output = [Self::Scalar]> + Index<RangeTo<usize>, Output = [Self::Scalar]> + Index<RangeToInclusive<usize>, Output = [Self::Scalar]> + Index<(Bound<usize>, Bound<usize>), Output = [Self::Scalar]> + IndexMut<usize, Output = Self::Scalar> + IndexMut<Range<usize>, Output = [Self::Scalar]> + IndexMut<RangeFrom<usize>, Output = [Self::Scalar]> + IndexMut<RangeFull, Output = [Self::Scalar]> + IndexMut<RangeInclusive<usize>, Output = [Self::Scalar]> + IndexMut<RangeTo<usize>, Output = [Self::Scalar]> + IndexMut<RangeToInclusive<usize>, Output = [Self::Scalar]> + IndexMut<(Bound<usize>, Bound<usize>), Output = [Self::Scalar]>,
{ type Scalar: Sized; const LEN: usize; // Required methods fn repeat(value: Self::Scalar) -> Self where Self::Scalar: Clone; 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 the array module.

When both associated_type_defaults and generic_const_exprs stabilise, this trait may become deprecated.

§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(4).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 slice.

Required Methods§

Source

fn repeat(value: Self::Scalar) -> Self
where Self::Scalar: Clone,

See array::repeat.

Source

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

See array::from_fn.

Source

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

See <[_; _]>::each_mut.

The value of LEN of the return type must remain equal to that of Self.

Source

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

See <[_; _]>::each_ref.

The value of LEN of the return type must remain equal to that of Self.

Source

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

See <[_; _]>::map.

The value of LEN of the return type must remain equal to that of Self.

Source

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

See <[_; _]>::as_slice.

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 repeat(value: Self::Scalar) -> Self
where Self::Scalar: Clone,

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§