Skip to main content

Array

Struct Array 

Source
pub struct Array<D, const RANK: usize> { /* private fields */ }
Expand description

🗃️ 🫗 A logical array over backing storage.


📍 data/layout/array::Array

📦 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 ArrayLayout mapping 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>

Source

pub const fn data(&self) -> &D

Returns a shared reference to the backing data.

Source

pub const fn layout(&self) -> ArrayLayout<RANK>

Returns the array layout.

Source

pub fn into_data(self) -> D

Consumes the array and returns its backing data.

Source

pub fn into_parts(self) -> (D, ArrayLayout<RANK>)

Decomposes the array into its backing data and layout.

Source

pub const fn shape(&self) -> ArrayShape<RANK>

Returns the logical shape.

Source

pub const fn rank(&self) -> usize

Returns the number of logical axes.

Source

pub const fn element_count(&self) -> usize

Returns the number of logical elements.

Source

pub const fn is_empty(&self) -> bool

Returns whether the logical array has no elements.

Source

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.

Source

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.

Source

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.

Source

pub const fn storage(&self) -> &[T]

Returns the complete physical backing slice.

This is not necessarily the array’s logical ravel order.

Source

pub const fn storage_mut(&mut self) -> &mut [T]

Returns the complete exclusive physical backing slice.

Source

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.

Source

pub const fn storage_len(&self) -> usize

Returns the backing-storage length.

Source

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.

Source

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.

Source

pub const fn reborrow(&self) -> Array<&[T], RANK>

Returns a shared slice-backed array reborrowed from this array.

Source

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.

Source

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.

Source

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.

Source

pub const fn storage(&self) -> &'a [T]

Returns the complete physical backing slice.

This is not necessarily the array’s logical ravel order.

Source

pub const fn storage_len(&self) -> usize

Returns the backing-storage length.

Source

pub const fn get(&self, coord: [usize; RANK]) -> Option<&'a T>

Returns the element at coord, or None if it is out of bounds.

Source

pub const fn reborrow(&self) -> Array<&[T], RANK>

Returns a shared view reborrowed for the lifetime of self.

Source§

impl<'a, T, const RANK: usize> Array<&'a mut [T], RANK>

§Methods over an exclusive slice.

Source

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.

Source

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.

Source

pub const fn storage(&self) -> &[T]

Returns the complete physical backing slice.

Source

pub const fn storage_mut(&mut self) -> &mut [T]

Returns the complete exclusive physical backing slice.

Source

pub const fn storage_len(&self) -> usize

Returns the backing-storage length.

Source

pub const fn get(&self, coord: [usize; RANK]) -> Option<&T>

Returns a shared reference to the element at coord.

Source

pub const fn get_mut(&mut self, coord: [usize; RANK]) -> Option<&mut T>

Returns an exclusive reference to the element at coord.

Source

pub const fn reborrow(&self) -> Array<&[T], RANK>

Returns a shared view reborrowed for the lifetime of self.

Source

pub const fn reborrow_mut(&mut self) -> Array<&mut [T], RANK>

Returns an exclusive view reborrowed for the lifetime of self.

Source

pub const fn into_shared(self) -> Array<&'a [T], RANK>

Consumes the exclusive view and returns a shared view.

Source§

impl<T, const RANK: usize> Array<Box<[T]>, RANK>

§Methods over a boxed slice.

Source

pub fn try_from_boxed_slice( storage: Box<[T]>, layout: ArrayLayout<RANK>, ) -> Result<Self, MismatchedCapacity>

Available on crate feature 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.

Source

pub fn storage(&self) -> &[T]

Available on crate feature alloc only.

Returns the complete physical backing slice.

This is not necessarily the array’s logical ravel order.

Source

pub fn storage_mut(&mut self) -> &mut [T]

Available on crate feature alloc only.

Returns the complete exclusive physical backing slice.

Source

pub fn storage_len(&self) -> usize

Available on crate feature alloc only.

Returns the backing-storage length.

Source

pub fn get(&self, coord: [usize; RANK]) -> Option<&T>

Available on crate feature alloc only.

Returns a shared reference to the element at coord.

Source

pub fn get_mut(&mut self, coord: [usize; RANK]) -> Option<&mut T>

Available on crate feature alloc only.

