Skip to content

Commit

Permalink
Idiomatic NSArray iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
agg23 committed Jul 19, 2023
1 parent 01507f7 commit 289a87e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
12 changes: 7 additions & 5 deletions src/appkit/app/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,12 @@ extern "C" fn accepted_cloudkit_share<T: AppDelegate>(this: &Object, _: Sel, _:
/// Fires when the application receives an `application:openURLs` message.
extern "C" fn open_urls<T: AppDelegate>(this: &Object, _: Sel, _: id, file_urls: id) {
let urls = NSArray::retain(file_urls)
.map(|url| {
.iter()
.filter_map(|url| {
let uri = NSString::retain(unsafe { msg_send![url, absoluteString] });

Url::parse(uri.to_str())
Url::parse(uri.to_str()).ok()
})
.into_iter()
.filter_map(|url| url.ok())
.collect();

app::<T>(this).open_urls(urls);
Expand Down Expand Up @@ -263,7 +262,10 @@ extern "C" fn print_files<T: AppDelegate>(
settings: id,
show_print_panels: BOOL
) -> NSUInteger {
let files = NSArray::retain(files).map(|file| NSString::retain(file).to_str().to_string());
let files = NSArray::retain(files)
.iter()
.map(|file| NSString::retain(file).to_str().to_string())
.collect();

let settings = PrintSettings::with_inner(settings);

Expand Down
44 changes: 32 additions & 12 deletions src/foundation/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,41 @@ impl NSArray {
unsafe { msg_send![&*self.0, count] }
}

/// A helper method for mapping over the backing `NSArray` items and producing a Rust `Vec<T>`.
/// Often times we need to map in this framework to convert between Rust types, so isolating
/// this out makes life much easier.
pub fn map<T, F: Fn(id) -> T>(&self, transform: F) -> Vec<T> {
let count = self.count();
let objc = &*self.0;
/// Returns an iterator over the `NSArray`
pub fn iter<'a>(&'a self) -> NSArrayIterator<'a> {
NSArrayIterator {
next_index: 0,
count: self.count(),
array: self
}
}
}

#[derive(Debug)]
pub struct NSArrayIterator<'a> {
next_index: usize,
count: usize,

array: &'a NSArray
}

impl Iterator for NSArrayIterator<'_> {
type Item = id;

fn next(&mut self) -> Option<Self::Item> {
// I don't know if it's worth trying to get in with NSFastEnumeration here. I'm content to
// just rely on Rust, but someone is free to profile it if they want.
(0..count)
.map(|index| {
let item: id = unsafe { msg_send![objc, objectAtIndex: index] };
transform(item)
})
.collect()
if self.next_index < self.count {
let objc = &*self.array.0;
let index = self.next_index;

let item: id = unsafe { msg_send![objc, objectAtIndex: index] };

self.next_index += 1;
Some(item)
} else {
None
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/pasteboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl Pasteboard {
}));
}

let urls = NSArray::retain(contents).map(|url| NSURL::retain(url)).into_iter().collect();
let urls = NSArray::retain(contents).iter().map(|url| NSURL::retain(url)).collect();

Ok(urls)
}
Expand Down

0 comments on commit 289a87e

Please sign in to comment.