Skip to content

Make CowArray an owned storage array, require Clone bound for into_shared #1028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/arraytraits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@
use alloc::boxed::Box;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use std::hash;
use std::mem;
use std::mem::size_of;
use std::ops::{Index, IndexMut};
use std::{hash, mem::size_of};
use std::{iter::FromIterator, slice};

use crate::imp_prelude::*;
use crate::Arc;

use crate::{
dimension,
iter::{Iter, IterMut},
numeric_util,
FoldWhile,
NdIndex,
OwnedArcRepr,
Zip,
};

Expand Down Expand Up @@ -457,7 +461,9 @@ where D: Dimension
{
fn from(arr: Array<A, D>) -> ArcArray<A, D>
{
arr.into_shared()
let data = OwnedArcRepr(Arc::new(arr.data));
// safe because: equivalent unmoved data, ptr and dims remain valid
unsafe { ArrayBase::from_data_ptr(data, arr.ptr).with_strides_dim(arr.strides, arr.dim) }
}
}

Expand Down
42 changes: 35 additions & 7 deletions src/data_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ unsafe impl<'a, A> DataMut for ViewRepr<&'a mut A> {}

/// Array representation trait.
///
/// A representation that is a unique or shared owner of its data.
/// A representation which can be the owner of its data.
///
/// ***Internal trait, see `Data`.***
// The owned storage represents the ownership and allocation of the array's elements.
Expand All @@ -553,9 +553,13 @@ pub unsafe trait DataOwned: Data
fn new(elements: Vec<Self::Elem>) -> Self;

/// Converts the data representation to a shared (copy on write)
/// representation, without any copying.
/// representation, cloning the array elements if necessary.
#[doc(hidden)]
fn into_shared(self) -> OwnedArcRepr<Self::Elem>;
#[allow(clippy::wrong_self_convention)]
fn into_shared<D>(self_: ArrayBase<Self, D>) -> ArcArray<Self::Elem, D>
where
Self::Elem: Clone,
D: Dimension;
}

/// Array representation trait.
Expand All @@ -578,9 +582,12 @@ unsafe impl<A> DataOwned for OwnedRepr<A>
OwnedRepr::from(elements)
}

fn into_shared(self) -> OwnedArcRepr<A>
fn into_shared<D>(self_: ArrayBase<Self, D>) -> ArcArray<A, D>
where
A: Clone,
D: Dimension,
{
OwnedArcRepr(Arc::new(self))
ArcArray::from(self_)
}
}

Expand All @@ -593,9 +600,12 @@ unsafe impl<A> DataOwned for OwnedArcRepr<A>
OwnedArcRepr(Arc::new(OwnedRepr::from(elements)))
}

fn into_shared(self) -> OwnedArcRepr<A>
fn into_shared<D>(self_: ArrayBase<Self, D>) -> ArcArray<A, D>
where
A: Clone,
D: Dimension,
{
self
self_
}
}

Expand Down Expand Up @@ -720,6 +730,24 @@ unsafe impl<'a, A> Data for CowRepr<'a, A>

unsafe impl<'a, A> DataMut for CowRepr<'a, A> where A: Clone {}

unsafe impl<'a, A> DataOwned for CowRepr<'a, A>
{
type MaybeUninit = CowRepr<'a, MaybeUninit<A>>;

fn new(elements: Vec<A>) -> Self
{
CowRepr::Owned(OwnedRepr::new(elements))
}

fn into_shared<D>(self_: ArrayBase<Self, D>) -> ArcArray<A, D>
where
A: Clone,
D: Dimension,
{
self_.into_owned().into_shared()
}
}

/// Array representation trait.
///
/// The RawDataSubst trait maps the element type of array storage, while
Expand Down
14 changes: 9 additions & 5 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,17 @@ where
}

/// Turn the array into a shared ownership (copy on write) array,
/// without any copying.
/// cloning the array elements if necessary.
///
/// If you want to generalize over `Array` and `ArcArray` inputs but avoid
/// an `A: Clone` bound, use `Into::<ArcArray<A, D>>::into` instead of this
/// method.
pub fn into_shared(self) -> ArcArray<A, D>
where S: DataOwned
where
A: Clone,
S: DataOwned,
{
let data = self.data.into_shared();
// safe because: equivalent unmoved data, ptr and dims remain valid
unsafe { ArrayBase::from_data_ptr(data, self.ptr).with_strides_dim(self.strides, self.dim) }
S::into_shared(self)
}

/// Returns a reference to the first element of the array, or `None` if it
Expand Down