//! Utilities for the `NSEnumerator` class. use objc2::rc::Retained; use objc2::Message; use crate::{iter, NSEnumerator}; // TODO: Measure whether iterating through `nextObject` or fast enumeration is // fastest. // impl Iterator for NSEnumerator { // type Item = Retained; // // #[inline] // fn next(&mut self) -> Option> { // self.nextObject() // } // } impl NSEnumerator { /// Iterate over the enumerator's elements. #[inline] pub fn iter(&self) -> Iter<'_, ObjectType> { Iter(iter::Iter::new(self)) } /// Iterate over the enumerator without retaining the elements. /// /// Consider using the [`iter`](Self::iter) method instead, unless you're /// seeing performance issues from the retaining. /// /// # Safety /// /// The enumerator and the underlying collection must not be mutated while /// the iterator is alive. #[inline] pub unsafe fn iter_unchecked(&self) -> IterUnchecked<'_, ObjectType> { IterUnchecked(iter::IterUnchecked::new(self)) } } unsafe impl iter::FastEnumerationHelper for NSEnumerator { type Item = ObjectType; #[inline] fn maybe_len(&self) -> Option { None } } /// An iterator over the items in an enumerator. #[derive(Debug)] pub struct Iter<'a, ObjectType: Message>(iter::Iter<'a, NSEnumerator>); __impl_iter! { impl<'a, ObjectType: Message> Iterator> for Iter<'a, ObjectType> { ... } } /// An iterator over unretained items in an enumerator. /// /// # Safety /// /// The enumerator and the underlying collection must not be mutated while /// this is alive. #[derive(Debug)] pub struct IterUnchecked<'a, ObjectType: Message + 'a>( iter::IterUnchecked<'a, NSEnumerator>, ); __impl_iter! { impl<'a, ObjectType: Message> Iterator for IterUnchecked<'a, ObjectType> { ... } } /// A consuming iterator over the items in an enumerator. #[derive(Debug)] pub struct IntoIter(iter::IntoIter>); __impl_iter! { impl Iterator> for IntoIter { ... } } __impl_into_iter! { impl IntoIterator for &NSEnumerator { type IntoIter = Iter<'_, ObjectType>; } impl IntoIterator for Retained> { #[uses(new)] type IntoIter = IntoIter; } } // TODO: Does fast enumeration modify the enumeration while iterating?