Skip to content

Commit

Permalink
Add DataRawClone and relax Clone implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jturner314 committed Nov 19, 2018
1 parent 9960d06 commit d2daef9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
52 changes: 37 additions & 15 deletions src/data_traits.rs
Expand Up @@ -64,6 +64,24 @@ pub unsafe trait DataRawMut : DataRaw {
fn try_is_unique(&mut self) -> Option<bool>;
}

/// 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.
Expand Down Expand Up @@ -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<T> DataClone for T where T: Data + DataRawClone {}

unsafe impl<A> DataRaw for RawViewRepr<*const A> {
type Elem = A;
Expand All @@ -135,6 +145,12 @@ unsafe impl<A> DataRaw for RawViewRepr<*const A> {
private_impl!{}
}

unsafe impl<A> DataRawClone for RawViewRepr<*const A> {
unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem) {
(*self, ptr)
}
}

unsafe impl<A> DataRaw for RawViewRepr<*mut A> {
type Elem = A;
fn _data_slice(&self) -> Option<&[A]> {
Expand All @@ -156,6 +172,12 @@ unsafe impl<A> DataRawMut for RawViewRepr<*mut A> {
}
}

unsafe impl<A> DataRawClone for RawViewRepr<*mut A> {
unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem) {
(*self, ptr)
}
}

unsafe impl<A> DataRaw for OwnedArcRepr<A> {
type Elem = A;
fn _data_slice(&self) -> Option<&[A]> {
Expand Down Expand Up @@ -222,7 +244,7 @@ unsafe impl<A> Data for OwnedArcRepr<A> {

unsafe impl<A> DataMut for OwnedArcRepr<A> where A: Clone {}

unsafe impl<A> DataClone for OwnedArcRepr<A> {
unsafe impl<A> DataRawClone for OwnedArcRepr<A> {
unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem) {
// pointer is preserved
(self.clone(), ptr)
Expand Down Expand Up @@ -263,7 +285,7 @@ unsafe impl<A> Data for OwnedRepr<A> {

unsafe impl<A> DataMut for OwnedRepr<A> { }

unsafe impl<A> DataClone for OwnedRepr<A>
unsafe impl<A> DataRawClone for OwnedRepr<A>
where A: Clone
{
unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem) {
Expand Down Expand Up @@ -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)
}
Expand Down
7 changes: 3 additions & 4 deletions src/impl_clone.rs
Expand Up @@ -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<S: DataClone, D: Clone> Clone for ArrayBase<S, D> {
impl<S: DataRawClone, D: Clone> Clone for ArrayBase<S, D> {
fn clone(&self) -> ArrayBase<S, D> {
unsafe {
let (data, ptr) = self.data.clone_with_ptr(self.ptr);
Expand All @@ -33,5 +33,4 @@ impl<S: DataClone, D: Clone> Clone for ArrayBase<S, D> {
}
}

impl<S: DataClone + Copy, D: Copy> Copy for ArrayBase<S, D> {}

impl<S: DataRawClone + Copy, D: Copy> Copy for ArrayBase<S, D> {}
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -154,6 +154,7 @@ pub use aliases::*;
pub use data_traits::{
DataRaw,
DataRawMut,
DataRawClone,
Data,
DataMut,
DataOwned,
Expand Down

0 comments on commit d2daef9

Please sign in to comment.