Skip to content

Commit d2daef9

Browse files
committed
Add DataRawClone and relax Clone implementation
1 parent 9960d06 commit d2daef9

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

src/data_traits.rs

+37-15
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ pub unsafe trait DataRawMut : DataRaw {
6464
fn try_is_unique(&mut self) -> Option<bool>;
6565
}
6666

67+
/// Array representation trait.
68+
///
69+
/// An array representation that can be cloned.
70+
///
71+
/// ***Internal trait, see `DataRaw`.***
72+
pub unsafe trait DataRawClone : DataRaw {
73+
#[doc(hidden)]
74+
/// Unsafe because, `ptr` must point inside the current storage.
75+
unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem);
76+
77+
#[doc(hidden)]
78+
unsafe fn clone_from_with_ptr(&mut self, other: &Self, ptr: *mut Self::Elem) -> *mut Self::Elem {
79+
let (data, ptr) = other.clone_with_ptr(ptr);
80+
*self = data;
81+
ptr
82+
}
83+
}
84+
6785
/// Array representation trait.
6886
///
6987
/// For an array with elements that can be accessed with safe code.
@@ -111,21 +129,13 @@ pub unsafe trait DataMut : Data + DataRawMut {
111129

112130
/// Array representation trait.
113131
///
114-
/// An array representation that can be cloned.
132+
/// An array representation that can be cloned and allows elements to be
133+
/// accessed with safe code.
115134
///
116135
/// ***Internal trait, see `Data`.***
117-
pub unsafe trait DataClone : Data {
118-
#[doc(hidden)]
119-
/// Unsafe because, `ptr` must point inside the current storage.
120-
unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem);
136+
pub trait DataClone : Data + DataRawClone {}
121137

122-
#[doc(hidden)]
123-
unsafe fn clone_from_with_ptr(&mut self, other: &Self, ptr: *mut Self::Elem) -> *mut Self::Elem {
124-
let (data, ptr) = other.clone_with_ptr(ptr);
125-
*self = data;
126-
ptr
127-
}
128-
}
138+
impl<T> DataClone for T where T: Data + DataRawClone {}
129139

130140
unsafe impl<A> DataRaw for RawViewRepr<*const A> {
131141
type Elem = A;
@@ -135,6 +145,12 @@ unsafe impl<A> DataRaw for RawViewRepr<*const A> {
135145
private_impl!{}
136146
}
137147

148+
unsafe impl<A> DataRawClone for RawViewRepr<*const A> {
149+
unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem) {
150+
(*self, ptr)
151+
}
152+
}
153+
138154
unsafe impl<A> DataRaw for RawViewRepr<*mut A> {
139155
type Elem = A;
140156
fn _data_slice(&self) -> Option<&[A]> {
@@ -156,6 +172,12 @@ unsafe impl<A> DataRawMut for RawViewRepr<*mut A> {
156172
}
157173
}
158174

175+
unsafe impl<A> DataRawClone for RawViewRepr<*mut A> {
176+
unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem) {
177+
(*self, ptr)
178+
}
179+
}
180+
159181
unsafe impl<A> DataRaw for OwnedArcRepr<A> {
160182
type Elem = A;
161183
fn _data_slice(&self) -> Option<&[A]> {
@@ -222,7 +244,7 @@ unsafe impl<A> Data for OwnedArcRepr<A> {
222244

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

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

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

266-
unsafe impl<A> DataClone for OwnedRepr<A>
288+
unsafe impl<A> DataRawClone for OwnedRepr<A>
267289
where A: Clone
268290
{
269291
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> {
307329
}
308330
}
309331

310-
unsafe impl<'a, A> DataClone for ViewRepr<&'a A> {
332+
unsafe impl<'a, A> DataRawClone for ViewRepr<&'a A> {
311333
unsafe fn clone_with_ptr(&self, ptr: *mut Self::Elem) -> (Self, *mut Self::Elem) {
312334
(*self, ptr)
313335
}

src/impl_clone.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88
use imp_prelude::*;
9-
use DataClone;
9+
use DataRawClone;
1010

11-
impl<S: DataClone, D: Clone> Clone for ArrayBase<S, D> {
11+
impl<S: DataRawClone, D: Clone> Clone for ArrayBase<S, D> {
1212
fn clone(&self) -> ArrayBase<S, D> {
1313
unsafe {
1414
let (data, ptr) = self.data.clone_with_ptr(self.ptr);
@@ -33,5 +33,4 @@ impl<S: DataClone, D: Clone> Clone for ArrayBase<S, D> {
3333
}
3434
}
3535

36-
impl<S: DataClone + Copy, D: Copy> Copy for ArrayBase<S, D> {}
37-
36+
impl<S: DataRawClone + Copy, D: Copy> Copy for ArrayBase<S, D> {}

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ pub use aliases::*;
154154
pub use data_traits::{
155155
DataRaw,
156156
DataRawMut,
157+
DataRawClone,
157158
Data,
158159
DataMut,
159160
DataOwned,

0 commit comments

Comments
 (0)