Skip to content

Commit 522f93b

Browse files
committed
Use Id conversion traits on NSArray
1 parent 9c95e19 commit 522f93b

3 files changed

Lines changed: 69 additions & 37 deletions

File tree

objc2-foundation/examples/basic_usage.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use objc2::rc::autoreleasepool;
1+
use objc2::rc::{autoreleasepool, FromId, Id, IntoId};
22
use objc2_foundation::{
33
INSArray, INSCopying, INSDictionary, INSString, NSArray, NSDictionary, NSObject, NSString,
44
};
@@ -13,14 +13,14 @@ fn main() {
1313

1414
// Create an NSArray from a Vec
1515
let objs = vec![obj, obj2];
16-
let array = NSArray::from_vec(objs);
16+
let array: Id<NSArray<_, _>, _> = objs.into_id();
1717
for obj in array.iter() {
1818
println!("{:?}", obj);
1919
}
2020
println!("{}", array.len());
2121

2222
// Turn the NSArray back into a Vec
23-
let mut objs = NSArray::into_vec(array);
23+
let mut objs = Vec::from_id(array);
2424
let obj = objs.pop().unwrap();
2525

2626
// Create an NSString from a str slice

objc2-foundation/src/array.rs

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,6 @@ pub unsafe trait INSArray: INSObject {
112112
}
113113
}
114114

115-
fn from_vec(vec: Vec<Id<Self::Item, Self::ItemOwnership>>) -> Id<Self, Self::Ownership> {
116-
unsafe { from_refs(vec.as_slice_ref()) }
117-
}
118-
119115
fn objects_in_range(&self, range: Range<usize>) -> Vec<&Self::Item> {
120116
let range = NSRange::from(range);
121117
let mut vec = Vec::with_capacity(range.length);
@@ -126,35 +122,12 @@ pub unsafe trait INSArray: INSObject {
126122
vec
127123
}
128124

129-
fn to_vec(&self) -> Vec<&Self::Item> {
130-
self.objects_in_range(0..self.len())
131-
}
132-
133-
// TODO: Take Id<Self, Self::ItemOwnership> ?
134-
fn into_vec(array: Id<Self, Owned>) -> Vec<Id<Self::Item, Self::ItemOwnership>> {
135-
array
136-
.to_vec()
137-
.into_iter()
138-
.map(|obj| unsafe { Id::retain(obj.into()) })
139-
.collect()
140-
}
141-
142125
fn from_slice(slice: &[Id<Self::Item, Shared>]) -> Id<Self, Self::Ownership>
143126
where
144127
Self: INSArray<ItemOwnership = Shared>,
145128
{
146129
unsafe { from_refs(slice.as_slice_ref()) }
147130
}
148-
149-
fn to_shared_vec(&self) -> Vec<Id<Self::Item, Shared>>
150-
where
151-
Self: INSArray<ItemOwnership = Shared>,
152-
{
153-
self.to_vec()
154-
.into_iter()
155-
.map(|obj| unsafe { Id::retain(obj.into()) })
156-
.collect()
157-
}
158131
}
159132

160133
/// TODO
@@ -211,6 +184,52 @@ impl<T: INSObject, O: Ownership> Index<usize> for NSArray<T, O> {
211184
}
212185
}
213186