Returns an exclusive reference to the element at coord.

Source

pub fn reborrow(&self) -> Array<&[T], RANK>

Available on crate feature alloc only.

Returns a shared slice-backed array reborrowed from this array.

Source

pub fn reborrow_mut(&mut self) -> Array<&mut [T], RANK>

Available on crate feature alloc only.

Returns an exclusive slice-backed array reborrowed from this array.

Source

pub fn into_vec(self) -> Array<Vec<T>, RANK>

Available on crate feature alloc only.

Converts this boxed-slice-backed array into a vector-backed array.

The logical layout is preserved.

Source§

impl<T, const RANK: usize> Array<Vec<T>, RANK>

§Methods over a vector.

Source

pub fn try_from_vec( storage: Vec<T>, layout: ArrayLayout<RANK>, ) -> Result<Self, MismatchedCapacity>

Available on crate feature 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.

Source

pub fn storage(&self) -> &[T]

Available on crate feature alloc only.

Returns the complete physical backing slice.

This is not necessarily the array’s logical ravel order.

Source

pub fn storage_mut(&mut self) -> &mut [T]

Available on crate feature alloc only.

Returns the complete exclusive physical backing slice.

This permits mutation of initialized elements without permitting changes to the vector’s length.

Source

pub fn storage_len(&self) -> usize

Available on crate feature alloc only.

Returns the backing-storage length.

Source

pub fn get(&self, coord: [usize; RANK]) -> Option<&T>

Available on crate feature alloc only.

Returns a shared reference to the element at coord.

Returns None if the coordinate is outside the logical shape.

Source

pub fn get_mut(&mut self, coord: [usize; RANK]) -> Option<&mut T>

Available on crate feature alloc only.

Returns an exclusive reference to the element at coord.

Returns None if the coordinate is outside the logical shape.

Source

pub fn reborrow(&self) -> Array<&[T], RANK>

Available on crate feature alloc only.

Returns a shared slice-backed array reborrowed from this array.

Source

pub fn reborrow_mut(&mut self) -> Array<&mut [T], RANK>

Available on crate feature alloc only.

Returns an exclusive slice-backed array reborrowed from this array.

Source

pub fn into_boxed(self) -> Array<Box<[T]>, RANK>

Available on crate feature alloc only.

Converts this vector-backed array into a boxed-slice-backed array.

The logical layout is preserved.

Trait Implementations§

Source§

impl<D: Clone, const RANK: usize> Clone for Array<D, RANK>

Source§

fn clone(&self) -> Array<D, RANK>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<D: Copy, const RANK: usize> Copy for Array<D, RANK>

Source§

impl<D: Debug, const RANK: usize> Debug for Array<D, RANK>

Source§

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

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

impl<D> From<Array<D, 2>> for Table<D>

Source§

fn from(array: Array<D, 2>) -> Self

Converts to this type from the input type.
Source§

impl<D> From<Table<D>> for Array<D, 2>

Source§

