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
fn len(&self) -> usize
Returns the number of elements in the array.
fn is_empty(&self) -> bool
Returns true if the array has a length of 0
fn first(&self) -> Option<&T>
Returns the first element of the array, or None
if it is empty.
fn first_mut(&mut self) -> Option<&mut T>
Returns a mutable pointer to the first element of the array, or None
if it is empty.
fn last(&self) -> Option<&T>
Returns the last element of the array, or None
if it is empty.
fn last_mut(&mut self) -> Option<&mut T>
Returns a mutable pointer to the last element of the array, or None
if it is empty.
fn get(&self, index: usize) -> Option<&T>
Returns the element of an array at the given index, or None
if the index is out of bounds.
fn get_mut(&mut self, index: usize) -> Option<&mut T>
Returns a mutable reference to the element at the given index, or None
if the index is out of bounds.
fn as_ptr(&self) -> *const T
Returns an raw pointer to the array's buffer.
fn as_mut_ptr(&mut self) -> *mut T
Returns an unsafe mutable pointer to the array's buffer.
fn as_slice(&self) -> &[T]
Extracts a slice containing the entire array.
fn as_mut_slice(&mut self) -> &mut [T]
Extracts a mutable slice of the entire array.
fn map_<F>(self, f: F) -> Self where
T: Copy,
F: FnMut(T) -> T,
Self: Sized,
T: Copy,
F: FnMut(T) -> T,
Self: Sized,
Takes a FnMut(T) -> T
closure and creates a new array by calling that closure on each element.
fn foldl<A, F>(self, acc: A, f: F) -> A where
T: Copy,
F: FnMut(A, T) -> A,
Self: Sized,
T: Copy,
F: FnMut(A, T) -> A,
Self: Sized,
Applies a function over the entire array, producing a single final value.
fn foldr<A, F>(self, acc: A, f: F) -> A where
T: Copy,
F: FnMut(A, T) -> A,
Self: Sized,
T: Copy,
F: FnMut(A, T) -> A,
Self: Sized,
Applies a function over the entire array (in reverse order), producing a single final value.
fn from_fn<F>(f: F) -> Self where
F: FnMut(usize) -> T,
Self: Sized,
F: FnMut(usize) -> T,
Self: Sized,
Creates a new array using the provided closure.
fn from_iter<I: Iterator<Item = T>>(iter: I) -> Option<Self> where
Self: Sized,
Self: Sized,
Creates an array by extracting elements from the provided iterator.
Implementors
impl<T> Array<T> for [T; 32]
impl<T> Array<T> for [T; 31]
impl<T> Array<T> for [T; 30]
impl<T> Array<T> for [T; 29]
impl<T> Array<T> for [T; 28]
impl<T> Array<T> for [T; 27]
impl<T> Array<T> for [T; 26]
impl<T> Array<T> for [T; 25]
impl<T> Array<T> for [T; 24]
impl<T> Array<T> for [T; 23]
impl<T> Array<T> for [T; 22]
impl<T> Array<T> for [T; 21]
impl<T> Array<T> for [T; 20]
impl<T> Array<T> for [T; 19]
impl<T> Array<T> for [T; 18]
impl<T> Array<T> for [T; 17]
impl<T> Array<T> for [T; 16]
impl<T> Array<T> for [T; 15]
impl<T> Array<T> for [T; 14]
impl<T> Array<T> for [T; 13]
impl<T> Array<T> for [T; 12]
impl<T> Array<T> for [T; 11]
impl<T> Array<T> for [T; 10]
impl<T> Array<T> for [T; 9]
impl<T> Array<T> for [T; 8]
impl<T> Array<T> for [T; 7]
impl<T> Array<T> for [T; 6]
impl<T> Array<T> for [T; 5]
impl<T> Array<T> for [T; 4]
impl<T> Array<T> for [T; 3]
impl<T> Array<T> for [T; 2]
impl<T> Array<T> for [T; 1]
impl<T> Array<T> for [T; 0]