187+
impl<T, O> FromId<NSArray<T, O>, Owned> for Vec<Id<T, O>>
188+
where
189+
T: INSObject,
190+
O: Ownership,
191+
{
192+
fn from_id(array: Id<NSArray<T, O>, Owned>) -> Self {
193+
let vec: Vec<&T> = (&*array).into();
194+
vec.into_iter()
195+
.map(|obj| unsafe { Id::retain(obj.into()) })
196+
.collect()
197+
}
198+
}
199+
200+
impl<T, O> IntoId<NSArray<T, O>, Owned> for Vec<Id<T, O>>
201+
where
202+
T: INSObject,
203+
O: Ownership,
204+
{
205+
fn into_id(self) -> Id<NSArray<T, O>, Owned> {
206+
unsafe { from_refs(self.as_slice_ref()) }
207+
}
208+
}
209+
210+
impl<'a, T, O> From<&'a NSArray<T, O>> for Vec<&'a T>
211+
where
212+
T: INSObject,
213+
O: Ownership,
214+
{
215+
fn from(array: &'a NSArray<T, O>) -> Self {
216+
array.objects_in_range(0..array.count())
217+
}
218+
}
219+
220+
impl<T> From<&'_ NSArray<T, Shared>> for Vec<Id<T, Shared>>
221+
where
222+
T: INSObject,
223+
{
224+
fn from(array: &NSArray<T, Shared>) -> Self {
225+
array
226+
.objects_in_range(0..array.count())
227+
.into_iter()
228+
.map(|obj| unsafe { Id::retain(obj.into()) })
229+
.collect()
230+
}
231+
}
232+
214233
pub unsafe trait INSMutableArray: INSArray {
215234
#[doc(alias = "addObject:")]
216235
fn push(&mut self, obj: Id<Self::Item, Self::ItemOwnership>) {
@@ -358,6 +377,16 @@ impl<T: INSObject, O: Ownership> Index<usize> for NSMutableArray<T, O> {
358377
}
359378
}
360379

380+
impl<T, O> IntoId<NSMutableArray<T, O>, Owned> for Vec<Id<T, O>>
381+
where
382+
T: INSObject,
383+
O: Ownership,
384+
{
385+
fn into_id(self) -> Id<NSMutableArray<T, O>, Owned> {
386+
unsafe { from_refs(self.as_slice_ref()) }
387+
}
388+
}
389+
361390
#[cfg(test)]
362391
mod tests {
363392
use alloc::vec;
@@ -374,7 +403,7 @@ mod tests {
374403
for _ in 0..len {
375404
vec.push(NSObject::new());
376405
}
377-
NSArray::from_vec(vec)
406+
vec.into_id()
378407
}
379408

380409
fn retain_count<T: INSObject>(obj: &T) -> usize {
@@ -455,7 +484,7 @@ mod tests {
455484
fn test_into_vec() {
456485
let array = sample_array(4);
457486

458-
let vec = INSArray::into_vec(array);
487+
let vec = Vec::from_id(array);
459488
assert_eq!(vec.len(), 4);
460489
}
461490

@@ -504,7 +533,7 @@ mod tests {
504533
#[test]
505534
fn test_sort() {
506535
let strings = vec![NSString::from_str("hello"), NSString::from_str("hi")];
507-
let mut strings = NSMutableArray::from_vec(strings);
536+
let mut strings: Id<NSMutableArray<_, _>, Owned> = strings.into_id();
508537

509538
autoreleasepool(|pool| {
510539
strings.sort_by(|s1, s2| s1.as_str(pool).len().cmp(&s2.as_str(pool).len()));

objc2-foundation/src/enumerator.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,16 @@ impl<'a, C: INSFastEnumeration> Iterator for NSFastEnumerator<'a, C> {
169169

170170
#[cfg(test)]
171171
mod tests {
172+
use alloc::vec::Vec;
173+
use objc2::rc::{Id, IntoId};
174+
172175
use super::INSFastEnumeration;
173176
use crate::{INSArray, INSValue, NSArray, NSValue};
174177

175178
#[test]
176179
fn test_enumerator() {
177-
let vec = (0u32..4).map(NSValue::new).collect();
178-
let array = NSArray::from_vec(vec);
180+
let vec: Vec<_> = (0u32..4).map(NSValue::new).collect();
181+
let array: Id<NSArray<_, _>, _> = vec.into_id();
179182

180183
let enumerator = array.iter();
181184
assert!(enumerator.count() == 4);
@@ -186,8 +189,8 @@ mod tests {
186189

187190
#[test]
188191
fn test_fast_enumerator() {
189-
let vec = (0u32..4).map(NSValue::new).collect();
190-
let array = NSArray::from_vec(vec);
192+
let vec: Vec<_> = (0u32..4).map(NSValue::new).collect();
193+
let array: Id<NSArray<_, _>, _> = vec.into_id();
191194

192195
let enumerator = array.enumerator();
193196
assert!(enumerator.count() == 4);

0 commit comments

Comments
 (0)