@@ -14,6 +14,7 @@ use std::sync::Arc;
14
14
use {
15
15
ArrayBase ,
16
16
Dimension ,
17
+ RawViewRepr ,
17
18
ViewRepr ,
18
19
OwnedRepr ,
19
20
OwnedRcRepr ,
@@ -22,92 +23,177 @@ use {
22
23
23
24
/// Array representation trait.
24
25
///
25
- /// ***Note:*** `Data` is not an extension interface at this point.
26
+ /// For an array that meets the invariants of the `ArrayBase` type. This trait
27
+ /// does not imply any ownership or lifetime; pointers to elements in the array
28
+ /// may not be safe to dereference.
29
+ ///
30
+ /// ***Note:*** `RawData` is not an extension interface at this point.
26
31
/// Traits in Rust can serve many different roles. This trait is public because
27
32
/// it is used as a bound on public methods.
28
- pub unsafe trait Data : Sized {
33
+ pub unsafe trait RawData : Sized {
29
34
/// The array element type.
30
35
type Elem ;
31
36
32
37
#[ doc( hidden) ]
33
38
// This method is only used for debugging
34
- fn _data_slice ( & self ) -> & [ Self :: Elem ] ;
39
+ fn _data_slice ( & self ) -> Option < & [ Self :: Elem ] > ;
40
+
41
+ private_decl ! { }
42
+ }
43
+
44
+ /// Array representation trait.
45
+ ///
46
+ /// For an array with writable elements.
47
+ ///
48
+ /// ***Internal trait, see `RawData`.***
49
+ pub unsafe trait RawDataMut : RawData {
50
+ /// If possible, ensures that the array has unique access to its data.
51
+ ///
52
+ /// If `Self` provides safe mutable access to array elements, then it
53
+ /// **must** panic or ensure that the data is unique.
54
+ #[ doc( hidden) ]
55
+ fn try_ensure_unique < D > ( & mut ArrayBase < Self , D > )
56
+ where Self : Sized ,
57
+ D : Dimension ;
35
58
59
+ /// If possible, returns whether the array has unique access to its data.
60
+ ///
61
+ /// If `Self` provides safe mutable access to array elements, then it
62
+ /// **must** return `Some(_)`.
63
+ #[ doc( hidden) ]
64
+ fn try_is_unique ( & mut self ) -> Option < bool > ;
65
+ }
66
+
67
+ /// Array representation trait.
68
+ ///
69
+ /// An array representation that can be cloned.
70
+ ///
71
+ /// ***Internal trait, see `RawData`.***
72
+ pub unsafe trait RawDataClone : RawData {
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
+
85
+ /// Array representation trait.
86
+ ///
87
+ /// For an array with elements that can be accessed with safe code.
88
+ ///
89
+ /// ***Internal trait, see `RawData`.***
90
+ pub unsafe trait Data : RawData {
36
91
/// Converts the array to a uniquely owned array, cloning elements if necessary.
37
92
#[ doc( hidden) ]
38
93
fn into_owned < D > ( self_ : ArrayBase < Self , D > ) -> ArrayBase < OwnedRepr < Self :: Elem > , D >
39
94
where
40
95
Self :: Elem : Clone ,
41
96
D : Dimension ;
42
-
43
- private_decl ! { }
44
97
}
45
98
46
99
/// Array representation trait.
47
100
///
48
- /// For an array with writable elements.
101
+ /// For an array with writable elements that can be accessed with safe code .
49
102
///
50
103
/// ***Internal trait, see `Data`.***
51
- pub unsafe trait DataMut : Data {
104
+ //
105
+ // # For implementers
106
+ //
107
+ // If you implement the `DataMut` trait, you are guaranteeing that the
108
+ // `RawDataMut::try_ensure_unique` implementation always panics or ensures that
109
+ // the data is unique. You are also guaranteeing that `try_is_unique` always
110
+ // returns `Some(_)`.
111
+ pub unsafe trait DataMut : Data + RawDataMut {
112
+ /// Ensures that the array has unique access to its data.
52
113
#[ doc( hidden) ]
53
114
#[ inline]
54
- fn ensure_unique < D > ( & mut ArrayBase < Self , D > )
55
- where Self : Sized ,
56
- D : Dimension
57
- { }
115
+ fn ensure_unique < D > ( self_ : & mut ArrayBase < Self , D > )
116
+ where Self : Sized ,
117
+ D : Dimension
118
+ {
119
+ Self :: try_ensure_unique ( self_)
120
+ }
58
121
122
+ /// Returns whether the array has unique access to its data.
59
123
#[ doc( hidden) ]
60
124
#[ inline]
61
125
fn is_unique ( & mut self ) -> bool {
62
- true
126
+ self . try_is_unique ( ) . unwrap ( )
63
127
}
64
128
}
65
129
66
130
/// Array representation trait.
67
131
///
68
- /// 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.
69
134
///
70
135
/// ***Internal trait, see `Data`.***
71
- pub unsafe trait DataClone : Data {
72
- #[ doc( hidden) ]
73
- /// Unsafe because, `ptr` must point inside the current storage.
74
- unsafe fn clone_with_ptr ( & self , ptr : * mut Self :: Elem ) -> ( Self , * mut Self :: Elem ) ;
136
+ #[ deprecated( note="use `Data + RawDataClone` instead" , since="0.13" ) ]
137
+ pub trait DataClone : Data + RawDataClone { }
75
138
76
- #[ doc( hidden) ]
77
- unsafe fn clone_from_with_ptr ( & mut self , other : & Self , ptr : * mut Self :: Elem ) -> * mut Self :: Elem {
78
- let ( data, ptr) = other. clone_with_ptr ( ptr) ;
79
- * self = data;
80
- ptr
139
+ #[ allow( deprecated) ]
140
+ impl < T > DataClone for T where T : Data + RawDataClone { }
141
+
142
+ unsafe impl < A > RawData for RawViewRepr < * const A > {
143
+ type Elem = A ;
144
+ fn _data_slice ( & self ) -> Option < & [ A ] > {
145
+ None
81
146
}
147
+ private_impl ! { }
82
148
}
83
149
84
- unsafe impl < A > Data for OwnedArcRepr < A > {
150
+ unsafe impl < A > RawDataClone for RawViewRepr < * const A > {
151
+ unsafe fn clone_with_ptr ( & self , ptr : * mut Self :: Elem ) -> ( Self , * mut Self :: Elem ) {
152
+ ( * self , ptr)
153
+ }
154
+ }
155
+
156
+ unsafe impl < A > RawData for RawViewRepr < * mut A > {
85
157
type Elem = A ;
86
- fn _data_slice ( & self ) -> & [ A ] {
87
- & self . 0
158
+ fn _data_slice ( & self ) -> Option < & [ A ] > {
159
+ None
88
160
}
89
- fn into_owned < D > ( mut self_ : ArrayBase < Self , D > ) -> ArrayBase < OwnedRepr < Self :: Elem > , D >
90
- where
91
- A : Clone ,
92
- D : Dimension ,
93
- {
94
- Self :: ensure_unique ( & mut self_) ;
95
- let data = OwnedRepr ( Arc :: try_unwrap ( self_. data . 0 ) . ok ( ) . unwrap ( ) ) ;
96
- ArrayBase {
97
- data : data,
98
- ptr : self_. ptr ,
99
- dim : self_. dim ,
100
- strides : self_. strides ,
101
- }
161
+ private_impl ! { }
162
+ }
163
+
164
+ unsafe impl < A > RawDataMut for RawViewRepr < * mut A > {
165
+ #[ inline]
166
+ fn try_ensure_unique < D > ( _: & mut ArrayBase < Self , D > )
167
+ where Self : Sized ,
168
+ D : Dimension
169
+ { }
170
+
171
+ #[ inline]
172
+ fn try_is_unique ( & mut self ) -> Option < bool > {
173
+ None
174
+ }
175
+ }
176
+
177
+ unsafe impl < A > RawDataClone for RawViewRepr < * mut A > {
178
+ unsafe fn clone_with_ptr ( & self , ptr : * mut Self :: Elem ) -> ( Self , * mut Self :: Elem ) {
179
+ ( * self , ptr)
180
+ }
181
+ }
182
+
183
+ unsafe impl < A > RawData for OwnedArcRepr < A > {
184
+ type Elem = A ;
185
+ fn _data_slice ( & self ) -> Option < & [ A ] > {
186
+ Some ( & self . 0 )
102
187
}
103
188
private_impl ! { }
104
189
}
105
190
106
191
// NOTE: Copy on write
107
- unsafe impl < A > DataMut for OwnedArcRepr < A >
108
- where A : Clone
192
+ unsafe impl < A > RawDataMut for OwnedArcRepr < A >
193
+ where
194
+ A : Clone ,
109
195
{
110
- fn ensure_unique < D > ( self_ : & mut ArrayBase < Self , D > )
196
+ fn try_ensure_unique < D > ( self_ : & mut ArrayBase < Self , D > )
111
197
where Self : Sized ,
112
198
D : Dimension
113
199
{
@@ -136,23 +222,59 @@ unsafe impl<A> DataMut for OwnedArcRepr<A>
136
222
}
137
223
}
138
224
139
- fn is_unique ( & mut self ) -> bool {
140
- Arc :: get_mut ( & mut self . 0 ) . is_some ( )
225
+ fn try_is_unique ( & mut self ) -> Option < bool > {
226
+ Some ( Arc :: get_mut ( & mut self . 0 ) . is_some ( ) )
141
227
}
142
228
}
143
229
144
- unsafe impl < A > DataClone for OwnedArcRepr < A > {
230
+ unsafe impl < A > Data for OwnedArcRepr < A > {
231
+ fn into_owned < D > ( mut self_ : ArrayBase < Self , D > ) -> ArrayBase < OwnedRepr < Self :: Elem > , D >
232
+ where
233
+ A : Clone ,
234
+ D : Dimension ,
235
+ {
236
+ Self :: ensure_unique ( & mut self_) ;
237
+ let data = OwnedRepr ( Arc :: try_unwrap ( self_. data . 0 ) . ok ( ) . unwrap ( ) ) ;
238
+ ArrayBase {
239
+ data : data,
240
+ ptr : self_. ptr ,
241
+ dim : self_. dim ,
242
+ strides : self_. strides ,
243
+ }
244
+ }
245
+ }
246
+
247
+ unsafe impl < A > DataMut for OwnedArcRepr < A > where A : Clone { }
248
+
249
+ unsafe impl < A > RawDataClone for OwnedArcRepr < A > {
145
250
unsafe fn clone_with_ptr ( & self , ptr : * mut Self :: Elem ) -> ( Self , * mut Self :: Elem ) {
146
251
// pointer is preserved
147
252
( self . clone ( ) , ptr)
148
253
}
149
254
}
150
255
151
- unsafe impl < A > Data for OwnedRepr < A > {
256
+ unsafe impl < A > RawData for OwnedRepr < A > {
152
257
type Elem = A ;
153
- fn _data_slice ( & self ) -> & [ A ] {
154
- & self . 0
258
+ fn _data_slice ( & self ) -> Option < & [ A ] > {
259
+ Some ( & self . 0 )
155
260
}
261
+ private_impl ! { }
262
+ }
263
+
264
+ unsafe impl < A > RawDataMut for OwnedRepr < A > {
265
+ #[ inline]
266
+ fn try_ensure_unique < D > ( _: & mut ArrayBase < Self , D > )
267
+ where Self : Sized ,
268
+ D : Dimension
269
+ { }
270
+
271
+ #[ inline]
272
+ fn try_is_unique ( & mut self ) -> Option < bool > {
273
+ Some ( true )
274
+ }
275
+ }
276
+
277
+ unsafe impl < A > Data for OwnedRepr < A > {
156
278
#[ inline]
157
279
fn into_owned < D > ( self_ : ArrayBase < Self , D > ) -> ArrayBase < OwnedRepr < Self :: Elem > , D >
158
280
where
@@ -161,12 +283,11 @@ unsafe impl<A> Data for OwnedRepr<A> {
161
283
{
162
284
self_
163
285
}
164
- private_impl ! { }
165
286
}
166
287
167
288
unsafe impl < A > DataMut for OwnedRepr < A > { }
168
289
169
- unsafe impl < A > DataClone for OwnedRepr < A >
290
+ unsafe impl < A > RawDataClone for OwnedRepr < A >
170
291
where A : Clone
171
292
{
172
293
unsafe fn clone_with_ptr ( & self , ptr : * mut Self :: Elem ) -> ( Self , * mut Self :: Elem ) {
@@ -192,40 +313,58 @@ unsafe impl<A> DataClone for OwnedRepr<A>
192
313
}
193
314
}
194
315
195
- unsafe impl < ' a , A > Data for ViewRepr < & ' a A > {
316
+ unsafe impl < ' a , A > RawData for ViewRepr < & ' a A > {
196
317
type Elem = A ;
197
- fn _data_slice ( & self ) -> & [ A ] {
198
- & [ ]
318
+ fn _data_slice ( & self ) -> Option < & [ A ] > {
319
+ None
199
320
}
321
+ private_impl ! { }
322
+ }
323
+
324
+ unsafe impl < ' a , A > Data for ViewRepr < & ' a A > {
200
325
fn into_owned < D > ( self_ : ArrayBase < Self , D > ) -> ArrayBase < OwnedRepr < Self :: Elem > , D >
201
326
where
202
327
Self :: Elem : Clone ,
203
328
D : Dimension ,
204
329
{
205
330
self_. to_owned ( )
206
331
}
207
- private_impl ! { }
208
332
}
209
333
210
- unsafe impl < ' a , A > DataClone for ViewRepr < & ' a A > {
334
+ unsafe impl < ' a , A > RawDataClone for ViewRepr < & ' a A > {
211
335
unsafe fn clone_with_ptr ( & self , ptr : * mut Self :: Elem ) -> ( Self , * mut Self :: Elem ) {
212
336
( * self , ptr)
213
337
}
214
338
}
215
339
216
- unsafe impl < ' a , A > Data for ViewRepr < & ' a mut A > {
340
+ unsafe impl < ' a , A > RawData for ViewRepr < & ' a mut A > {
217
341
type Elem = A ;
218
- fn _data_slice ( & self ) -> & [ A ] {
219
- & [ ]
342
+ fn _data_slice ( & self ) -> Option < & [ A ] > {
343
+ None
220
344
}
345
+ private_impl ! { }
346
+ }
347
+
348
+ unsafe impl < ' a , A > RawDataMut for ViewRepr < & ' a mut A > {
349
+ #[ inline]
350
+ fn try_ensure_unique < D > ( _: & mut ArrayBase < Self , D > )
351
+ where Self : Sized ,
352
+ D : Dimension { }
353
+
354
+ #[ inline]
355
+ fn try_is_unique ( & mut self ) -> Option < bool > {
356
+ Some ( true )
357
+ }
358
+ }
359
+
360
+ unsafe impl < ' a , A > Data for ViewRepr < & ' a mut A > {
221
361
fn into_owned < D > ( self_ : ArrayBase < Self , D > ) -> ArrayBase < OwnedRepr < Self :: Elem > , D >
222
362
where
223
363
Self :: Elem : Clone ,
224
364
D : Dimension ,
225
365
{
226
366
self_. to_owned ( )
227
367
}
228
- private_impl ! { }
229
368
}
230
369
231
370
unsafe impl < ' a , A > DataMut for ViewRepr < & ' a mut A > { }
@@ -250,7 +389,7 @@ pub unsafe trait DataOwned : Data {
250
389
/// A representation that is a lightweight view.
251
390
///
252
391
/// ***Internal trait, see `Data`.***
253
- pub unsafe trait DataShared : Clone + DataClone { }
392
+ pub unsafe trait DataShared : Clone + Data + RawDataClone { }
254
393
255
394
unsafe impl < A > DataShared for OwnedRcRepr < A > { }
256
395
unsafe impl < ' a , A > DataShared for ViewRepr < & ' a A > { }
0 commit comments