Struct ArrayWrapper

Source
pub struct ArrayWrapper<A> { /* private fields */ }
Expand description

Wrapper over array types. It implements the same1 traits as array, but not for only arrays of sizes 0..=32 but for all arrays, those sizes are supported by the crate2:

Note: Implementations of the most trai just copy-pasted from std

§Examples

PartialEq/Eq :

use arraylib::{ArrayExt, ArrayWrapper};

let arr = [1, 2, 3].wrap();
assert_eq!(arr, [1, 2, 3]);
assert_eq!(format!("{:?}", arr), "[1, 2, 3]");
assert!(arr < [1, 4, 0]);
assert_eq!(arr[1], 2);

assert!(arr.into_iter().eq(vec![1, 2, 3].into_iter()));

assert_eq!(<ArrayWrapper<[i32; 3]> as Default>::default(), [0, 0, 0]);
use arraylib::{ArrayExt, ArrayWrapper};
use std::borrow::{Borrow, BorrowMut};

let mut arr = [1, 2, 3].wrap();

let slice: &[i32] = arr.as_ref();
assert_eq!(slice, &[1, 2, 3]);
use arraylib::ArrayExt;
use std::{
    collections::hash_map::DefaultHasher,
    hash::{Hash, Hasher},
};

let mut hasher = DefaultHasher::new();
[1, 2, 3].wrap().hash(&mut hasher);
let hash0 = hasher.finish();

let mut hasher = DefaultHasher::new();
[1, 2, 3].hash(&mut hasher);
let hash1 = hasher.finish();

assert_eq!(hash0, hash1);

In difference with std, ArrayWrapper is iterable:

use arraylib::ArrayExt;

