@@ -14,6 +14,7 @@ use std::sync::Arc;
1414use {
1515 ArrayBase ,
1616 Dimension ,
17+ RawViewRepr ,
1718 ViewRepr ,
1819 OwnedRepr ,
1920 OwnedRcRepr ,
@@ -22,92 +23,177 @@ use {
2223
2324/// Array representation trait.
2425///
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.
2631/// Traits in Rust can serve many different roles. This trait is public because
2732/// it is used as a bound on public methods.
28- pub unsafe trait Data : Sized {
33+ pub unsafe trait RawData : Sized {
2934 /// The array element type.
3035 type Elem ;
3136
3237 #[ doc( hidden) ]
3338 // 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 ;
3558
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 {
3691 /// Converts the array to a uniquely owned array, cloning elements if necessary.
3792 #[ doc( hidden) ]
3893 fn into_owned < D > ( self_ : ArrayBase < Self , D > ) -> ArrayBase < OwnedRepr < Self :: Elem > , D >
3994 where
4095 Self :: Elem : Clone ,
4196 D : Dimension ;
42-
43- private_decl ! { }
4497}
4598
4699/// Array representation trait.
47100///
48- /// For an array with writable elements.
101+ /// For an array with writable elements that can be accessed with safe code .
49102///
50103/// ***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.
52113 #[ doc( hidden) ]
53114 #[ 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+ }
58121
122+ /// Returns whether the array has unique access to its data.
59123 #[ doc( hidden) ]
60124 #[ inline]
61125 fn is_unique ( & mut self ) -> bool {
62- true
126+ self . try_is_unique ( ) . unwrap ( )
63127 }
64128}
65129
66130/// Array representation trait.
67131///
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.
69134///
70135/// ***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 { }
75138
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
81146 }
147+ private_impl ! { }
82148}
83149
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 > {
85157 type Elem = A ;
86- fn _data_slice ( & self ) -> & [ A ] {
87- & self . 0
158+ fn _data_slice ( & self ) -> Option < & [ A ] > {
159+ None
88160 }
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 )
102187 }
103188 private_impl ! { }
104189}
105190
106191// 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 ,
109195{
110- fn ensure_unique < D > ( self_ : & mut ArrayBase < Self , D > )
196+ fn try_ensure_unique < D > ( self_ : & mut ArrayBase < Self , D > )
111197 where Self : Sized ,
112198 D : Dimension
113199 {
@@ -136,23 +222,59 @@ unsafe impl<A> DataMut for OwnedArcRepr<A>
136222 }
137223 }
138224
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 ( ) )
141227 }
142228}
143229
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 > {
145250 unsafe fn clone_with_ptr ( & self , ptr : * mut Self :: Elem ) -> ( Self , * mut Self :: Elem ) {
146251 // pointer is preserved
147252 ( self . clone ( ) , ptr)
148253 }
149254}
150255
151- unsafe impl < A > Data for OwnedRepr < A > {
256+ unsafe impl < A > RawData for OwnedRepr < A > {
152257 type Elem = A ;
153- fn _data_slice ( & self ) -> & [ A ] {
154- & self . 0
258+ fn _data_slice ( & self ) -> Option < & [ A ] > {
259+ Some ( & self . 0 )
155260 }
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 > {
156278 #[ inline]
157279 fn into_owned < D > ( self_ : ArrayBase < Self , D > ) -> ArrayBase < OwnedRepr < Self :: Elem > , D >
158280 where
@@ -161,12 +283,11 @@ unsafe impl<A> Data for OwnedRepr<A> {
161283 {
162284 self_
163285 }
164- private_impl ! { }
165286}
166287
167288unsafe impl < A > DataMut for OwnedRepr < A > { }
168289
169- unsafe impl < A > DataClone for OwnedRepr < A >
290+ unsafe impl < A > RawDataClone for OwnedRepr < A >
170291 where A : Clone
171292{
172293 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>
192313 }
193314}
194315
195- unsafe impl < ' a , A > Data for ViewRepr < & ' a A > {
316+ unsafe impl < ' a , A > RawData for ViewRepr < & ' a A > {
196317 type Elem = A ;
197- fn _data_slice ( & self ) -> & [ A ] {
198- & [ ]
318+ fn _data_slice ( & self ) -> Option < & [ A ] > {
319+ None
199320 }
321+ private_impl ! { }
322+ }
323+
324+ unsafe impl < ' a , A > Data for ViewRepr < & ' a A > {
200325 fn into_owned < D > ( self_ : ArrayBase < Self , D > ) -> ArrayBase < OwnedRepr < Self :: Elem > , D >
201326 where
202327 Self :: Elem : Clone ,
203328 D : Dimension ,
204329 {
205330 self_. to_owned ( )
206331 }
207- private_impl ! { }
208332}
209333
210- unsafe impl < ' a , A > DataClone for ViewRepr < & ' a A > {
334+ unsafe impl < ' a , A > RawDataClone for ViewRepr < & ' a A > {
211335 unsafe fn clone_with_ptr ( & self , ptr : * mut Self :: Elem ) -> ( Self , * mut Self :: Elem ) {
212336 ( * self , ptr)
213337 }
214338}
215339
216- unsafe impl < ' a , A > Data for ViewRepr < & ' a mut A > {
340+ unsafe impl < ' a , A > RawData for ViewRepr < & ' a mut A > {
217341 type Elem = A ;
218- fn _data_slice ( & self ) -> & [ A ] {
219- & [ ]
342+ fn _data_slice ( & self ) -> Option < & [ A ] > {
343+ None
220344 }
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 > {
221361 fn into_owned < D > ( self_ : ArrayBase < Self , D > ) -> ArrayBase < OwnedRepr < Self :: Elem > , D >
222362 where
223363 Self :: Elem : Clone ,
224364 D : Dimension ,
225365 {
226366 self_. to_owned ( )
227367 }
228- private_impl ! { }
229368}
230369
231370unsafe impl < ' a , A > DataMut for ViewRepr < & ' a mut A > { }
@@ -250,7 +389,7 @@ pub unsafe trait DataOwned : Data {
250389/// A representation that is a lightweight view.
251390///
252391/// ***Internal trait, see `Data`.***
253- pub unsafe trait DataShared : Clone + DataClone { }
392+ pub unsafe trait DataShared : Clone + Data + RawDataClone { }
254393
255394unsafe impl < A > DataShared for OwnedRcRepr < A > { }
256395unsafe impl < ' a , A > DataShared for ViewRepr < & ' a A > { }
0 commit comments