@@ -85,6 +85,7 @@ pub(crate) unsafe fn with_objects<T: Message + ?Sized, R: Message, O: Ownership>
85
85
}
86
86
}
87
87
88
+ /// Generic creation methods.
88
89
impl < T : Message , O : Ownership > NSArray < T , O > {
89
90
/// Get an empty array.
90
91
pub fn new ( ) -> Id < Self , Shared > {
@@ -96,8 +97,39 @@ impl<T: Message, O: Ownership> NSArray<T, O> {
96
97
// hence the notion of ownership over the elements is void.
97
98
unsafe { msg_send_id ! [ Self :: class( ) , new] . expect ( "unexpected NULL NSArray" ) }
98
99
}
100
+
101
+ pub fn from_vec ( vec : Vec < Id < T , O > > ) -> Id < Self , O > {
102
+ // SAFETY:
103
+ // `initWithObjects:` may choose to deduplicate arrays (I could
104
+ // imagine it having a special case for arrays with one `NSNumber`
105
+ // object), and returning mutable references to those would be
106
+ // unsound!
107
+ // However, when we know that we have ownership over the variables, we
108
+ // also know that there cannot be another array in existence with the
109
+ // same objects, so `Id<NSArray<T, Owned>, Owned>` is safe to return.
110
+ //
111
+ // In essence, we can choose between always returning `Id<T, Shared>`
112
+ // or `Id<T, O>`, and the latter is probably the most useful, as we
113
+ // would like to know when we're the only owner of the array, to
114
+ // allow mutation of the array's items.
115
+ unsafe { with_objects ( Self :: class ( ) , vec. as_slice_ref ( ) ) }
116
+ }
117
+ }
118
+
119
+ /// Creation methods that produce shared arrays.
120
+ impl < T : Message > NSArray < T , Shared > {
121
+ pub fn from_slice ( slice : & [ Id < T , Shared > ] ) -> Id < Self , Shared > {
122
+ // SAFETY: Taking `&T` would not be sound, since the `&T` could come
123
+ // from an `Id<T, Owned>` that would now no longer be owned!
124
+ //
125
+ // (Note that NSArray internally retains all the objects it is given,
126
+ // effectively making the safety requirements the same as
127
+ // `Id::retain`).
128
+ unsafe { with_objects ( Self :: class ( ) , slice. as_slice_ref ( ) ) }
129
+ }
99
130
}
100
131
132
+ /// Generic accessor methods.
101
133
impl < T : Message , O : Ownership > NSArray < T , O > {
102
134
#[ doc( alias = "count" ) ]
103
135
pub fn len ( & self ) -> usize {
@@ -137,13 +169,6 @@ impl<T: Message, O: Ownership> NSArray<T, O> {
137
169
}
138
170
}
139
171
140
- // The `NSArray` itself (length and number of items) is always immutable,
141
- // but we would like to know when we're the only owner of the array, to
142
- // allow mutation of the array's items.
143
- pub fn from_vec ( vec : Vec < Id < T , O > > ) -> Id < Self , O > {
144
- unsafe { with_objects ( Self :: class ( ) , vec. as_slice_ref ( ) ) }
145
- }
146
-
147
172
pub fn objects_in_range ( & self , range : Range < usize > ) -> Vec < & T > {
148
173
let range = NSRange :: from ( range) ;
149
174
let mut vec = Vec :: with_capacity ( range. length ) ;
@@ -168,11 +193,8 @@ impl<T: Message, O: Ownership> NSArray<T, O> {
168
193
}
169
194
}
170
195
196
+ /// Accessor methods that work on shared arrays.
171
197
impl < T : Message > NSArray < T , Shared > {
172
- pub fn from_slice ( slice : & [ Id < T , Shared > ] ) -> Id < Self , Shared > {
173
- unsafe { with_objects ( Self :: class ( ) , slice. as_slice_ref ( ) ) }
174
- }
175
-
176
198
#[ doc( alias = "objectAtIndex:" ) ]
177
199
pub fn get_retained ( & self , index : usize ) -> Id < T , Shared > {
178
200
let obj = self . get ( index) . unwrap ( ) ;
@@ -188,6 +210,7 @@ impl<T: Message> NSArray<T, Shared> {
188
210
}
189
211
}
190
212
213
+ /// Accessor methods that work on owned arrays.
191
214
impl < T : Message > NSArray < T , Owned > {
192
215
#[ doc( alias = "objectAtIndex:" ) ]
193
216
pub fn get_mut ( & mut self , index : usize ) -> Option < & mut T > {
@@ -211,13 +234,15 @@ impl<T: Message> NSArray<T, Owned> {
211
234
}
212
235
}
213
236
214
- // Copying only possible when ItemOwnership = Shared
215
-
237
+ /// This is implemented as a shallow copy.
238
+ ///
239
+ /// As such, it is only possible when the array's contents are `Shared`.
216
240
unsafe impl < T : Message > NSCopying for NSArray < T , Shared > {
217
241
type Ownership = Shared ;
218
242
type Output = NSArray < T , Shared > ;
219
243
}
220
244
245
+ /// This is implemented as a shallow copy.
221
246
unsafe impl < T : Message > NSMutableCopying for NSArray < T , Shared > {
222
247
type Output = NSMutableArray < T , Shared > ;
223
248
}
0 commit comments