diff --git a/src/data_traits.rs b/src/data_traits.rs index 73aee36fe..535c83c2d 100644 --- a/src/data_traits.rs +++ b/src/data_traits.rs @@ -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(self_: ArrayBase) -> ArrayBase, D> + where + Self::Elem: Clone, + D: Dimension; + private_decl!{} } @@ -77,6 +86,20 @@ unsafe impl Data for OwnedArcRepr { fn _data_slice(&self) -> &[A] { &self.0 } + fn into_owned(mut self_: ArrayBase) -> ArrayBase, 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!{} } @@ -130,6 +153,14 @@ unsafe impl Data for OwnedRepr { fn _data_slice(&self) -> &[A] { &self.0 } + #[inline] + fn into_owned(self_: ArrayBase) -> ArrayBase, D> + where + A: Clone, + D: Dimension, + { + self_ + } private_impl!{} } @@ -166,6 +197,13 @@ unsafe impl<'a, A> Data for ViewRepr<&'a A> { fn _data_slice(&self) -> &[A] { &[] } + fn into_owned(self_: ArrayBase) -> ArrayBase, D> + where + Self::Elem: Clone, + D: Dimension, + { + self_.to_owned() + } private_impl!{} } @@ -180,6 +218,13 @@ unsafe impl<'a, A> Data for ViewRepr<&'a mut A> { fn _data_slice(&self) -> &[A] { &[] } + fn into_owned(self_: ArrayBase) -> ArrayBase, D> + where + Self::Elem: Clone, + D: Dimension, + { + self_.to_owned() + } private_impl!{} } @@ -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; + + /// Converts the data representation to a shared (copy on write) + /// representation, without any copying. #[doc(hidden)] fn into_shared(self) -> OwnedRcRepr; - #[doc(hidden)] - fn into_owned(self_: ArrayBase) -> ArrayBase, D> - where - Self::Elem: Clone, - D: Dimension; } /// Array representation trait. @@ -219,14 +262,6 @@ unsafe impl DataOwned for OwnedRepr { fn into_shared(self) -> OwnedRcRepr { OwnedArcRepr(Arc::new(self.0)) } - #[inline] - fn into_owned(self_: ArrayBase) -> ArrayBase, D> - where - A: Clone, - D: Dimension, - { - self_ - } } unsafe impl DataOwned for OwnedArcRepr { @@ -237,20 +272,4 @@ unsafe impl DataOwned for OwnedArcRepr { fn into_shared(self) -> OwnedRcRepr { self } - - fn into_owned(mut self_: ArrayBase) -> ArrayBase, 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, - } - } } - diff --git a/src/impl_methods.rs b/src/impl_methods.rs index 7d5c7e9a5..886249625 100644 --- a/src/impl_methods.rs +++ b/src/impl_methods.rs @@ -177,10 +177,9 @@ impl ArrayBase where S: Data, 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 where A: Clone, - S: DataOwned, { S::into_owned(self) }