Trait array_ext::Array [] [src]

pub trait Array<T> {
    fn len(&self) -> usize;
    fn is_empty(&self) -> bool;
    fn first(&self) -> Option<&T>;
    fn first_mut(&mut self) -> Option<&mut T>;
    fn last(&self) -> Option<&T>;
    fn last_mut(&mut self) -> Option<&mut T>;
    fn get(&self, index: usize) -> Option<&T>;
    fn get_mut(&mut self, index: usize) -> Option<&mut T>;
    fn as_ptr(&self) -> *const T;
    fn as_mut_ptr(&mut self) -> *mut T;
    fn as_slice(&self) -> &[T];
    fn as_mut_slice(&mut self) -> &mut [T];
    fn map_<F>(self, f: F) -> Self
    where
        T: Copy,
        F: FnMut(T) -> T,
        Self: Sized
; fn foldl<A, F>(self, acc: A, f: F) -> A
    where
        T: Copy,
        F: FnMut(A, T) -> A,
        Self: Sized
; fn foldr<A, F>(self, acc: A, f: F) -> A
    where
        T: Copy,
        F: FnMut(A, T) -> A,
        Self: Sized
; fn from_fn<F>(f: F) -> Self
    where
        F: FnMut(usize) -> T,
        Self: Sized
; fn from_iter<I: Iterator<Item = T>>(iter: I) -> Option<Self>
    where
        Self: Sized
; }

Generic array type.

This trait allows passing arrays by value in a generic way without turning them into slices, so the functions get monomorphized for a specific size.

Examples

use array_ext::Array;

fn average<T: Array<f32>>(arr: T) -> f32
{
    let n = arr.len() as f32;
    arr.foldl(0.0, |acc, val| acc + val) / n
}

assert_eq!(average([8.96, 3.14, 17.9]), 10.0);

Required Methods

Returns the number of elements in the array.

Returns true if the array has a length of 0

Returns the first element of the array, or None if it is empty.

Returns a mutable pointer to the first element of the array, or None if it is empty.

Returns the last element of the array, or None if it is empty.

Returns a mutable pointer to the last element of the array, or None if it is empty.

Returns the element of an array at the given index, or None if the index is out of bounds.

Returns a mutable reference to the element at the given index, or None if the index is out of bounds.

Returns an raw pointer to the array's buffer.

Returns an unsafe mutable pointer to the array's buffer.

Extracts a slice containing the entire array.

Extracts a mutable slice of the entire array.

Takes a FnMut(T) -> T closure and creates a new array by calling that closure on each element.

Applies a function over the entire array, producing a single final value.

Applies a function over the entire array (in reverse order), producing a single final value.

Creates a new array using the provided closure.

Creates an array by extracting elements from the provided iterator.

Implementors