fn from(table: Table<D>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<D, const RANK: usize> Freeze for Array<D, RANK>
where D: Freeze, ArrayLayout<RANK>: Freeze,

§

impl<D, const RANK: usize> RefUnwindSafe for Array<D, RANK>

§

impl<D, const RANK: usize> Send for Array<D, RANK>
where D: Send, ArrayLayout<RANK>: Send,

§

impl<D, const RANK: usize> Sync for Array<D, RANK>
where D: Sync, ArrayLayout<RANK>: Sync,

§

impl<D, const RANK: usize> Unpin for Array<D, RANK>
where D: Unpin, ArrayLayout<RANK>: Unpin,

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn type_id() -> TypeId

Returns the TypeId of Self. Read more
Source§

fn type_of(&self) -> TypeId

Returns the TypeId of self. Read more
Source§

fn type_name(&self) -> &'static str

Returns the type name of self. Read more
Source§

fn type_is<T: 'static>(&self) -> bool

Returns true if Self is of type T. Read more
Source§

fn type_hash(&self) -> u64

Returns a deterministic hash of the TypeId of Self.
Source§

fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64

Returns a deterministic hash of the TypeId of Self using a custom hasher.
Source§

fn as_any_ref(&self) -> &dyn Any
where Self: Sized,

Upcasts &self as &dyn Any. Read more
Source§

fn as_any_mut(&mut self) -> &mut dyn Any
where Self: Sized,

Upcasts &mut self as &mut dyn Any. Read more
Source§

fn as_any_box(self: Box<Self>) -> Box<dyn Any>
where Self: Sized,

Available on crate feature alloc only.
Upcasts Box<self> as Box<dyn Any>. Read more
Source§

fn downcast_ref<T: 'static>(&self) -> Option<&T>

Available on crate feature unsafe_layout and non-crate feature safe_code only.
Returns some shared reference to the inner value if it is of type T. Read more
Source§

fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T>

Available on crate feature unsafe_layout and non-crate feature safe_code only.
Returns some exclusive reference to the inner value if it is of type T. 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> ByteSized for T

Source§

const BYTE_ALIGN: usize = _

The alignment of this type in bytes.
Source§

const BYTE_SIZE: usize = _

The size of this type in bytes.
Source§

fn byte_align(&self) -> usize

Returns the alignment of this type in bytes.
Source§

fn byte_size(&self) -> usize

Returns the size of this type in bytes. Read more
Source§

fn ptr_size_ratio(&self) -> [usize; 2]

Returns the size ratio between Ptr::BYTES and BYTE_SIZE. 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> Hook for T

Source§

fn hook<F>(self, f: F) -> Self
where F: FnOnce(&mut Self),

Hooks a mutation step into the value and returns it. Read more
Source§

fn tap<F>(self, f: F) -> Self
where F: FnOnce(&Self),

Taps into the value for observation and returns it unchanged. Read more
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> MemExt for T
where T: ?Sized,

Source§

const NEEDS_DROP: bool = _

Know whether dropping values of this type matters, in compile-time.
Source§

fn mem_align_of<T>() -> usize

Returns the minimum alignment of the type in bytes. Read more
Source§

fn mem_align_of_val(&self) -> usize

Returns the alignment of the pointed-to value in bytes. Read more
Source§

fn mem_size_of<T>() -> usize

Returns the size of a type in bytes. Read more
Source§

fn mem_size_of_val(&self) -> usize

Returns the size of the pointed-to value in bytes. Read more
Source§

fn mem_copy(&self) -> Self
where Self: Copy,

Bitwise-copies a value. Read more
Source§

fn mem_needs_drop(&self) -> bool

Returns true if dropping values of this type matters. Read more
Source§

fn mem_drop(self)
where Self: Sized,

Drops self by running its destructor. Read more
Source§

fn mem_forget(self)
where Self: Sized,

Forgets about self without running its destructor. Read more
Source§

fn mem_replace(&mut self, other: Self) -> Self
where Self: Sized,

Replaces self with other, returning the previous value of self. Read more
Source§

fn mem_take(&mut self) -> Self
where Self: Default,

Replaces self with its default value, returning the previous value of self. Read more
Source§

fn mem_swap(&mut self, other: &mut Self)
where Self: Sized,

Swaps the value of self and other without deinitializing either one. Read more
Source§

unsafe fn mem_zeroed<T>() -> T

Available on crate feature unsafe_layout only.
Returns the value of type T represented by the all-zero byte-pattern. Read more
Source§

unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst

Available on crate feature unsafe_layout only.
Returns the value of type T represented by the all-zero byte-pattern. Read more
Source§

fn mem_as_bytes(&self) -> &[u8]
where Self: Sync + Unpin,

Available on crate feature unsafe_slice only.
View a Sync + Unpin self as &[u8]. Read more
Source§

fn mem_as_bytes_mut(&mut self) -> &mut [u8]
where Self: Sync + Unpin,

Available on crate feature unsafe_slice only.
View a Sync + Unpin self as &mut [u8]. Read more
Source§

impl<T, R> Morph<R> for T
where T: ?Sized,

Source§

fn morph<F>(self, f: F) -> R
where F: FnOnce(Self) -> R, Self: Sized,

Morphs the value into a new one and returns it. Read more
Source§

fn morph_ref<F>(&self, f: F) -> R
where F: FnOnce(&Self) -> R,

Morphs the value by shared reference and returns the result. Read more
Source§

fn morph_mut<F>(&mut self, f: F) -> R
where F: FnOnce(&mut Self) -> R,

Morphs the value by exclusive reference and returns the result. Read more
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.