Skip to content

Remove DataOwned constraint from into_owned #498

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 1 commit into from
Oct 27, 2018
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
77 changes: 48 additions & 29 deletions src/data_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,18 @@ use {
pub unsafe trait Data : Sized {
/// The array element type.
type Elem;

#[doc(hidden)]
// This method is only used for debugging
fn _data_slice(&self) -> &[Self::Elem];

/// Converts the array to a uniquely owned array, cloning elements if necessary.
#[doc(hidden)]
fn into_owned<D>(self_: ArrayBase<Self, D>) -> ArrayBase<OwnedRepr<Self::Elem>, D>
where
Self::Elem: Clone,
D: Dimension;

private_decl!{}
}

Expand Down Expand Up @@ -77,6 +86,20 @@ unsafe impl<A> Data for OwnedArcRepr<A> {
fn _data_slice(&self) -> &[A] {
&self.0
}
fn into_owned<D>(mut self_: ArrayBase<Self, D>) -> ArrayBase<OwnedRepr<Self::Elem>, D>
where
A: Clone,
D: Dimension,
{
Self::ensure_unique(&mut self_);
let data = OwnedRepr(Arc::try_unwrap(self_.data.0).ok().unwrap());
ArrayBase {
data: data,
ptr: self_.ptr,
dim: self_.dim,
strides: self_.strides,
}
}
private_impl!{}
}

Expand Down Expand Up @@ -130,6 +153,14 @@ unsafe impl<A> Data for OwnedRepr<A> {
fn _data_slice(&self) -> &[A] {
&self.0
}
#[inline]
fn into_owned<D>(self_: ArrayBase<Self, D>) -> ArrayBase<OwnedRepr<Self::Elem>, D>
where
A: Clone,
D: Dimension,
{
self_
}
private_impl!{}
}

Expand Down Expand Up @@ -166,6 +197,13 @@ unsafe impl<'a, A> Data for ViewRepr<&'a A> {
fn _data_slice(&self) -> &[A] {
&[]
}
fn into_owned<D>(self_: ArrayBase<Self, D>) -> ArrayBase<OwnedRepr<Self::Elem>, D>
where
Self::Elem: Clone,
D: Dimension,
{
self_.to_owned()
}
private_impl!{}
}

Expand All @@ -180,6 +218,13 @@ unsafe impl<'a, A> Data for ViewRepr<&'a mut A> {
fn _data_slice(&self) -> &[A] {
&[]
}
fn into_owned<D>(self_: ArrayBase<Self, D>) -> ArrayBase<OwnedRepr<Self::Elem>, D>
where
Self::Elem: Clone,
D: Dimension,
{
self_.to_owned()
}
private_impl!{}
}

Expand All @@ -193,13 +238,11 @@ unsafe impl<'a, A> DataMut for ViewRepr<&'a mut A> { }
pub unsafe trait DataOwned : Data {
#[doc(hidden)]
fn new(elements: Vec<Self::Elem>) -> Self;

/// Converts the data representation to a shared (copy on write)
/// representation, without any copying.
#[doc(hidden)]
fn into_shared(self) -> OwnedRcRepr<Self::Elem>;
#[doc(hidden)]
fn into_owned<D>(self_: ArrayBase<Self, D>) -> ArrayBase<OwnedRepr<Self::Elem>, D>
where
Self::Elem: Clone,
D: Dimension;
}

/// Array representation trait.
Expand All @@ -219,14 +262,6 @@ unsafe impl<A> DataOwned for OwnedRepr<A> {
fn into_shared(self) -> OwnedRcRepr<A> {
OwnedArcRepr(Arc::new(self.0))
}
#[inline]
fn into_owned<D>(self_: ArrayBase<Self, D>) -> ArrayBase<OwnedRepr<Self::Elem>, D>
where
A: Clone,
D: Dimension,
{
self_
}
}

unsafe impl<A> DataOwned for OwnedArcRepr<A> {
Expand All @@ -237,20 +272,4 @@ unsafe impl<A> DataOwned for OwnedArcRepr<A> {
fn into_shared(self) -> OwnedRcRepr<A> {
self
}

fn into_owned<D>(mut self_: ArrayBase<Self, D>) -> ArrayBase<OwnedRepr<Self::Elem>, D>
where
A: Clone,
D: Dimension,
{
Self::ensure_unique(&mut self_);
let data = OwnedRepr(Arc::try_unwrap(self_.data.0).ok().unwrap());
ArrayBase {
data: data,
ptr: self_.ptr,
dim: self_.dim,
strides: self_.strides,
}
}
}

3 changes: 1 addition & 2 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,9 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
}

/// Turn the array into a uniquely owned array, cloning the array elements
/// to unshare them if necessary.
/// if necessary.
pub fn into_owned(self) -> Array<A, D>
where A: Clone,
S: DataOwned,
{
S::into_owned(self)
}
Expand Down