@@ -22,44 +22,89 @@ use {
22
22
23
23
/// Array representation trait.
24
24
///
25
- /// ***Note:*** `Data` is not an extension interface at this point.
25
+ /// For an array that meets the invariants of the `ArrayBase` type. This trait
26
+ /// does not imply any ownership or lifetime; pointers to elements in the array
27
+ /// may not be safe to dereference.
28
+ ///
29
+ /// ***Note:*** `DataRaw` is not an extension interface at this point.
26
30
/// Traits in Rust can serve many different roles. This trait is public because
27
31
/// it is used as a bound on public methods.
28
- pub unsafe trait Data : Sized {
32
+ pub unsafe trait DataRaw : Sized {
29
33
/// The array element type.
30
34
type Elem ;
31
35
32
36
#[ doc( hidden) ]
33
37
// This method is only used for debugging
34
- fn _data_slice ( & self ) -> & [ Self :: Elem ] ;
38
+ fn _data_slice ( & self ) -> Option < & [ Self :: Elem ] > ;
39
+
40
+ private_decl ! { }
41
+ }
42
+
43
+ /// Array representation trait.
44
+ ///
45
+ /// For an array with writable elements.
46
+ ///
47
+ /// ***Internal trait, see `DataRaw`.***
48
+ pub unsafe trait DataRawMut : DataRaw {
49
+ /// If possible, ensures that the array has unique access to its data.
50
+ ///
51
+ /// If `Self` provides safe mutable access to array elements, then it
52
+ /// **must** panic or ensure that the data is unique.
53
+ #[ doc( hidden) ]
54
+ fn try_ensure_unique < D > ( & mut ArrayBase < Self , D > )
55
+ where Self : Sized ,
56
+ D : Dimension ;
35
57
58
+ /// If possible, returns whether the array has unique access to its data.
59
+ ///
60
+ /// If `Self` provides safe mutable access to array elements, then it
61
+ /// **must** return `Some(_)`.
62
+ #[ doc( hidden) ]
63
+ fn try_is_unique ( & mut self ) -> Option < bool > ;
64
+ }
65
+
66
+ /// Array representation trait.
67
+ ///
68
+ /// For an array with elements that can be accessed with safe code.
69
+ ///
70
+ /// ***Internal trait, see `DataRaw`.***
71
+ pub unsafe trait Data : DataRaw {
36
72
/// Converts the array to a uniquely owned array, cloning elements if necessary.
37
73
#[ doc( hidden) ]
38
74
fn into_owned < D > ( self_ : ArrayBase < Self , D > ) -> ArrayBase < OwnedRepr < Self :: Elem > , D >
39
75
where
40
76
Self :: Elem : Clone ,
41
77
D : Dimension ;
42
-
43
- private_decl ! { }
44
78
}
45
79
46
80
/// Array representation trait.
47
81
///
48
- /// For an array with writable elements.
82
+ /// For an array with writable elements that can be accessed with safe code .
49
83
///
50
84
/// ***Internal trait, see `Data`.***
51
- pub unsafe trait DataMut : Data {
85
+ //
86
+ // # For implementers
87
+ //
88
+ // If you implement the `DataMut` trait, you are guaranteeing that the
89
+ // `DataRawMut::try_ensure_unique` implementation always panics or ensures that
90
+ // the data is unique. You are also guaranteeing that `try_is_unique` always
91
+ // returns `Some(_)`.
92
+ pub unsafe trait DataMut : Data + DataRawMut {
93
+ /// Ensures that the array has unique access to its data.
52
94
#[ doc( hidden) ]
53
95
#[ inline]
54
- fn ensure_unique < D > ( & mut ArrayBase < Self , D > )
55
- where Self : Sized ,
56
- D : Dimension
57
- { }
96
+ fn ensure_unique < D > ( self_ : & mut ArrayBase < Self , D > )
97
+ where Self : Sized ,
98
+ D : Dimension
99
+ {
100
+ Self :: try_ensure_unique ( self_)
101
+ }
58
102
103
+ /// Returns whether the array has unique access to its data.
59
104
#[ doc( hidden) ]
60
105
#[ inline]
61
106
fn is_unique ( & mut self ) -> bool {
62
- true
107
+ self . try_is_unique ( ) . unwrap ( )
63
108
}
64
109
}
65
110
@@ -81,33 +126,20 @@ pub unsafe trait DataClone : Data {
81
126
}
82
127
}
83
128
84
- unsafe impl < A > Data for OwnedArcRepr < A > {
129
+ unsafe impl < A > DataRaw for OwnedArcRepr < A > {
85
130
type Elem = A ;
86
- fn _data_slice ( & self ) -> & [ A ] {
87
- & self . 0
88
- }
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
- }
131
+ fn _data_slice ( & self ) -> Option < & [ A ] > {
132
+ Some ( & self . 0 )
102
133
}
103
134
private_impl ! { }
104
135
}
105
136
106
137
// NOTE: Copy on write
107
- unsafe impl < A > DataMut for OwnedArcRepr < A >
108
- where A : Clone
138
+ unsafe impl < A > DataRawMut for OwnedArcRepr < A >
139
+ where
140
+ A : Clone ,
109
141
{
110
- fn ensure_unique < D > ( self_ : & mut ArrayBase < Self , D > )
142
+ fn try_ensure_unique < D > ( self_ : & mut ArrayBase < Self , D > )
111
143
where Self : Sized ,
112
144
D : Dimension
113
145
{
@@ -136,23 +168,59 @@ unsafe impl<A> DataMut for OwnedArcRepr<A>
136
168
}
137
169
}
138
170
139
- fn is_unique ( & mut self ) -> bool {
140
- Arc :: get_mut ( & mut self . 0 ) . is_some ( )
171
+ fn try_is_unique ( & mut self ) -> Option < bool > {
172
+ Some ( Arc :: get_mut ( & mut self . 0 ) . is_some ( ) )
173
+ }
174
+ }
175
+
176
+ unsafe impl < A > Data for OwnedArcRepr < A > {
177
+ fn into_owned < D > ( mut self_ : ArrayBase < Self , D > ) -> ArrayBase < OwnedRepr < Self :: Elem > , D >
178
+ where
179
+ A : Clone ,
180
+ D : Dimension ,
181
+ {
182
+ Self :: ensure_unique ( & mut self_) ;
183
+ let data = OwnedRepr ( Arc :: try_unwrap ( self_. data . 0 ) . ok ( ) . unwrap ( ) ) ;
184
+ ArrayBase {
185
+ data : data,
186
+ ptr : self_. ptr ,
187
+ dim : self_. dim ,
188
+ strides : self_. strides ,
189
+ }
141
190
}
142
191
}
143
192
193
+ unsafe impl < A > DataMut for OwnedArcRepr < A > where A : Clone { }
194
+
144
195
unsafe impl < A > DataClone for OwnedArcRepr < A > {
145
196
unsafe fn clone_with_ptr ( & self , ptr : * mut Self :: Elem ) -> ( Self , * mut Self :: Elem ) {
146
197
// pointer is preserved
147
198
( self . clone ( ) , ptr)
148
199
}
149
200
}
150
201
151
- unsafe impl < A > Data for OwnedRepr < A > {
202
+ unsafe impl < A > DataRaw for OwnedRepr < A > {
152
203
type Elem = A ;
153
- fn _data_slice ( & self ) -> & [ A ] {
154
- & self . 0
204
+ fn _data_slice ( & self ) -> Option < & [ A ] > {
205
+ Some ( & self . 0 )
206
+ }
207
+ private_impl ! { }
208
+ }
209
+
210
+ unsafe impl < A > DataRawMut for OwnedRepr < A > {
211
+ #[ inline]
212
+ fn try_ensure_unique < D > ( _: & mut ArrayBase < Self , D > )
213
+ where Self : Sized ,
214
+ D : Dimension
215
+ { }
216
+
217
+ #[ inline]
218
+ fn try_is_unique ( & mut self ) -> Option < bool > {
219
+ Some ( true )
155
220
}
221
+ }
222
+
223
+ unsafe impl < A > Data for OwnedRepr < A > {
156
224
#[ inline]
157
225
fn into_owned < D > ( self_ : ArrayBase < Self , D > ) -> ArrayBase < OwnedRepr < Self :: Elem > , D >
158
226
where
@@ -161,7 +229,6 @@ unsafe impl<A> Data for OwnedRepr<A> {
161
229
{
162
230
self_
163
231
}
164
- private_impl ! { }
165
232
}
166
233
167
234
unsafe impl < A > DataMut for OwnedRepr < A > { }
@@ -192,19 +259,22 @@ unsafe impl<A> DataClone for OwnedRepr<A>
192
259
}
193
260
}
194
261
195
- unsafe impl < ' a , A > Data for ViewRepr < & ' a A > {
262
+ unsafe impl < ' a , A > DataRaw for ViewRepr < & ' a A > {
196
263
type Elem = A ;
197
- fn _data_slice ( & self ) -> & [ A ] {
198
- & [ ]
264
+ fn _data_slice ( & self ) -> Option < & [ A ] > {
265
+ None
199
266
}
267
+ private_impl ! { }
268
+ }
269
+
270
+ unsafe impl < ' a , A > Data for ViewRepr < & ' a A > {
200
271
fn into_owned < D > ( self_ : ArrayBase < Self , D > ) -> ArrayBase < OwnedRepr < Self :: Elem > , D >
201
272
where
202
273
Self :: Elem : Clone ,
203
274
D : Dimension ,
204
275
{
205
276
self_. to_owned ( )
206
277
}
207
- private_impl ! { }
208
278
}
209
279
210
280
unsafe impl < ' a , A > DataClone for ViewRepr < & ' a A > {
@@ -213,19 +283,34 @@ unsafe impl<'a, A> DataClone for ViewRepr<&'a A> {
213
283
}
214
284
}
215
285
216
- unsafe impl < ' a , A > Data for ViewRepr < & ' a mut A > {
286
+ unsafe impl < ' a , A > DataRaw for ViewRepr < & ' a mut A > {
217
287
type Elem = A ;
218
- fn _data_slice ( & self ) -> & [ A ] {
219
- & [ ]
288
+ fn _data_slice ( & self ) -> Option < & [ A ] > {
289
+ None
220
290
}
291
+ private_impl ! { }
292
+ }
293
+
294
+ unsafe impl < ' a , A > DataRawMut for ViewRepr < & ' a mut A > {
295
+ #[ inline]
296
+ fn try_ensure_unique < D > ( _: & mut ArrayBase < Self , D > )
297
+ where Self : Sized ,
298
+ D : Dimension { }
299
+
300
+ #[ inline]
301
+ fn try_is_unique ( & mut self ) -> Option < bool > {
302
+ Some ( true )
303
+ }
304
+ }
305
+
306
+ unsafe impl < ' a , A > Data for ViewRepr < & ' a mut A > {
221
307
fn into_owned < D > ( self_ : ArrayBase < Self , D > ) -> ArrayBase < OwnedRepr < Self :: Elem > , D >
222
308
where
223
309
Self :: Elem : Clone ,
224
310
D : Dimension ,
225
311
{
226
312
self_. to_owned ( )
227
313
}
228
- private_impl ! { }
229
314
}
230
315
231
316
unsafe impl < ' a , A > DataMut for ViewRepr < & ' a mut A > { }
0 commit comments