|
| 1 | +//! Utilities for the `NSArray` and `NSMutableArray` classes. |
1 | 2 | #![cfg(feature = "Foundation_NSArray")]
|
2 | 3 | use alloc::vec::Vec;
|
3 | 4 | use core::fmt;
|
4 | 5 | use core::mem;
|
5 | 6 | use core::ops::{Index, IndexMut, Range};
|
6 | 7 | use core::panic::{RefUnwindSafe, UnwindSafe};
|
7 | 8 |
|
8 |
| -use objc2::msg_send; |
9 | 9 | use objc2::mutability::{IsMutable, IsRetainable};
|
10 | 10 |
|
| 11 | +use super::iter; |
11 | 12 | use super::util;
|
12 | 13 | use crate::common::*;
|
13 | 14 | #[cfg(feature = "Foundation_NSMutableArray")]
|
@@ -220,15 +221,6 @@ extern_methods!(
|
220 | 221 | );
|
221 | 222 |
|
222 | 223 | impl<T: Message> NSArray<T> {
|
223 |
| - #[doc(alias = "objectEnumerator")] |
224 |
| - #[cfg(feature = "Foundation_NSEnumerator")] |
225 |
| - pub fn iter(&self) -> Foundation::NSEnumerator2<'_, T> { |
226 |
| - unsafe { |
227 |
| - let result: *mut Object = msg_send![self, objectEnumerator]; |
228 |
| - Foundation::NSEnumerator2::from_ptr(result) |
229 |
| - } |
230 |
| - } |
231 |
| - |
232 | 224 | unsafe fn objects_in_range_unchecked(&self, range: Range<usize>) -> Vec<&T> {
|
233 | 225 | let range = Foundation::NSRange::from(range);
|
234 | 226 | let mut vec: Vec<NonNull<T>> = Vec::with_capacity(range.length);
|
@@ -340,33 +332,109 @@ impl<T: Message> NSMutableArray<T> {
|
340 | 332 | }
|
341 | 333 | }
|
342 | 334 |
|
343 |
| -unsafe impl<T: Message> Foundation::NSFastEnumeration2 for NSArray<T> { |
| 335 | +impl<T: Message> NSArray<T> { |
| 336 | + #[doc(alias = "objectEnumerator")] |
| 337 | + #[inline] |
| 338 | + pub fn iter(&self) -> Iter<'_, T> { |
| 339 | + Iter(super::iter::Iter::new(self)) |
| 340 | + } |
| 341 | + |
| 342 | + #[doc(alias = "objectEnumerator")] |
| 343 | + #[inline] |
| 344 | + pub fn iter_mut(&mut self) -> IterMut<'_, T> |
| 345 | + where |
| 346 | + T: IsMutable, |
| 347 | + { |
| 348 | + IterMut(super::iter::IterMut::new(self)) |
| 349 | + } |
| 350 | + |
| 351 | + #[doc(alias = "objectEnumerator")] |
| 352 | + #[inline] |
| 353 | + pub fn iter_retained(&self) -> IterRetained<'_, T> |
| 354 | + where |
| 355 | + T: IsIdCloneable, |
| 356 | + { |
| 357 | + IterRetained(super::iter::IterRetained::new(self)) |
| 358 | + } |
| 359 | +} |
| 360 | + |
| 361 | +unsafe impl<T: Message> iter::FastEnumerationHelper for NSArray<T> { |
344 | 362 | type Item = T;
|
| 363 | + |
| 364 | + #[inline] |
| 365 | + fn maybe_len(&self) -> Option<usize> { |
| 366 | + Some(self.len()) |
| 367 | + } |
345 | 368 | }
|
346 | 369 |
|
347 | 370 | #[cfg(feature = "Foundation_NSMutableArray")]
|
348 |
| -unsafe impl<T: Message> Foundation::NSFastEnumeration2 for NSMutableArray<T> { |
| 371 | +unsafe impl<T: Message> iter::FastEnumerationHelper for NSMutableArray<T> { |
349 | 372 | type Item = T;
|
| 373 | + |
| 374 | + #[inline] |
| 375 | + fn maybe_len(&self) -> Option<usize> { |
| 376 | + Some(self.len()) |
| 377 | + } |
350 | 378 | }
|
351 | 379 |
|
352 |
| -impl<'a, T: Message> IntoIterator for &'a NSArray<T> { |
353 |
| - type Item = &'a T; |
354 |
| - type IntoIter = Foundation::NSFastEnumerator2<'a, NSArray<T>>; |
| 380 | +/// Immutable `NSArray` iterator. |
| 381 | +#[derive(Debug)] |
| 382 | +pub struct Iter<'a, T: Message>(iter::Iter<'a, NSArray<T>>); |
355 | 383 |
|
356 |
| - fn into_iter(self) -> Self::IntoIter { |
357 |
| - use Foundation::NSFastEnumeration2; |
358 |
| - self.iter_fast() |
359 |
| - } |
| 384 | +__impl_iter! { |
| 385 | + impl<'a, T: Message> Iterator<Item = &'a T> for Iter<'a, T> { ... } |
360 | 386 | }
|
361 | 387 |
|
362 |
| -#[cfg(feature = "Foundation_NSMutableArray")] |
363 |
| -impl<'a, T: Message> IntoIterator for &'a NSMutableArray<T> { |
364 |
| - type Item = &'a T; |
365 |
| - type IntoIter = Foundation::NSFastEnumerator2<'a, NSMutableArray<T>>; |
| 388 | +/// Mutable `NSArray` iterator. |
| 389 | +#[derive(Debug)] |
| 390 | +pub struct IterMut<'a, T: Message>(iter::IterMut<'a, NSArray<T>>); |
| 391 | + |
| 392 | +__impl_iter! { |
| 393 | + impl<'a, T: IsMutable> Iterator<Item = &'a mut T> for IterMut<'a, T> { ... } |
| 394 | +} |
| 395 | + |
| 396 | +/// Retained `NSArray` iterator. |
| 397 | +#[derive(Debug)] |
| 398 | +pub struct IterRetained<'a, T: Message>(iter::IterRetained<'a, NSArray<T>>); |
| 399 | + |
| 400 | +__impl_iter! { |
| 401 | + impl<'a, T: IsIdCloneable> Iterator<Item = Id<T>> for IterRetained<'a, T> { ... } |
| 402 | +} |
| 403 | + |
| 404 | +/// Consuming `NSArray` iterator. |
| 405 | +#[derive(Debug)] |
| 406 | +pub struct IntoIter<T: Message>(iter::IntoIter<NSArray<T>>); |
| 407 | + |
| 408 | +__impl_iter! { |
| 409 | + impl<'a, T: Message> Iterator<Item = Id<T>> for IntoIter<T> { ... } |
| 410 | +} |
| 411 | + |
| 412 | +__impl_into_iter! { |
| 413 | + impl<T: Message> IntoIterator for &NSArray<T> { |
| 414 | + type IntoIter = Iter<'_, T>; |
| 415 | + } |
| 416 | + |
| 417 | + #[cfg(feature = "Foundation_NSMutableArray")] |
| 418 | + impl<T: Message> IntoIterator for &NSMutableArray<T> { |
| 419 | + type IntoIter = Iter<'_, T>; |
| 420 | + } |
| 421 | + |
| 422 | + impl<T: IsMutable> IntoIterator for &mut NSArray<T> { |
| 423 | + type IntoIter = IterMut<'_, T>; |
| 424 | + } |
| 425 | + |
| 426 | + #[cfg(feature = "Foundation_NSMutableArray")] |
| 427 | + impl<T: IsMutable> IntoIterator for &mut NSMutableArray<T> { |
| 428 | + type IntoIter = IterMut<'_, T>; |
| 429 | + } |
| 430 | + |
| 431 | + impl<T: IsIdCloneable> IntoIterator for Id<NSArray<T>> { |
| 432 | + type IntoIter = IntoIter<T>; |
| 433 | + } |
366 | 434 |
|
367 |
| - fn into_iter(self) -> Self::IntoIter { |
368 |
| - use Foundation::NSFastEnumeration2; |
369 |
| - self.iter_fast() |
| 435 | + #[cfg(feature = "Foundation_NSMutableArray")] |
| 436 | + impl<T: Message> IntoIterator for Id<NSMutableArray<T>> { |
| 437 | + type IntoIter = IntoIter<T>; |
370 | 438 | }
|
371 | 439 | }
|
372 | 440 |
|
@@ -403,8 +471,7 @@ impl<T: IsMutable> IndexMut<usize> for NSMutableArray<T> {
|
403 | 471 | impl<T: fmt::Debug + Message> fmt::Debug for NSArray<T> {
|
404 | 472 | #[inline]
|
405 | 473 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
406 |
| - use Foundation::NSFastEnumeration2; |
407 |
| - f.debug_list().entries(self.iter_fast()).finish() |
| 474 | + f.debug_list().entries(self).finish() |
408 | 475 | }
|
409 | 476 | }
|
410 | 477 |
|
|
0 commit comments