pub enum CowArray<'a, T: Element, D: Dimension> {
Borrowed(ArrayView<'a, T, D>),
Owned(Array<T, D>),
}Expand description
A copy-on-write array that is either a borrowed view or an owned array.
This is useful when a function might or might not need to allocate: it can accept borrowed data and only clone if mutation is required.
Variants§
Borrowed(ArrayView<'a, T, D>)
Borrowed — refers to data owned by another array.
Owned(Array<T, D>)
Owned — has its own data buffer.
Implementations§
Source§impl<'a, T: Element, D: Dimension> CowArray<'a, T, D>
impl<'a, T: Element, D: Dimension> CowArray<'a, T, D>
Sourcepub const fn from_view(view: ArrayView<'a, T, D>) -> Self
pub const fn from_view(view: ArrayView<'a, T, D>) -> Self
Create a CowArray from a borrowed view.
Sourcepub const fn from_owned(arr: Array<T, D>) -> Self
pub const fn from_owned(arr: Array<T, D>) -> Self
Create a CowArray from an owned array.
Sourcepub fn layout(&self) -> MemoryLayout
pub fn layout(&self) -> MemoryLayout
Memory layout.
Sourcepub const fn is_borrowed(&self) -> bool
pub const fn is_borrowed(&self) -> bool
Whether this is a borrowed (view) variant.
Sourcepub fn into_owned(self) -> Array<T, D>
pub fn into_owned(self) -> Array<T, D>
Convert to an owned array, cloning if currently borrowed.
Sourcepub fn to_mut(&mut self) -> &mut Array<T, D>
pub fn to_mut(&mut self) -> &mut Array<T, D>
Ensure this is the owned variant, cloning if necessary, and return a mutable reference to the owned array.
Sourcepub fn view(&self) -> ArrayView<'_, T, D>
pub fn view(&self) -> ArrayView<'_, T, D>
Get a read-only view of the data.
If this is a borrowed variant, returns a view with the same lifetime
as &self. If owned, returns a view borrowing from self.
Sourcepub fn flags(&self) -> ArrayFlags
pub fn flags(&self) -> ArrayFlags
Array flags.