pub struct Array<D, const RANK: usize> { /* private fields */ }Expand description
🗃️ 🫗 A logical array over backing storage.
📦 size_of::<Array<[u8; 6], 1>>() == 20 bytes / 160 bits #️⃣ #[cfg(target_pointer_width = “32”)]
⚗️Option<T> ❗🟰 T
📦 size_of::<Array<[u8; 6], 2>>() == 28 bytes / 224 bits #️⃣ #[cfg(target_pointer_width = “32”)]
⚗️Option<T> ❗🟰 T
📦 size_of::<Array<[u8; 6], 3>>() == 36 bytes / 288 bits #️⃣ #[cfg(target_pointer_width = “32”)]
⚗️Option<T> ❗🟰 T
📦 size_of::<Array<[u8; 6], 1>>() == 32 bytes / 256 bits #️⃣ #[cfg(target_pointer_width = “64”)]
⚗️Option<T> ❗🟰 T
📦 size_of::<Array<[u8; 6], 2>>() == 48 bytes / 384 bits #️⃣ #[cfg(target_pointer_width = “64”)]
⚗️Option<T> ❗🟰 T
📦 size_of::<Array<[u8; 6], 3>>() == 64 bytes / 512 bits #️⃣ #[cfg(target_pointer_width = “64”)]
⚗️Option<T> ❗🟰 T
An array joins:
- backing data of type
D; - an
ArrayLayoutmapping logical coordinates into that data.
D determines whether the array borrows or owns its storage,
whether access is shared or exclusive, and whether the
storage length is fixed or dynamically determined.
§Invariant
Every physical storage position addressed by layout
must be accessible through data.
The provided constructors preserve this relationship.
§Examples
º
use devela::{ArrayLayout, ArrayShape, Array};
let storage = [0, 1, 2, 3, 4, 5];
let shape = ArrayShape::new([2, 3]);
let layout = ArrayLayout::dense_last(shape)?;
let view = Array::try_from_slice_ref(&storage, layout)?;
assert_eq!(view.get([1, 2]).copied(), Some(5));Implementations§
Source§impl<D, const RANK: usize> Array<D, RANK>
impl<D, const RANK: usize> Array<D, RANK>
Sourcepub const fn layout(&self) -> ArrayLayout<RANK>
pub const fn layout(&self) -> ArrayLayout<RANK>
Returns the array layout.
Sourcepub fn into_parts(self) -> (D, ArrayLayout<RANK>) ⓘ
pub fn into_parts(self) -> (D, ArrayLayout<RANK>) ⓘ
Decomposes the array into its backing data and layout.
Sourcepub const fn shape(&self) -> ArrayShape<RANK>
pub const fn shape(&self) -> ArrayShape<RANK>
Returns the logical shape.
Sourcepub const fn element_count(&self) -> usize
pub const fn element_count(&self) -> usize
Returns the number of logical elements.
Sourcepub const fn coords(&self) -> ArrayCoordIter<RANK> ⓘ
pub const fn coords(&self) -> ArrayCoordIter<RANK> ⓘ
Returns an iterator over every logical coordinate of this array.
Coordinates are independent of physical offset, strides, backing-storage order, and ownership.
Source§impl<T, const LEN: usize, const RANK: usize> Array<[T; LEN], RANK>
§Methods over a fixed native array.
impl<T, const LEN: usize, const RANK: usize> Array<[T; LEN], RANK>
§Methods over a fixed native array.
Sourcepub fn try_from_array(
storage: [T; LEN],
layout: ArrayLayout<RANK>,
) -> Result<Self, MismatchedCapacity> ⓘ
pub fn try_from_array( storage: [T; LEN], layout: ArrayLayout<RANK>, ) -> Result<Self, MismatchedCapacity> ⓘ
Creates an owning array over the fixed native storage.
Extra backing elements are permitted.
§Errors
Returns MismatchedCapacity if storage does not cover
every physical position addressed by layout.
Sourcepub const fn try_from_array_copy(
storage: [T; LEN],
layout: ArrayLayout<RANK>,
) -> Result<Self, MismatchedCapacity> ⓘwhere
T: Copy,
pub const fn try_from_array_copy(
storage: [T; LEN],
layout: ArrayLayout<RANK>,
) -> Result<Self, MismatchedCapacity> ⓘwhere
T: Copy,
Creates an owning array over a fixed native backing array in const contexts.
This is the T: Copy counterpart of try_from_array.
Extra backing elements are permitted.
§Errors
Returns MismatchedCapacity if storage does not cover every
physical position addressed by layout.
Sourcepub const fn storage(&self) -> &[T] ⓘ
pub const fn storage(&self) -> &[T] ⓘ
Returns the complete physical backing slice.
This is not necessarily the array’s logical ravel order.
Sourcepub const fn storage_mut(&mut self) -> &mut [T] ⓘ
pub const fn storage_mut(&mut self) -> &mut [T] ⓘ
Returns the complete exclusive physical backing slice.
Sourcepub const fn data_mut(&mut self) -> &mut [T; LEN] ⓘ
pub const fn data_mut(&mut self) -> &mut [T; LEN] ⓘ
Returns exclusive access to the fixed native backing array.
Unlike dynamically sized backing storage, exposing this value cannot change the physical storage length.
Sourcepub const fn storage_len(&self) -> usize
pub const fn storage_len(&self) -> usize
Returns the backing-storage length.
Sourcepub const fn get(&self, coord: [usize; RANK]) -> Option<&T> ⓘ
pub const fn get(&self, coord: [usize; RANK]) -> Option<&T> ⓘ
Returns a shared reference to the element at coord.
Returns None if the coordinate is outside the logical shape.
Sourcepub const fn get_mut(&mut self, coord: [usize; RANK]) -> Option<&mut T> ⓘ
pub const fn get_mut(&mut self, coord: [usize; RANK]) -> Option<&mut T> ⓘ
Returns an exclusive reference to the element at coord.
Returns None if the coordinate is outside the logical shape.
Sourcepub const fn reborrow(&self) -> Array<&[T], RANK>
pub const fn reborrow(&self) -> Array<&[T], RANK>
Returns a shared slice-backed array reborrowed from this array.
Sourcepub const fn reborrow_mut(&mut self) -> Array<&mut [T], RANK>
pub const fn reborrow_mut(&mut self) -> Array<&mut [T], RANK>
Returns an exclusive slice-backed array reborrowed from this array.
Source§impl<'a, T, const RANK: usize> Array<&'a [T], RANK>
§Methods over a shared slice.
impl<'a, T, const RANK: usize> Array<&'a [T], RANK>
§Methods over a shared slice.
Sourcepub const fn try_from_slice(
storage: &'a [T],
layout: ArrayLayout<RANK>,
) -> Result<Self, MismatchedCapacity> ⓘ
pub const fn try_from_slice( storage: &'a [T], layout: ArrayLayout<RANK>, ) -> Result<Self, MismatchedCapacity> ⓘ
Creates a shared view over storage.
Extra backing elements are permitted and remain visible through
storage.
§Errors
Returns MismatchedCapacity if the storage is too short to contain
every position addressed by layout.
Sourcepub const fn try_from_slice_ref(
storage: &'a [T],
layout: ArrayLayout<RANK>,
) -> Result<Self, MismatchedCapacity> ⓘ
pub const fn try_from_slice_ref( storage: &'a [T], layout: ArrayLayout<RANK>, ) -> Result<Self, MismatchedCapacity> ⓘ
Creates a shared slice-backed view.
This inference-friendly constructor is equivalent to
try_from_slice, but allows the backing data
and rank to be inferred from storage and layout.
Sourcepub const fn storage(&self) -> &'a [T] ⓘ
pub const fn storage(&self) -> &'a [T] ⓘ
Returns the complete physical backing slice.
This is not necessarily the array’s logical ravel order.
Sourcepub const fn storage_len(&self) -> usize
pub const fn storage_len(&self) -> usize
Returns the backing-storage length.
Source§impl<'a, T, const RANK: usize> Array<&'a mut [T], RANK>
§Methods over an exclusive slice.
impl<'a, T, const RANK: usize> Array<&'a mut [T], RANK>
§Methods over an exclusive slice.
Sourcepub const fn try_from_slice(
storage: &'a mut [T],
layout: ArrayLayout<RANK>,
) -> Result<Self, MismatchedCapacity> ⓘ
pub const fn try_from_slice( storage: &'a mut [T], layout: ArrayLayout<RANK>, ) -> Result<Self, MismatchedCapacity> ⓘ
Creates an exclusive view over storage.
Extra backing elements are permitted and remain accessible through
storage_mut.
§Errors
Returns MismatchedCapacity if the storage is too short to contain
every position addressed by layout.
Sourcepub const fn try_from_slice_mut(
storage: &'a mut [T],
layout: ArrayLayout<RANK>,
) -> Result<Self, MismatchedCapacity> ⓘ
pub const fn try_from_slice_mut( storage: &'a mut [T], layout: ArrayLayout<RANK>, ) -> Result<Self, MismatchedCapacity> ⓘ
Creates an exclusive slice-backed view.
This inference-friendly constructor is equivalent to
try_from_slice, but allows the backing data
and rank to be inferred from storage and layout.
Sourcepub const fn storage_mut(&mut self) -> &mut [T] ⓘ
pub const fn storage_mut(&mut self) -> &mut [T] ⓘ
Returns the complete exclusive physical backing slice.
Sourcepub const fn storage_len(&self) -> usize
pub const fn storage_len(&self) -> usize
Returns the backing-storage length.
Sourcepub const fn get(&self, coord: [usize; RANK]) -> Option<&T> ⓘ
pub const fn get(&self, coord: [usize; RANK]) -> Option<&T> ⓘ
Returns a shared reference to the element at coord.
Sourcepub const fn get_mut(&mut self, coord: [usize; RANK]) -> Option<&mut T> ⓘ
pub const fn get_mut(&mut self, coord: [usize; RANK]) -> Option<&mut T> ⓘ
Returns an exclusive reference to the element at coord.
Sourcepub const fn reborrow(&self) -> Array<&[T], RANK>
pub const fn reborrow(&self) -> Array<&[T], RANK>
Returns a shared view reborrowed for the lifetime of self.
Sourcepub const fn reborrow_mut(&mut self) -> Array<&mut [T], RANK>
pub const fn reborrow_mut(&mut self) -> Array<&mut [T], RANK>
Returns an exclusive view reborrowed for the lifetime of self.
Consumes the exclusive view and returns a shared view.
Source§impl<T, const RANK: usize> Array<Box<[T]>, RANK>
§Methods over a boxed slice.
impl<T, const RANK: usize> Array<Box<[T]>, RANK>
§Methods over a boxed slice.
Sourcepub fn try_from_boxed_slice(
storage: Box<[T]>,
layout: ArrayLayout<RANK>,
) -> Result<Self, MismatchedCapacity> ⓘ
Available on crate feature alloc only.
pub fn try_from_boxed_slice( storage: Box<[T]>, layout: ArrayLayout<RANK>, ) -> Result<Self, MismatchedCapacity> ⓘ
alloc only.Creates an owning array over the boxed slice storage.
Extra initialized backing elements are permitted.
§Errors
Returns MismatchedCapacity if storage does not cover
every physical position addressed by layout.
Sourcepub fn storage(&self) -> &[T] ⓘ
Available on crate feature alloc only.
pub fn storage(&self) -> &[T] ⓘ
alloc only.Returns the complete physical backing slice.
This is not necessarily the array’s logical ravel order.
Sourcepub fn storage_mut(&mut self) -> &mut [T] ⓘ
Available on crate feature alloc only.
pub fn storage_mut(&mut self) -> &mut [T] ⓘ
alloc only.Returns the complete exclusive physical backing slice.
Sourcepub fn storage_len(&self) -> usize
Available on crate feature alloc only.
pub fn storage_len(&self) -> usize
alloc only.Returns the backing-storage length.
Sourcepub fn get(&self, coord: [usize; RANK]) -> Option<&T> ⓘ
Available on crate feature alloc only.
pub fn get(&self, coord: [usize; RANK]) -> Option<&T> ⓘ
alloc only.Returns a shared reference to the element at coord.
Sourcepub fn get_mut(&mut self, coord: [usize; RANK]) -> Option<&mut T> ⓘ
Available on crate feature alloc only.
pub fn get_mut(&mut self, coord: [usize; RANK]) -> Option<&mut T> ⓘ
alloc only.Returns an exclusive reference to the element at coord.
Sourcepub fn reborrow(&self) -> Array<&[T], RANK>
Available on crate feature alloc only.
pub fn reborrow(&self) -> Array<&[T], RANK>
alloc only.Returns a shared slice-backed array reborrowed from this array.
Sourcepub fn reborrow_mut(&mut self) -> Array<&mut [T], RANK>
Available on crate feature alloc only.
pub fn reborrow_mut(&mut self) -> Array<&mut [T], RANK>
alloc only.Returns an exclusive slice-backed array reborrowed from this array.
Source§impl<T, const RANK: usize> Array<Vec<T>, RANK>
§Methods over a vector.
impl<T, const RANK: usize> Array<Vec<T>, RANK>
§Methods over a vector.
Sourcepub fn try_from_vec(
storage: Vec<T>,
layout: ArrayLayout<RANK>,
) -> Result<Self, MismatchedCapacity> ⓘ
Available on crate feature alloc only.
pub fn try_from_vec( storage: Vec<T>, layout: ArrayLayout<RANK>, ) -> Result<Self, MismatchedCapacity> ⓘ
alloc only.Creates an owning array over the vector storage.
Extra initialized backing elements are permitted.
The vector’s capacity is not considered accessible storage;
only its initialized length must cover layout.
§Errors
Returns MismatchedCapacity if storage does not cover
every physical position addressed by layout.
Sourcepub fn storage(&self) -> &[T] ⓘ
Available on crate feature alloc only.
pub fn storage(&self) -> &[T] ⓘ
alloc only.Returns the complete physical backing slice.
This is not necessarily the array’s logical ravel order.
Sourcepub fn storage_mut(&mut self) -> &mut [T] ⓘ
Available on crate feature alloc only.
pub fn storage_mut(&mut self) -> &mut [T] ⓘ
alloc only.Returns the complete exclusive physical backing slice.
This permits mutation of initialized elements without permitting changes to the vector’s length.
Sourcepub fn storage_len(&self) -> usize
Available on crate feature alloc only.
pub fn storage_len(&self) -> usize
alloc only.Returns the backing-storage length.
Sourcepub fn get(&self, coord: [usize; RANK]) -> Option<&T> ⓘ
Available on crate feature alloc only.
pub fn get(&self, coord: [usize; RANK]) -> Option<&T> ⓘ
alloc only.Returns a shared reference to the element at coord.
Returns None if the coordinate is outside the logical shape.
Sourcepub fn get_mut(&mut self, coord: [usize; RANK]) -> Option<&mut T> ⓘ
Available on crate feature alloc only.
pub fn get_mut(&mut self, coord: [usize; RANK]) -> Option<&mut T> ⓘ
alloc only.Returns an exclusive reference to the element at coord.
Returns None if the coordinate is outside the logical shape.
Sourcepub fn reborrow(&self) -> Array<&[T], RANK>
Available on crate feature alloc only.
pub fn reborrow(&self) -> Array<&[T], RANK>
alloc only.Returns a shared slice-backed array reborrowed from this array.
Sourcepub fn reborrow_mut(&mut self) -> Array<&mut [T], RANK>
Available on crate feature alloc only.
pub fn reborrow_mut(&mut self) -> Array<&mut [T], RANK>
alloc only.Returns an exclusive slice-backed array reborrowed from this array.
Sourcepub fn into_boxed(self) -> Array<Box<[T]>, RANK>
Available on crate feature alloc only.
pub fn into_boxed(self) -> Array<Box<[T]>, RANK>
alloc only.Converts this vector-backed array into a boxed-slice-backed array.
The logical layout is preserved.
Trait Implementations§
impl<D: Copy, const RANK: usize> Copy for Array<D, RANK>
Auto Trait Implementations§
impl<D, const RANK: usize> Freeze for Array<D, RANK>
impl<D, const RANK: usize> RefUnwindSafe for Array<D, RANK>
impl<D, const RANK: usize> Send for Array<D, RANK>
impl<D, const RANK: usize> Sync for Array<D, RANK>
impl<D, const RANK: usize> Unpin for Array<D, RANK>
impl<D, const RANK: usize> UnsafeUnpin for Array<D, RANK>
impl<D, const RANK: usize> UnwindSafe for Array<D, RANK>
Blanket Implementations§
Source§impl<T> AnyExt for T
impl<T> AnyExt for T
Source§fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
TypeId of Self using a custom hasher.Source§fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
Source§fn as_any_box(self: Box<Self>) -> Box<dyn Any>where
Self: Sized,
fn as_any_box(self: Box<Self>) -> Box<dyn Any>where
Self: Sized,
alloc only.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> ByteSized for T
impl<T> ByteSized for T
Source§const BYTE_ALIGN: usize = _
const BYTE_ALIGN: usize = _
Source§fn byte_align(&self) -> usize
fn byte_align(&self) -> usize
Source§fn ptr_size_ratio(&self) -> [usize; 2]
fn ptr_size_ratio(&self) -> [usize; 2]
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> MemExt for Twhere
T: ?Sized,
impl<T> MemExt for Twhere
T: ?Sized,
Source§const NEEDS_DROP: bool = _
const NEEDS_DROP: bool = _
Source§fn mem_align_of<T>() -> usize
fn mem_align_of<T>() -> usize
Source§fn mem_align_of_val(&self) -> usize
fn mem_align_of_val(&self) -> usize
Source§fn mem_size_of<T>() -> usize
fn mem_size_of<T>() -> usize
Source§fn mem_size_of_val(&self) -> usize
fn mem_size_of_val(&self) -> usize
Source§fn mem_needs_drop(&self) -> bool
fn mem_needs_drop(&self) -> bool
true if dropping values of this type matters. Read moreSource§fn mem_forget(self)where
Self: Sized,
fn mem_forget(self)where
Self: Sized,
self without running its destructor. Read moreSource§fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
Source§unsafe fn mem_zeroed<T>() -> T
unsafe fn mem_zeroed<T>() -> T
unsafe_layout only.T represented by the all-zero byte-pattern. Read moreSource§unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe_layout only.T represented by the all-zero byte-pattern. Read moreSource§fn mem_as_bytes(&self) -> &[u8] ⓘ
fn mem_as_bytes(&self) -> &[u8] ⓘ
unsafe_slice only.