let arr = [0, 3, 45, 91].wrap();
for x in arr {}
  • IntoIterator (on std’s array IntoIterator is implemented only on &[T; N] and on &mut [T; N] while on ArrayWrapper it’s implemented directly (using IterMove))
  • In some traits instead of TryFromSliceError there is SizeError
  • Copy/Clone are implemented only when A: Copy/Clone and not when A::Item: Copy/Clone because we can’t use compiler magic :(

  1. differences: 

  2. See Sizes Limitations paragraph in crate docs. 

Implementations§

Source§

impl<A> ArrayWrapper<A>
where A: Array,

Source

pub fn new(inner: A) -> Self

Create new ArrayWrapper

use arraylib::ArrayWrapper;

let arr = ArrayWrapper::new([0, 1, 2, 3]);
println!("{:?}", arr);
Source

pub fn into_inner(self) -> A

Destruct ArrayWrapper into inner array

use arraylib::ArrayWrapper;

let arr = ArrayWrapper::new([0, 1, 2, 3]);
// ...
let arr = arr.into_inner();
println!("{:?}", arr);

Trait Implementations§

Source§

impl<A> Array for ArrayWrapper<A>
where A: Array,

§Safety

ArrayWrapper has #[repr(transparent)] so it has the same ABI as A. So if A is array then so is ArrayWrapper.

Source§

const SIZE: usize = A::SIZE

Size of the array. Read more
Source§

type Item = <A as Array>::Item

Type of the Items in the array. i.e. Read more
Source§

type Maybe = ArrayWrapper<<A as Array>::Maybe>

Same array but item is wrapped with MaybeUninit<_>. Read more
Source§

fn into_boxed_slice(self) -> Box<[Self::Item]>

Available on crate feature alloc only.
Converts self into Box<[Self::Item]>
Source§

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

Extracts a slice containing the entire array. Read more
Source§

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

Extracts a mutable slice of the entire array. Read more
Source§

fn try_unfold<St, F, E>(init: St, f: F) -> Result<Self, E>
where F: FnMut(&mut St) -> Result<Self::Item, E>,

Create new array, filled with elements returned by f. If f return Err then this method also return Err. Read more
Source§

fn unfold<St, F>(init: St, f: F) -> Self
where F: FnMut(&mut St) -> Self::Item,

Create new array, filled with elements returned by f Read more
Source§

fn try_from_fn<F, E>(f: F) -> Result<Self, E>
where F: FnMut(usize) -> Result<Self::Item, E>,

Create new array, filled with elements returned by f. If f return Err then this method also return Err. Read more
Source§

fn from_fn<F>(f: F) -> Self
where F: FnMut(usize) -> Self::Item,

Create new array, filled with elements returned by f Read more
Source§

fn try_from_iter<I>(iter: I) -> Option<Self>
where I: IntoIterator<Item = Self::Item>,

Creates an array from an iterator. Read more
Source§

fn into_uninit(self) -> Self::Maybe

Converts self into [MaybeUninit<Self::Item>; Self::Size]. This function is used internally in this crate for some unsafe code. Read more
Source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = Self::Item>,

Creates an array from an iterator. Read more
Source§

fn uninit() -> Self::Maybe

Creates uninitialized array of MaybeUninit<T>. Read more
Source§

unsafe fn assume_init(uninit: Self::Maybe) -> Self

Extracts the values from the MaybeUninit<T> containers. Read more
Source§

impl<A> AsMut<[<A as Array>::Item]> for ArrayWrapper<A>
where A: Array,

Source§

fn as_mut(&mut self) -> &mut [A::Item]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<A> AsMut<A> for ArrayWrapper<A>

Source§

fn as_mut(&mut self) -> &mut A

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<A> AsRef<[<A as Array>::Item]> for ArrayWrapper<A>
where A: Array,

Source§

fn as_ref(&self) -> &[A::Item]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<A> AsRef<A> for ArrayWrapper<A>

Source§

fn as_ref(&self) -> &A

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<A> Borrow<[<A as Array>::Item]> for ArrayWrapper<A>
where A: Array,

Source§

fn borrow(&self) -> &[A::Item]

Immutably borrows from an owned value. Read more
Source§

impl<A> BorrowMut<[<A as Array>::Item]> for ArrayWrapper<A>
where A: Array,

Source§

fn borrow_mut(&mut self) -> &mut [A::Item]

Mutably borrows from an owned value. Read more
Source§

impl<A: Clone> Clone for ArrayWrapper<A>

Source§

fn clone(&self) -> ArrayWrapper<A>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<A> Debug for ArrayWrapper<A>
where A::Item: Debug, A: Array,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<A> Default for ArrayWrapper<A>
where A: Array, A::Item: Default,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<A> Hash for ArrayWrapper<A>
where A::Item: Hash, A: Array,

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<A, Idx> Index<Idx> for ArrayWrapper<A>
where A: Array, Idx: SliceIndex<[A::Item]>,

Source§

type Output = <Idx as SliceIndex<[<A as Array>::Item]>>::Output

The returned type after indexing.
Source§

fn index(&self, index: Idx) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<A, Idx> IndexMut<Idx> for ArrayWrapper<A>
where A: Array, Idx: SliceIndex<[A::Item]>,

Source§

fn index_mut(&mut self, index: Idx) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<A> IntoIterator for ArrayWrapper<A>
where A: Array,

Source§

type IntoIter = IterMove<ArrayWrapper<A>>

Which kind of iterator are we turning this into?
Source§

type Item = <A as Array>::Item

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<A> Ord for ArrayWrapper<A>
where A: Array, A::Item: Ord,

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'t, A, T> PartialEq<&'t [T]> for ArrayWrapper<A>
where A::Item: PartialEq<T>, A: Array,

Source§

fn eq(&self, other: &&'t [T]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'t, A, T> PartialEq<&'t mut [T]> for ArrayWrapper<A>
where A::Item: PartialEq<T>, A: Array,

Source§

fn eq(&self, other: &&'t mut [T]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<A, T> PartialEq<[T]> for ArrayWrapper<A>
where A::Item: PartialEq<T>, A: Array,

Source§

fn eq(&self, other: &[T]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'t, A, T> PartialEq<ArrayWrapper<A>> for &'t [T]
where T: PartialEq<A::Item>, A: Array,

Source§

fn eq(&self, other: &ArrayWrapper<A>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'t, A, T> PartialEq<ArrayWrapper<A>> for &'t mut [T]
where T: PartialEq<A::Item>, A: Array,

Source§

fn eq(&self, other: &ArrayWrapper<A>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<A, T> PartialEq<ArrayWrapper<A>> for [T]
where T: PartialEq<A::Item>, A: Array,

Source§

fn eq(&self, other: &ArrayWrapper<A>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<A, B> PartialEq<B> for ArrayWrapper<A>
where A::Item: PartialEq<B::Item>, A: Array, B: Array,

Source§

fn eq(&self, other: &B) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<A, B> PartialOrd<B> for ArrayWrapper<A>
where A: Array, B: Array<Item = A::Item>, A::Item: PartialOrd,

Source§

fn partial_cmp(&self, other: &B) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
Source§

fn lt(&self, other: &B) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
Source§

fn le(&self, other: &B) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
Source§

fn gt(&self, other: &B) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
Source§

fn ge(&self, other: &B) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a, A> TryFrom<&'a [<A as Array>::Item]> for &'a ArrayWrapper<A>
where A: Array,

§Examples
use arraylib::ArrayWrapper;
use core::convert::TryInto;

let slice = &[0, 1, 2][..];
let arr: &ArrayWrapper<[i32; 3]> = slice.try_into().unwrap();
assert_eq!(arr, &ArrayWrapper::new([0, 1, 2]))
Source§

type Error = &'a [<A as Array>::Item]

The type returned in the event of a conversion error.
Source§

fn try_from(slice: &'a [A::Item]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<A> TryFrom<&[<A as Array>::Item]> for ArrayWrapper<A>
where A::Item: Copy, A: Array,

Source§

type Error = SizeError

The type returned in the event of a conversion error.
Source§

fn try_from(slice: &[A::Item]) -> Result<Self, SizeError>

Performs the conversion.
Source§

impl<'a, A> TryFrom<&'a mut [<A as Array>::Item]> for &'a mut ArrayWrapper<A>
where A: Array,

§Examples
use arraylib::ArrayWrapper;
use core::convert::TryInto;

let slice = &mut [0, 1, 2][..];
let arr: &mut ArrayWrapper<[i32; 3]> = slice.try_into().unwrap();
assert_eq!(arr, &ArrayWrapper::new([0, 1, 2]))
Source§

type Error = &'a mut [<A as Array>::Item]

The type returned in the event of a conversion error.
Source§

fn try_from(slice: &'a mut [A::Item]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<A: Copy> Copy for ArrayWrapper<A>

Source§

impl<A> Eq for ArrayWrapper<A>
where A: Array, A::Item: Eq,

Auto Trait Implementations§

§

impl<A> Freeze for ArrayWrapper<A>
where A: Freeze,

§

impl<A> RefUnwindSafe for ArrayWrapper<A>
where A: RefUnwindSafe,

§

impl<A> Send for ArrayWrapper<A>
where A: Send,

§

impl<A> Sync for ArrayWrapper<A>
where A: Sync,

§

impl<A> Unpin for ArrayWrapper<A>
where A: Unpin,

§

impl<A> UnwindSafe for ArrayWrapper<A>
where A: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<A> ArrayExt for A
where A: Array,

Source§

fn iter_move(self) -> IterMove<Self>

Creates iterator which moves elements out of array. Read more
Source§

fn concat_arr<A, R>(self, other: A) -> R
where A: Array<Item = Self::Item>, R: Array<Item = Self::Item>,

Example Read more
Source§

fn split_arr<A, B>(self) -> (A, B)
where A: Array<Item = Self::Item>, B: Array<Item = Self::Item>,

Splits self into 2 arrays Read more
Source§

fn into_array<A>(self) -> Result<A, Self>
where A: Array<Item = Self::Item>,

Converts self into an array. This function will return Some(_) if sizes of Self and A are the same and None otherwise. Read more
Source§

fn to_vec(&self) -> Vec<Self::Item>
where Self::Item: Clone,

Available on crate feature alloc only.
Copies self into a new Vec. Read more
Source§

fn into_vec(self) -> Vec<Self::Item>

Available on crate feature alloc only.
Converts self into a vector without clones. Read more
Source§

fn from_slice(slice: &[Self::Item]) -> Result<Self, SizeError>
where Self::Item: Copy,

Create array from slice. Return Err(()) if slice.len != Self::SIZE. Read more
Source§

fn clone_from_slice(slice: &[Self::Item]) -> Result<Self, SizeError>
where Self::Item: Clone,

Create array from slice. Return Err(()) if slice.len != Self::SIZE. Read more
Source§

fn wrap(self) -> ArrayWrapper<Self>

Wrap self into ArrayWrapper
Source§

fn ref_cast(slice: &[Self::Item]) -> &Self

Safely cast &[T] to &Self ([T; N]) Read more
Source§

fn mut_cast(slice: &mut [Self::Item]) -> &mut Self

Safely cast &mut [T] to &mut Self ([T; N]) Read more
Source§

fn try_ref_cast(slice: &[Self::Item]) -> Result<&Self, &[Self::Item]>

Safely cast &[T] to &Self ([T; N]) Read more
Source§

fn try_mut_cast( slice: &mut [Self::Item], ) -> Result<&mut Self, &mut [Self::Item]>

Safely cast &mut [T] to &mut Self ([T; N]) Read more
Source§

unsafe fn ref_cast_unchecked(slice: &[Self::Item]) -> &Self

Unsafety cast &[T] to &Self ([T; N]) Read more
Source§

unsafe fn mut_cast_unchecked(slice: &mut [Self::Item]) -> &mut Self

Unsafety cast &mut [T] to &mut Self ([T; N]) Read more
Source§

impl<A> ArrayShorthand for A
where A: Array,

Source§

fn index<Idx>(&self, idx: Idx) -> &Idx::Output
where Idx: SliceIndex<[Self::Item]>,

Returns a reference to the value corresponding to the supplied index. Read more
Source§

fn index_mut<Idx>(&mut self, idx: Idx) -> &mut Idx::Output
where Idx: SliceIndex<[Self::Item]>,

Returns a unique reference to the value corresponding to the supplied index. Read more
Source§

fn replace(&mut self, idx: usize, item: Self::Item) -> Self::Item

Replace element of the array Read more
Source§

fn take(&mut self, idx: usize) -> Self::Item
where Self::Item: Default,

Take element of the array, replacing it with default Read more
Source§

fn iter(&self) -> Iter<'_, Self::Item>

Returns an iterator over refs to the array. Read more
Source§

fn iter_mut(&mut self) -> IterMut<'_, Self::Item>

Returns an iterator that allows modifying each value. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.