diff --git a/src/data_traits.rs b/src/data_traits.rs index 86636043c..b4a33ff53 100644 --- a/src/data_traits.rs +++ b/src/data_traits.rs @@ -64,6 +64,24 @@ pub unsafe trait DataRawMut : DataRaw { fn try_is_unique(&mut self) -> Option; } +/// Array representation trait. +/// +/// An array representation that can be cloned. +/// +/// ***Internal trait, see `DataRaw`.*** +pub unsafe trait DataRawClone : DataRaw { + #[doc(hidden)] + /// Unsafe because, `ptr` must point inside the current storage. + unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem); + + #[doc(hidden)] + unsafe fn clone_from_with_ptr(&mut self, other: &Self, ptr: *mut Self::Elem) -> *mut Self::Elem { + let (data, ptr) = other.clone_with_ptr(ptr); + *self = data; + ptr + } +} + /// Array representation trait. /// /// For an array with elements that can be accessed with safe code. @@ -111,21 +129,13 @@ pub unsafe trait DataMut : Data + DataRawMut { /// Array representation trait. /// -/// An array representation that can be cloned. +/// An array representation that can be cloned and allows elements to be +/// accessed with safe code. /// /// ***Internal trait, see `Data`.*** -pub unsafe trait DataClone : Data { - #[doc(hidden)] - /// Unsafe because, `ptr` must point inside the current storage. - unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem); +pub trait DataClone : Data + DataRawClone {} - #[doc(hidden)] - unsafe fn clone_from_with_ptr(&mut self, other: &Self, ptr: *mut Self::Elem) -> *mut Self::Elem { - let (data, ptr) = other.clone_with_ptr(ptr); - *self = data; - ptr - } -} +impl DataClone for T where T: Data + DataRawClone {} unsafe impl DataRaw for RawViewRepr<*const A> { type Elem = A; @@ -135,6 +145,12 @@ unsafe impl DataRaw for RawViewRepr<*const A> { private_impl!{} } +unsafe impl DataRawClone for RawViewRepr<*const A> { + unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem) { + (*self, ptr) + } +} + unsafe impl DataRaw for RawViewRepr<*mut A> { type Elem = A; fn _data_slice(&self) -> Option<&[A]> { @@ -156,6 +172,12 @@ unsafe impl DataRawMut for RawViewRepr<*mut A> { } } +unsafe impl DataRawClone for RawViewRepr<*mut A> { + unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem) { + (*self, ptr) + } +} + unsafe impl DataRaw for OwnedArcRepr { type Elem = A; fn _data_slice(&self) -> Option<&[A]> { @@ -222,7 +244,7 @@ unsafe impl Data for OwnedArcRepr { unsafe impl DataMut for OwnedArcRepr where A: Clone {} -unsafe impl DataClone for OwnedArcRepr { +unsafe impl DataRawClone for OwnedArcRepr { unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem) { // pointer is preserved (self.clone(), ptr) @@ -263,7 +285,7 @@ unsafe impl Data for OwnedRepr { unsafe impl DataMut for OwnedRepr { } -unsafe impl DataClone for OwnedRepr +unsafe impl DataRawClone for OwnedRepr where A: Clone { unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem) { @@ -307,7 +329,7 @@ unsafe impl<'a, A> Data for ViewRepr<&'a A> { } } -unsafe impl<'a, A> DataClone for ViewRepr<&'a A> { +unsafe impl<'a, A> DataRawClone for ViewRepr<&'a A> { unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem) { (*self, ptr) } diff --git a/src/impl_clone.rs b/src/impl_clone.rs index 9626d945d..34c5599f6 100644 --- a/src/impl_clone.rs +++ b/src/impl_clone.rs @@ -6,9 +6,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. use imp_prelude::*; -use DataClone; +use DataRawClone; -impl Clone for ArrayBase { +impl Clone for ArrayBase { fn clone(&self) -> ArrayBase { unsafe { let (data, ptr) = self.data.clone_with_ptr(self.ptr); @@ -33,5 +33,4 @@ impl Clone for ArrayBase { } } -impl Copy for ArrayBase {} - +impl Copy for ArrayBase {} diff --git a/src/lib.rs b/src/lib.rs index 7c8bf929b..7659f6c55 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -154,6 +154,7 @@ pub use aliases::*; pub use data_traits::{ DataRaw, DataRawMut, + DataRawClone, Data, DataMut, DataOwned,