diff --git a/crates/test-ui/ui/nsarray_not_message.stderr b/crates/test-ui/ui/nsarray_not_message.stderr index 7b499e2e4..c50891702 100644 --- a/crates/test-ui/ui/nsarray_not_message.stderr +++ b/crates/test-ui/ui/nsarray_not_message.stderr @@ -46,11 +46,11 @@ error[E0277]: the trait bound `Retained: Message` is not satisfied NSEnumerator NSError and $N others -note: required by a bound in `objc2_foundation::array::>::from_slice` +note: required by a bound in `objc2_foundation::array::>::from_slice` --> $WORKSPACE/framework-crates/objc2-foundation/src/array.rs | - | impl NSArray { - | ^^^^^^^ required by this bound in `objc2_foundation::array::>::from_slice` + | impl NSArray { + | ^^^^^^^ required by this bound in `objc2_foundation::array::>::from_slice` ... - | pub fn from_slice(slice: &[&T]) -> Retained { + | pub fn from_slice(slice: &[&ObjectType]) -> Retained { | ---------- required by a bound in this associated function diff --git a/framework-crates/objc2-foundation/src/array.rs b/framework-crates/objc2-foundation/src/array.rs index 5d5f9e22f..ab8adbee5 100644 --- a/framework-crates/objc2-foundation/src/array.rs +++ b/framework-crates/objc2-foundation/src/array.rs @@ -13,7 +13,7 @@ use crate::iter; use crate::{util, NSArray, NSMutableArray}; /// Convenience creation methods. -impl NSArray { +impl NSArray { /// Create a new array from a slice of objects. /// /// This is a safe interface to `initWithObjects:count:`. @@ -30,12 +30,12 @@ impl NSArray { /// ]); /// ``` #[doc(alias = "initWithObjects:count:")] - pub fn from_slice(slice: &[&T]) -> Retained { + pub fn from_slice(slice: &[&ObjectType]) -> Retained { let len = slice.len(); let ptr = util::ref_ptr_cast_const(slice.as_ptr()); // SAFETY: - // - All `T: Message` use interior mutability, and the array extends - // the lifetime of them internally by retaining them. + // - All `ObjectType: Message` use interior mutability, and the array + // extends the lifetime of them internally by retaining them. // - The pointer and length are valid until the method has finished // executing, at which point the array will have created its own // internal storage for holding the pointers. @@ -58,7 +58,7 @@ impl NSArray { /// ]); /// ``` #[doc(alias = "initWithObjects:count:")] - pub fn from_retained_slice(slice: &[Retained]) -> Retained { + pub fn from_retained_slice(slice: &[Retained]) -> Retained { let len = slice.len(); let ptr = util::retained_ptr_cast_const(slice.as_ptr()); // SAFETY: Same as `from_slice`, this is just a faster version to @@ -71,9 +71,9 @@ impl NSArray { } /// Convenience creation methods. -impl NSMutableArray { +impl NSMutableArray { #[doc(alias = "initWithObjects:count:")] - pub fn from_slice(slice: &[&T]) -> Retained { + pub fn from_slice(slice: &[&ObjectType]) -> Retained { let len = slice.len(); let ptr = util::ref_ptr_cast_const(slice.as_ptr()); // SAFETY: Same as `NSArray::from_slice`. @@ -81,7 +81,7 @@ impl NSMutableArray { } #[doc(alias = "initWithObjects:count:")] - pub fn from_retained_slice(slice: &[Retained]) -> Retained { + pub fn from_retained_slice(slice: &[Retained]) -> Retained { let len = slice.len(); let ptr = util::retained_ptr_cast_const(slice.as_ptr()); // SAFETY: Same as `NSArray::from_retained_slice` @@ -100,7 +100,7 @@ impl NSMutableArray { /// doing so - otherwise, we might end up accessing a deallocated object. /// /// [collections-own]: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-SW12 -impl NSArray { +impl NSArray { /// Get a direct reference to one of the array's objects. /// /// Throws an error if the object was not found. @@ -113,7 +113,7 @@ impl NSArray { /// The array must not be mutated while the reference is live. #[doc(alias = "objectAtIndex:")] #[inline] - pub unsafe fn objectAtIndex_unchecked(&self, index: usize) -> &T { + pub unsafe fn objectAtIndex_unchecked(&self, index: usize) -> &ObjectType { // SAFETY: Upheld by caller. unsafe { msg_send![self, objectAtIndex: index] } } @@ -128,7 +128,7 @@ impl NSArray { /// The array must not be mutated while the reference is live. #[doc(alias = "firstObject")] #[inline] - pub unsafe fn firstObject_unchecked(&self) -> Option<&T> { + pub unsafe fn firstObject_unchecked(&self) -> Option<&ObjectType> { // SAFETY: Upheld by caller. unsafe { msg_send![self, firstObject] } } @@ -143,7 +143,7 @@ impl NSArray { /// The array must not be mutated while the reference is live. #[doc(alias = "lastObject")] #[inline] - pub unsafe fn lastObject_unchecked(&self) -> Option<&T> { + pub unsafe fn lastObject_unchecked(&self) -> Option<&ObjectType> { // SAFETY: Upheld by caller. unsafe { msg_send![self, lastObject] } } @@ -157,10 +157,10 @@ impl NSArray { /// /// The array must not be mutated while the returned references are alive. #[doc(alias = "getObjects:")] - pub unsafe fn to_vec_unchecked(&self) -> Vec<&T> { + pub unsafe fn to_vec_unchecked(&self) -> Vec<&ObjectType> { let len = self.count(); - let mut vec: Vec> = Vec::with_capacity(len); - let ptr: NonNull> = NonNull::new(vec.as_mut_ptr()).unwrap(); + let mut vec: Vec> = Vec::with_capacity(len); + let ptr: NonNull> = NonNull::new(vec.as_mut_ptr()).unwrap(); // SAFETY: The buffer is at least the size of the array, as guaranteed // by `Vec::with_capacity`. @@ -176,10 +176,10 @@ impl NSArray { // uninitialized elements. unsafe { vec.set_len(len) }; - // SAFETY: `NonNull` has the same layout as `&T`, and the lifetime - // is bound to the array, and caller upholds that the array isn't - // mutated. - unsafe { mem::transmute::>, Vec<&T>>(vec) } + // SAFETY: `NonNull` has the same layout as `&ObjectType`, + // and the lifetime is bound to the array, and caller upholds that the + // array isn't mutated. + unsafe { mem::transmute::>, Vec<&ObjectType>>(vec) } } /// Iterate over the array without retaining the elements. @@ -194,13 +194,13 @@ impl NSArray { #[cfg(feature = "NSEnumerator")] #[doc(alias = "objectEnumerator")] #[inline] - pub unsafe fn iter_unchecked(&self) -> IterUnchecked<'_, T> { + pub unsafe fn iter_unchecked(&self) -> IterUnchecked<'_, ObjectType> { IterUnchecked(iter::IterUnchecked::new(self)) } } /// Various accessor methods. -impl NSArray { +impl NSArray { /// The amount of elements in the array. #[doc(alias = "count")] #[inline] @@ -216,7 +216,7 @@ impl NSArray { /// Convert the array to a `Vec` of the array's objects. #[doc(alias = "getObjects:")] - pub fn to_vec(&self) -> Vec> { + pub fn to_vec(&self) -> Vec> { // SAFETY: We retain the elements below, so we know that the array // isn't mutated while the references are alive. // @@ -225,14 +225,14 @@ impl NSArray { // to rule this out though, as that's basically never going to happen, // and will make a lot of other things unsound too. let vec = unsafe { self.to_vec_unchecked() }; - vec.into_iter().map(T::retain).collect() + vec.into_iter().map(ObjectType::retain).collect() } /// Iterate over the array's elements. #[cfg(feature = "NSEnumerator")] #[doc(alias = "objectEnumerator")] #[inline] - pub fn iter(&self) -> Iter<'_, T> { + pub fn iter(&self) -> Iter<'_, ObjectType> { Iter(iter::Iter::new(self)) } @@ -243,7 +243,7 @@ impl NSArray { /// Panics if the range was out of bounds. #[doc(alias = "getObjects:range:")] #[cfg(feature = "NSRange")] - pub fn objects_in_range(&self, range: core::ops::Range) -> Vec> { + pub fn objects_in_range(&self, range: core::ops::Range) -> Vec> { let count = self.count(); // TODO: Replace this check with catching the thrown NSRangeException @@ -255,27 +255,27 @@ impl NSArray { } let range = crate::NSRange::from(range); - let mut vec: Vec> = Vec::with_capacity(range.length); - let ptr: NonNull> = NonNull::new(vec.as_mut_ptr()).unwrap(); + let mut vec: Vec> = Vec::with_capacity(range.length); + let ptr: NonNull> = NonNull::new(vec.as_mut_ptr()).unwrap(); // SAFETY: Mostly the same as in `to_vec_unchecked`. unsafe { self.getObjects_range(ptr, range) }; unsafe { vec.set_len(range.length) }; - let vec = unsafe { mem::transmute::>, Vec<&T>>(vec) }; + let vec = unsafe { mem::transmute::>, Vec<&ObjectType>>(vec) }; - vec.into_iter().map(T::retain).collect() + vec.into_iter().map(ObjectType::retain).collect() } } /// Convenience mutation methods. -impl NSMutableArray { +impl NSMutableArray { /// Insert an object into the array at the given index. /// /// # Panics /// /// Panics if the index is out of bounds. #[doc(alias = "insertObject:atIndex:")] - pub fn insert(&self, index: usize, obj: &T) { + pub fn insert(&self, index: usize, obj: &ObjectType) { // TODO: Replace this check with catching the thrown NSRangeException let len = self.len(); if index < len { @@ -291,13 +291,13 @@ impl NSMutableArray { /// Sort the array by the given comparison closure. #[cfg(feature = "NSObjCRuntime")] #[doc(alias = "sortUsingFunction:context:")] - pub fn sort_by core::cmp::Ordering>(&self, compare: F) { + pub fn sort_by core::cmp::Ordering>(&self, compare: F) { unsafe extern "C-unwind" fn compare_with_closure< - T, - F: FnMut(&T, &T) -> core::cmp::Ordering, + ObjectType, + F: FnMut(&ObjectType, &ObjectType) -> core::cmp::Ordering, >( - obj1: core::ptr::NonNull, - obj2: core::ptr::NonNull, + obj1: core::ptr::NonNull, + obj2: core::ptr::NonNull, context: *mut core::ffi::c_void, ) -> isize { let context: *mut F = context.cast(); @@ -313,7 +313,7 @@ impl NSMutableArray { } // Create function pointer - let f: unsafe extern "C-unwind" fn(_, _, _) -> _ = compare_with_closure::; + let f: unsafe extern "C-unwind" fn(_, _, _) -> _ = compare_with_closure::; // Grab a type-erased pointer to the closure (a pointer to stack). let mut closure = compare; @@ -326,8 +326,8 @@ impl NSMutableArray { } #[cfg(feature = "NSEnumerator")] -unsafe impl iter::FastEnumerationHelper for NSArray { - type Item = T; +unsafe impl iter::FastEnumerationHelper for NSArray { + type Item = ObjectType; #[inline] fn maybe_len(&self) -> Option { @@ -336,8 +336,8 @@ unsafe impl iter::FastEnumerationHelper for NSArray { } #[cfg(feature = "NSEnumerator")] -unsafe impl iter::FastEnumerationHelper for NSMutableArray { - type Item = T; +unsafe impl iter::FastEnumerationHelper for NSMutableArray { + type Item = ObjectType; #[inline] fn maybe_len(&self) -> Option { @@ -348,11 +348,11 @@ unsafe impl iter::FastEnumerationHelper for NSMutableArray { /// An iterator over the items of an array. #[derive(Debug)] #[cfg(feature = "NSEnumerator")] -pub struct Iter<'a, T: Message>(iter::Iter<'a, NSArray>); +pub struct Iter<'a, ObjectType: Message>(iter::Iter<'a, NSArray>); #[cfg(feature = "NSEnumerator")] __impl_iter! { - impl<'a, T: Message> Iterator> for Iter<'a, T> { ... } + impl<'a, ObjectType: Message> Iterator> for Iter<'a, ObjectType> { ... } } /// An iterator over unretained items of an array. @@ -362,46 +362,46 @@ __impl_iter! { /// The array must not be mutated while this is alive. #[derive(Debug)] #[cfg(feature = "NSEnumerator")] -pub struct IterUnchecked<'a, T: Message>(iter::IterUnchecked<'a, NSArray>); +pub struct IterUnchecked<'a, ObjectType: Message>(iter::IterUnchecked<'a, NSArray>); #[cfg(feature = "NSEnumerator")] __impl_iter! { - impl<'a, T: Message> Iterator for IterUnchecked<'a, T> { ... } + impl<'a, ObjectType: Message> Iterator for IterUnchecked<'a, ObjectType> { ... } } /// A retained iterator over the items of an array. #[derive(Debug)] #[cfg(feature = "NSEnumerator")] -pub struct IntoIter(iter::IntoIter>); +pub struct IntoIter(iter::IntoIter>); #[cfg(feature = "NSEnumerator")] __impl_iter! { - impl Iterator> for IntoIter { ... } + impl Iterator> for IntoIter { ... } } #[cfg(feature = "NSEnumerator")] __impl_into_iter! { - impl IntoIterator for &NSArray { - type IntoIter = Iter<'_, T>; + impl IntoIterator for &NSArray { + type IntoIter = Iter<'_, ObjectType>; } - impl IntoIterator for &NSMutableArray { - type IntoIter = Iter<'_, T>; + impl IntoIterator for &NSMutableArray { + type IntoIter = Iter<'_, ObjectType>; } - impl IntoIterator for Retained> { + impl IntoIterator for Retained> { #[uses(new)] - type IntoIter = IntoIter; + type IntoIter = IntoIter; } - impl IntoIterator for Retained> { + impl IntoIterator for Retained> { #[uses(new_mutable)] - type IntoIter = IntoIter; + type IntoIter = IntoIter; } } #[cfg(feature = "NSEnumerator")] -impl fmt::Debug for NSArray { +impl fmt::Debug for NSArray { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self).finish() @@ -409,49 +409,53 @@ impl fmt::Debug for NSArray { } #[cfg(feature = "NSEnumerator")] -impl fmt::Debug for NSMutableArray { +impl fmt::Debug for NSMutableArray { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } -impl Extend> for &NSMutableArray { - fn extend>>(&mut self, iter: I) { +impl Extend> for &NSMutableArray { + fn extend>>(&mut self, iter: I) { iter.into_iter().for_each(move |item| self.addObject(&item)); } } -impl<'a, T: Message> Extend<&'a T> for &NSMutableArray { - fn extend>(&mut self, iter: I) { +impl<'a, ObjectType: Message> Extend<&'a ObjectType> for &NSMutableArray { + fn extend>(&mut self, iter: I) { iter.into_iter().for_each(move |item| self.addObject(item)); } } -impl<'a, T: Message + 'a> RetainedFromIterator<&'a T> for NSArray { - fn retained_from_iter>(iter: I) -> Retained { +impl<'a, ObjectType: Message + 'a> RetainedFromIterator<&'a ObjectType> for NSArray { + fn retained_from_iter>(iter: I) -> Retained { let vec = Vec::from_iter(iter); Self::from_slice(&vec) } } -impl RetainedFromIterator> for NSArray { - fn retained_from_iter>>(iter: I) -> Retained { +impl RetainedFromIterator> for NSArray { + fn retained_from_iter>>(iter: I) -> Retained { let vec = Vec::from_iter(iter); Self::from_retained_slice(&vec) } } -impl<'a, T: Message + 'a> RetainedFromIterator<&'a T> for NSMutableArray { - fn retained_from_iter>(iter: I) -> Retained { +impl<'a, ObjectType: Message + 'a> RetainedFromIterator<&'a ObjectType> + for NSMutableArray +{ + fn retained_from_iter>(iter: I) -> Retained { // TODO: Is this, or is using `initWithCapacity` the most optimal? let vec = Vec::from_iter(iter); Self::from_slice(&vec) } } -impl RetainedFromIterator> for NSMutableArray { - fn retained_from_iter>>(iter: I) -> Retained { +impl RetainedFromIterator> + for NSMutableArray +{ + fn retained_from_iter>>(iter: I) -> Retained { // TODO: Is this, or is using `initWithCapacity` the most optimal? let vec = Vec::from_iter(iter); Self::from_retained_slice(&vec) diff --git a/framework-crates/objc2-foundation/src/dictionary.rs b/framework-crates/objc2-foundation/src/dictionary.rs index 579f0d7d0..abc4f34fc 100644 --- a/framework-crates/objc2-foundation/src/dictionary.rs +++ b/framework-crates/objc2-foundation/src/dictionary.rs @@ -19,19 +19,19 @@ use crate::{util, CopyingHelper, NSCopying}; use crate::{NSDictionary, NSMutableDictionary}; #[cfg(feature = "NSObject")] -fn keys_to_ptr(keys: &[&Q]) -> *mut NonNull> +fn keys_to_ptr(keys: &[&CopiedKey]) -> *mut NonNull> where - Q: Message + NSCopying, + CopiedKey: Message + NSCopying, { - let keys: *mut NonNull = util::ref_ptr_cast_const(keys.as_ptr()); - // SAFETY: `Q` is `Message + NSCopying`, and is therefore safe to cast to + let keys: *mut NonNull = util::ref_ptr_cast_const(keys.as_ptr()); + // SAFETY: `CopiedKey` is `Message + NSCopying`, and is therefore safe to cast to // `ProtocolObject`. let keys: *mut NonNull> = keys.cast(); keys } /// Convenience creation methods. -impl NSDictionary { +impl NSDictionary { /// Create a new dictionary from a slice of keys, and a slice of objects. /// /// This is a safe interface to `initWithObjects:forKeys:count:`. @@ -54,13 +54,13 @@ impl NSDictionary { /// assert_eq!(&*dict.objectForKey(ns_string!("key2")).unwrap(), ns_string!("value2")); /// ``` #[cfg(feature = "NSObject")] - pub fn from_slices(keys: &[&Q], objects: &[&V]) -> Retained + pub fn from_slices(keys: &[&CopiedKey], objects: &[&ObjectType]) -> Retained where // The dictionary copies its keys, which is why we require `NSCopying` - // and use `CopyingHelper` on all input data - we want to ensure that the - // type-system knows that it's not actually e.g. `NSMutableString` + // and use `CopyingHelper` on all input data - we want to ensure that + // the type-system knows that it's not actually e.g. `NSMutableString` // that is being stored, but instead `NSString`. - Q: Message + NSCopying + CopyingHelper, + CopiedKey: Message + NSCopying + CopyingHelper, { // Ensure that we don't read too far into one of the buffers. assert_eq!( @@ -74,8 +74,9 @@ impl NSDictionary { let objects = util::ref_ptr_cast_const(objects.as_ptr()); // SAFETY: - // - All `T: Message` use interior mutability, and the dictionary - // extends the lifetime of them internally by retaining them. + // - All types that are `Message` use interior mutability, and the + // dictionary extends the lifetime of them internally by retaining + // them. // // - The pointers are valid until the method has finished executing, // at which point the dictionary will have created its own internal @@ -95,9 +96,12 @@ impl NSDictionary { } #[cfg(feature = "NSObject")] - pub fn from_retained_objects(keys: &[&Q], objects: &[Retained]) -> Retained + pub fn from_retained_objects( + keys: &[&CopiedKey], + objects: &[Retained], + ) -> Retained where - Q: Message + NSCopying + CopyingHelper, + CopiedKey: Message + NSCopying + CopyingHelper, { // Ensure that we don't read too far into one of the buffers. assert_eq!( @@ -116,11 +120,11 @@ impl NSDictionary { } /// Convenience creation methods. -impl NSMutableDictionary { +impl NSMutableDictionary { #[cfg(feature = "NSObject")] - pub fn from_slices(keys: &[&Q], objects: &[&V]) -> Retained + pub fn from_slices(keys: &[&CopiedKey], objects: &[&ObjectType]) -> Retained where - Q: Message + NSCopying + CopyingHelper, + CopiedKey: Message + NSCopying + CopyingHelper, { // Ensure that we don't read too far into one of the buffers. assert_eq!( @@ -138,9 +142,12 @@ impl NSMutableDictionary { } #[cfg(feature = "NSObject")] - pub fn from_retained_objects(keys: &[&Q], objects: &[Retained]) -> Retained + pub fn from_retained_objects( + keys: &[&CopiedKey], + objects: &[Retained], + ) -> Retained where - Q: Message + NSCopying + CopyingHelper, + CopiedKey: Message + NSCopying + CopyingHelper, { // Ensure that we don't read too far into one of the buffers. assert_eq!( @@ -158,15 +165,15 @@ impl NSMutableDictionary { } } -// Note: We'd like to make getter methods take `K: Borrow` like -// `std::collections::HashMap`, so that e.g. `NSDictionary` +// Note: We'd like to make getter methods take `KeyType: Borrow` +// like `std::collections::HashMap`, so that e.g. `NSDictionary` // could take a `&NSObject` as input, and still make that work since // `NSString` borrows to `NSObject`. // // But we can't really, at least not with extra `unsafe` / an extra trait, // since we don't control how the comparisons happen. // -// The most useful alternative would probably be to take `impl AsRef`, but +// The most useful alternative would probably be to take `impl AsRef`, but // objc2 classes deref to their superclass anyhow, so let's just use a simple, // normal reference. @@ -182,7 +189,7 @@ impl NSMutableDictionary { /// accessing a deallocated object. /// /// [collections-own]: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-SW12 -impl NSDictionary { +impl NSDictionary { /// Get a direct reference to the object corresponding to the key. /// /// Consider using the [`objectForKey`](Self::objectForKey) method @@ -193,7 +200,7 @@ impl NSDictionary { /// The dictionary must not be mutated while the reference is live. #[doc(alias = "objectForKey:")] #[inline] - pub unsafe fn objectForKey_unchecked(&self, key: &K) -> Option<&V> { + pub unsafe fn objectForKey_unchecked(&self, key: &KeyType) -> Option<&ObjectType> { unsafe { msg_send![self, objectForKey: key] } } @@ -208,7 +215,7 @@ impl NSDictionary { /// The dictionary must not be mutated while the returned references are /// alive. #[doc(alias = "getObjects:andKeys:")] - pub unsafe fn to_vecs_unchecked(&self) -> (Vec<&K>, Vec<&V>) { + pub unsafe fn to_vecs_unchecked(&self) -> (Vec<&KeyType>, Vec<&ObjectType>) { let len = self.len(); let mut keys = Vec::with_capacity(len); let mut objs = Vec::with_capacity(len); @@ -231,8 +238,8 @@ impl NSDictionary { // lifetime is upheld by the caller. unsafe { ( - mem::transmute::>, Vec<&K>>(keys), - mem::transmute::>, Vec<&V>>(objs), + mem::transmute::>, Vec<&KeyType>>(keys), + mem::transmute::>, Vec<&ObjectType>>(objs), ) } } @@ -249,7 +256,7 @@ impl NSDictionary { #[cfg(feature = "NSEnumerator")] #[doc(alias = "keyEnumerator")] #[inline] - pub unsafe fn keys_unchecked(&self) -> KeysUnchecked<'_, K, V> { + pub unsafe fn keys_unchecked(&self) -> KeysUnchecked<'_, KeyType, ObjectType> { KeysUnchecked(iter::IterUnchecked::new(self)) } @@ -265,7 +272,7 @@ impl NSDictionary { #[cfg(feature = "NSEnumerator")] #[doc(alias = "objectEnumerator")] #[inline] - pub unsafe fn objects_unchecked(&self) -> ObjectsUnchecked<'_, K, V> { + pub unsafe fn objects_unchecked(&self) -> ObjectsUnchecked<'_, KeyType, ObjectType> { // SAFETY: Avoiding mutation is upheld by caller. let enumerator = unsafe { self.objectEnumerator() }; // SAFETY: The enumerator came from the dictionary. @@ -274,7 +281,7 @@ impl NSDictionary { } /// Various accessor methods. -impl NSDictionary { +impl NSDictionary { /// The amount of elements in the dictionary. #[doc(alias = "count")] #[inline] @@ -307,13 +314,13 @@ impl NSDictionary { /// } /// ``` #[doc(alias = "getObjects:")] - pub fn to_vecs(&self) -> (Vec>, Vec>) { + pub fn to_vecs(&self) -> (Vec>, Vec>) { // SAFETY: We retain the elements below, so that we know that the // dictionary isn't mutated while they are alive. let (keys, objects) = unsafe { self.to_vecs_unchecked() }; ( - keys.into_iter().map(K::retain).collect(), - objects.into_iter().map(V::retain).collect(), + keys.into_iter().map(KeyType::retain).collect(), + objects.into_iter().map(ObjectType::retain).collect(), ) } @@ -321,7 +328,7 @@ impl NSDictionary { #[cfg(feature = "NSEnumerator")] #[doc(alias = "keyEnumerator")] #[inline] - pub fn keys(&self) -> Keys<'_, K, V> { + pub fn keys(&self) -> Keys<'_, KeyType, ObjectType> { Keys(iter::Iter::new(self)) } @@ -343,7 +350,7 @@ impl NSDictionary { #[cfg(feature = "NSEnumerator")] #[doc(alias = "objectEnumerator")] #[inline] - pub fn objects(&self) -> Objects<'_, K, V> { + pub fn objects(&self) -> Objects<'_, KeyType, ObjectType> { // SAFETY: The iterator checks for mutation while enumerating. let enumerator = unsafe { self.objectEnumerator() }; // SAFETY: The enumerator came from the dictionary. @@ -352,7 +359,7 @@ impl NSDictionary { } /// Convenience mutation methods. -impl NSMutableDictionary { +impl NSMutableDictionary { /// Inserts a key-value pair into the dictionary. /// /// If the dictionary did not have this key present, the value is @@ -370,20 +377,22 @@ impl NSMutableDictionary { #[cfg(feature = "NSObject")] #[doc(alias = "setObject:forKey:")] #[inline] - pub fn insert(&self, key: &Q, object: &V) + pub fn insert(&self, key: &CopiedKey, object: &ObjectType) where - Q: Message + NSCopying + CopyingHelper, + CopiedKey: Message + NSCopying + CopyingHelper, { let key = ProtocolObject::from_ref(key); - // SAFETY: The key is copied, and then has the correct type `K`. + // SAFETY: The key is copied, and then has the correct type `KeyType`. unsafe { self.setObject_forKey(object, key) }; } } #[cfg(feature = "NSEnumerator")] -unsafe impl iter::FastEnumerationHelper for NSDictionary { +unsafe impl iter::FastEnumerationHelper + for NSDictionary +{ // Fast enumeration for dictionaries returns the keys. - type Item = K; + type Item = KeyType; #[inline] fn maybe_len(&self) -> Option { @@ -392,9 +401,11 @@ unsafe impl iter::FastEnumerationHelper for NSDictionary } #[cfg(feature = "NSEnumerator")] -unsafe impl iter::FastEnumerationHelper for NSMutableDictionary { +unsafe impl iter::FastEnumerationHelper + for NSMutableDictionary +{ // The same goes for mutable dictionaries. - type Item = K; + type Item = KeyType; #[inline] fn maybe_len(&self) -> Option { @@ -405,11 +416,13 @@ unsafe impl iter::FastEnumerationHelper for NSMutableDic /// An iterator over the keys of a dictionary. #[derive(Debug)] #[cfg(feature = "NSEnumerator")] -pub struct Keys<'a, K: Message, V: Message>(iter::Iter<'a, NSDictionary>); +pub struct Keys<'a, KeyType: Message, ObjectType: Message>( + iter::Iter<'a, NSDictionary>, +); #[cfg(feature = "NSEnumerator")] __impl_iter! { - impl<'a, K: Message, V: Message> Iterator> for Keys<'a, K, V> { ... } + impl<'a, KeyType: Message, ObjectType: Message> Iterator> for Keys<'a, KeyType, ObjectType> { ... } } /// An iterator over unretained keys of a dictionary. @@ -419,23 +432,29 @@ __impl_iter! { /// The dictionary must not be mutated while this is alive. #[derive(Debug)] #[cfg(feature = "NSEnumerator")] -pub struct KeysUnchecked<'a, K: Message, V: Message>(iter::IterUnchecked<'a, NSDictionary>); +pub struct KeysUnchecked<'a, KeyType: Message, ObjectType: Message>( + iter::IterUnchecked<'a, NSDictionary>, +); #[cfg(feature = "NSEnumerator")] __impl_iter! { - impl<'a, K: Message, V: Message> Iterator for KeysUnchecked<'a, K, V> { ... } + impl<'a, KeyType: Message, ObjectType: Message> Iterator for KeysUnchecked<'a, KeyType, ObjectType> { ... } } /// An iterator over the objects / values in a dictionary. #[derive(Debug)] #[cfg(feature = "NSEnumerator")] -pub struct Objects<'a, K: Message, V: Message>( - iter::IterWithBackingEnum<'a, NSDictionary, crate::NSEnumerator>, +pub struct Objects<'a, KeyType: Message, ObjectType: Message>( + iter::IterWithBackingEnum< + 'a, + NSDictionary, + crate::NSEnumerator, + >, ); #[cfg(feature = "NSEnumerator")] __impl_iter! { - impl<'a, K: Message, V: Message> Iterator> for Objects<'a, K, V> { ... } + impl<'a, KeyType: Message, ObjectType: Message> Iterator> for Objects<'a, KeyType, ObjectType> { ... } } /// An iterator over unretained objects / values of a dictionary. @@ -445,16 +464,22 @@ __impl_iter! { /// The dictionary must not be mutated while this is alive. #[derive(Debug)] #[cfg(feature = "NSEnumerator")] -pub struct ObjectsUnchecked<'a, K: Message, V: Message + 'a>( - iter::IterUncheckedWithBackingEnum<'a, NSDictionary, crate::NSEnumerator>, +pub struct ObjectsUnchecked<'a, KeyType: Message, ObjectType: Message + 'a>( + iter::IterUncheckedWithBackingEnum< + 'a, + NSDictionary, + crate::NSEnumerator, + >, ); #[cfg(feature = "NSEnumerator")] __impl_iter! { - impl<'a, K: Message, V: Message> Iterator for ObjectsUnchecked<'a, K, V> { ... } + impl<'a, KeyType: Message, ObjectType: Message> Iterator for ObjectsUnchecked<'a, KeyType, ObjectType> { ... } } -impl fmt::Debug for NSDictionary { +impl fmt::Debug + for NSDictionary +{ #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // SAFETY: Unsound, use `to_vecs` instead when that doesn't have extra bounds @@ -464,7 +489,9 @@ impl fmt::Debug for NSDictiona } } -impl fmt::Debug for NSMutableDictionary { +impl fmt::Debug + for NSMutableDictionary +{ #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) diff --git a/framework-crates/objc2-foundation/src/enumerator.rs b/framework-crates/objc2-foundation/src/enumerator.rs index 3fd67d9d7..6b2d61344 100644 --- a/framework-crates/objc2-foundation/src/enumerator.rs +++ b/framework-crates/objc2-foundation/src/enumerator.rs @@ -6,19 +6,19 @@ use crate::{iter, NSEnumerator}; // TODO: Measure whether iterating through `nextObject` or fast enumeration is // fastest. -// impl Iterator for NSEnumerator { -// type Item = Retained; +// impl Iterator for NSEnumerator { +// type Item = Retained; // // #[inline] -// fn next(&mut self) -> Option> { +// fn next(&mut self) -> Option> { // self.nextObject() // } // } -impl NSEnumerator { +impl NSEnumerator { /// Iterate over the enumerator's elements. #[inline] - pub fn iter(&self) -> Iter<'_, T> { + pub fn iter(&self) -> Iter<'_, ObjectType> { Iter(iter::Iter::new(self)) } @@ -32,13 +32,13 @@ impl NSEnumerator { /// The enumerator and the underlying collection must not be mutated while /// the iterator is alive. #[inline] - pub unsafe fn iter_unchecked(&self) -> IterUnchecked<'_, T> { + pub unsafe fn iter_unchecked(&self) -> IterUnchecked<'_, ObjectType> { IterUnchecked(iter::IterUnchecked::new(self)) } } -unsafe impl iter::FastEnumerationHelper for NSEnumerator { - type Item = T; +unsafe impl iter::FastEnumerationHelper for NSEnumerator { + type Item = ObjectType; #[inline] fn maybe_len(&self) -> Option { @@ -48,10 +48,10 @@ unsafe impl iter::FastEnumerationHelper for NSEnumerator { /// An iterator over the items in an enumerator. #[derive(Debug)] -pub struct Iter<'a, T: Message>(iter::Iter<'a, NSEnumerator>); +pub struct Iter<'a, ObjectType: Message>(iter::Iter<'a, NSEnumerator>); __impl_iter! { - impl<'a, T: Message> Iterator> for Iter<'a, T> { ... } + impl<'a, ObjectType: Message> Iterator> for Iter<'a, ObjectType> { ... } } /// An iterator over unretained items in an enumerator. @@ -61,28 +61,30 @@ __impl_iter! { /// The enumerator and the underlying collection must not be mutated while /// this is alive. #[derive(Debug)] -pub struct IterUnchecked<'a, T: Message + 'a>(iter::IterUnchecked<'a, NSEnumerator>); +pub struct IterUnchecked<'a, ObjectType: Message + 'a>( + iter::IterUnchecked<'a, NSEnumerator>, +); __impl_iter! { - impl<'a, T: Message> Iterator for IterUnchecked<'a, T> { ... } + 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>); +pub struct IntoIter(iter::IntoIter>); __impl_iter! { - impl Iterator> for IntoIter { ... } + impl Iterator> for IntoIter { ... } } __impl_into_iter! { - impl IntoIterator for &NSEnumerator { - type IntoIter = Iter<'_, T>; + impl IntoIterator for &NSEnumerator { + type IntoIter = Iter<'_, ObjectType>; } - impl IntoIterator for Retained> { + impl IntoIterator for Retained> { #[uses(new)] - type IntoIter = IntoIter; + type IntoIter = IntoIter; } } diff --git a/framework-crates/objc2-foundation/src/iter.rs b/framework-crates/objc2-foundation/src/iter.rs index 129b6a42a..9cdea1ca7 100644 --- a/framework-crates/objc2-foundation/src/iter.rs +++ b/framework-crates/objc2-foundation/src/iter.rs @@ -602,16 +602,16 @@ macro_rules! __impl_into_iter { () => {}; ( $(#[$m:meta])* - impl IntoIterator for &$ty:ident { - type IntoIter = $iter:ident<'_, T>; + impl<$param:ident: Message> IntoIterator for &$ty:ident<$param2:ident> { + type IntoIter = $iter:ident<'_, $param3:ident>; } $($rest:tt)* ) => { $(#[$m])* - impl<'a, T: Message> IntoIterator for &'a $ty { - type Item = Retained; - type IntoIter = $iter<'a, T>; + impl<'a, $param: Message> IntoIterator for &'a $ty<$param2> { + type Item = Retained<$param3>; + type IntoIter = $iter<'a, $param3>; #[inline] fn into_iter(self) -> Self::IntoIter { @@ -625,17 +625,17 @@ macro_rules! __impl_into_iter { }; ( $(#[$m:meta])* - impl IntoIterator for Retained<$ty:ident> { + impl<$param:ident: Message> IntoIterator for Retained<$ty:ident<$param2:ident>> { #[uses($new_fn:ident)] - type IntoIter = $into_iter:ident; + type IntoIter = $into_iter:ident<$param3:ident>; } $($rest:tt)* ) => { $(#[$m])* - impl objc2::rc::RetainedIntoIterator for $ty { - type Item = Retained; - type IntoIter = $into_iter; + impl<$param: Message> objc2::rc::RetainedIntoIterator for $ty<$param2> { + type Item = Retained<$param3>; + type IntoIter = $into_iter<$param3>; #[inline] fn retained_into_iter(this: Retained) -> Self::IntoIter { diff --git a/framework-crates/objc2-foundation/src/set.rs b/framework-crates/objc2-foundation/src/set.rs index ba3ce4cbd..835fe4b5d 100644 --- a/framework-crates/objc2-foundation/src/set.rs +++ b/framework-crates/objc2-foundation/src/set.rs @@ -11,7 +11,7 @@ use crate::iter; use crate::{util, NSMutableSet, NSSet}; /// Convenience creation methods. -impl NSSet { +impl NSSet { /// Creates an [`NSSet`] from a slice of `Retained`s. /// /// # Examples @@ -22,14 +22,14 @@ impl NSSet { /// let strs = ["one", "two", "three"].map(NSString::from_str); /// let set = NSSet::from_retained_slice(&strs); /// ``` - pub fn from_retained_slice(slice: &[Retained]) -> Retained { + pub fn from_retained_slice(slice: &[Retained]) -> Retained { let len = slice.len(); let ptr = util::retained_ptr_cast_const(slice.as_ptr()); // SAFETY: Same as `NSArray::from_retained_slice` unsafe { Self::initWithObjects_count(Self::alloc(), ptr, len) } } - pub fn from_slice(slice: &[&T]) -> Retained { + pub fn from_slice(slice: &[&ObjectType]) -> Retained { let len = slice.len(); let ptr = util::ref_ptr_cast_const(slice.as_ptr()); // SAFETY: Same as `NSArray::from_slice`. @@ -38,7 +38,7 @@ impl NSSet { } /// Convenience creation methods. -impl NSMutableSet { +impl NSMutableSet { /// Creates an [`NSMutableSet`] from a slice of `Retained`s. /// /// # Examples @@ -49,14 +49,14 @@ impl NSMutableSet { /// let strs = ["one", "two", "three"].map(NSString::from_str); /// let set = NSMutableSet::from_retained_slice(&strs); /// ``` - pub fn from_retained_slice(slice: &[Retained]) -> Retained { + pub fn from_retained_slice(slice: &[Retained]) -> Retained { let len = slice.len(); let ptr = util::retained_ptr_cast_const(slice.as_ptr()); // SAFETY: Same as `NSArray::from_retained_slice` unsafe { Self::initWithObjects_count(Self::alloc(), ptr, len) } } - pub fn from_slice(slice: &[&T]) -> Retained { + pub fn from_slice(slice: &[&ObjectType]) -> Retained { let len = slice.len(); let ptr = util::ref_ptr_cast_const(slice.as_ptr()); // SAFETY: Same as `NSArray::from_slice`. @@ -75,7 +75,7 @@ impl NSMutableSet { /// doing so - otherwise, we might end up accessing a deallocated object. /// /// [collections-own]: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-SW12 -impl NSSet { +impl NSSet { /// A direct reference to an arbitrary object in the set. /// /// Consider using the [`anyObject`](Self::anyObject) method instead, @@ -85,7 +85,7 @@ impl NSSet { /// /// The set must not be mutated while the reference is live. #[doc(alias = "anyObject")] - pub unsafe fn anyObject_unchecked(&self) -> Option<&T> { + pub unsafe fn anyObject_unchecked(&self) -> Option<&ObjectType> { // SAFETY: Upheld by caller. unsafe { msg_send![self, anyObject] } } @@ -99,7 +99,7 @@ impl NSSet { /// /// The set must not be mutated while the returned reference is live. #[doc(alias = "member:")] - pub unsafe fn member_unchecked(&self, object: &T) -> Option<&T> { + pub unsafe fn member_unchecked(&self, object: &ObjectType) -> Option<&ObjectType> { // SAFETY: Upheld by caller. unsafe { msg_send![self, member: object] } } @@ -117,13 +117,13 @@ impl NSSet { #[cfg(feature = "NSEnumerator")] #[doc(alias = "objectEnumerator")] #[inline] - pub unsafe fn iter_unchecked(&self) -> IterUnchecked<'_, T> { + pub unsafe fn iter_unchecked(&self) -> IterUnchecked<'_, ObjectType> { IterUnchecked(super::iter::IterUnchecked::new(self)) } } /// Various accessor methods. -impl NSSet { +impl NSSet { /// Returns the number of elements in the set. /// /// # Examples @@ -169,7 +169,7 @@ impl NSSet { #[cfg(feature = "NSEnumerator")] #[doc(alias = "objectEnumerator")] #[inline] - pub fn iter(&self) -> Iter<'_, T> { + pub fn iter(&self) -> Iter<'_, ObjectType> { Iter(super::iter::Iter::new(self)) } @@ -190,14 +190,14 @@ impl NSSet { /// assert_eq!(vec.len(), 3); /// ``` #[cfg(feature = "NSEnumerator")] - pub fn to_vec(&self) -> Vec> { + pub fn to_vec(&self) -> Vec> { self.iter().collect() } } #[cfg(feature = "NSEnumerator")] -unsafe impl iter::FastEnumerationHelper for NSSet { - type Item = T; +unsafe impl iter::FastEnumerationHelper for NSSet { + type Item = ObjectType; #[inline] fn maybe_len(&self) -> Option { @@ -206,8 +206,8 @@ unsafe impl iter::FastEnumerationHelper for NSSet { } #[cfg(feature = "NSEnumerator")] -unsafe impl iter::FastEnumerationHelper for NSMutableSet { - type Item = T; +unsafe impl iter::FastEnumerationHelper for NSMutableSet { + type Item = ObjectType; #[inline] fn maybe_len(&self) -> Option { @@ -218,21 +218,21 @@ unsafe impl iter::FastEnumerationHelper for NSMutableSet { /// An iterator over the items of a set. #[derive(Debug)] #[cfg(feature = "NSEnumerator")] -pub struct Iter<'a, T: Message>(iter::Iter<'a, NSSet>); +pub struct Iter<'a, ObjectType: Message>(iter::Iter<'a, NSSet>); #[cfg(feature = "NSEnumerator")] __impl_iter! { - impl<'a, T: Message> Iterator> for Iter<'a, T> { ... } + impl<'a, ObjectType: Message> Iterator> for Iter<'a, ObjectType> { ... } } /// An unchecked iterator over the items of a set. #[derive(Debug)] #[cfg(feature = "NSEnumerator")] -pub struct IterUnchecked<'a, T: Message>(iter::IterUnchecked<'a, NSSet>); +pub struct IterUnchecked<'a, ObjectType: Message>(iter::IterUnchecked<'a, NSSet>); #[cfg(feature = "NSEnumerator")] __impl_iter! { - impl<'a, T: Message> Iterator for IterUnchecked<'a, T> { ... } + impl<'a, ObjectType: Message> Iterator for IterUnchecked<'a, ObjectType> { ... } } /// An iterator over unretained items of a set. @@ -242,36 +242,36 @@ __impl_iter! { /// The set must not be mutated while this is alive. #[derive(Debug)] #[cfg(feature = "NSEnumerator")] -pub struct IntoIter(iter::IntoIter>); +pub struct IntoIter(iter::IntoIter>); #[cfg(feature = "NSEnumerator")] __impl_iter! { - impl<'a, T: Message> Iterator> for IntoIter { ... } + impl<'a, ObjectType: Message> Iterator> for IntoIter { ... } } #[cfg(feature = "NSEnumerator")] __impl_into_iter! { - impl IntoIterator for &NSSet { - type IntoIter = Iter<'_, T>; + impl IntoIterator for &NSSet { + type IntoIter = Iter<'_, ObjectType>; } - impl IntoIterator for &NSMutableSet { - type IntoIter = Iter<'_, T>; + impl IntoIterator for &NSMutableSet { + type IntoIter = Iter<'_, ObjectType>; } - impl IntoIterator for Retained> { + impl IntoIterator for Retained> { #[uses(new)] - type IntoIter = IntoIter; + type IntoIter = IntoIter; } - impl IntoIterator for Retained> { + impl IntoIterator for Retained> { #[uses(new_mutable)] - type IntoIter = IntoIter; + type IntoIter = IntoIter; } } #[cfg(feature = "NSEnumerator")] -impl fmt::Debug for NSSet { +impl fmt::Debug for NSSet { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_set().entries(self).finish() @@ -279,7 +279,7 @@ impl fmt::Debug for NSSet { } #[cfg(feature = "NSEnumerator")] -impl fmt::Debug for NSMutableSet { +impl fmt::Debug for NSMutableSet { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) @@ -287,53 +287,55 @@ impl fmt::Debug for NSMutableSet { } #[cfg(feature = "NSEnumerator")] -impl fmt::Debug for crate::NSCountedSet { +impl fmt::Debug for crate::NSCountedSet { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } -impl Extend> for &NSMutableSet { - fn extend>>(&mut self, iter: I) { +impl Extend> for &NSMutableSet { + fn extend>>(&mut self, iter: I) { iter.into_iter().for_each(move |item| { self.addObject(&item); }); } } -impl<'a, T: Message> Extend<&'a T> for &NSMutableSet { - fn extend>(&mut self, iter: I) { +impl<'a, ObjectType: Message> Extend<&'a ObjectType> for &NSMutableSet { + fn extend>(&mut self, iter: I) { iter.into_iter().for_each(move |item| { self.addObject(item); }); } } -impl<'a, T: Message + 'a> RetainedFromIterator<&'a T> for NSSet { - fn retained_from_iter>(iter: I) -> Retained { +impl<'a, ObjectType: Message + 'a> RetainedFromIterator<&'a ObjectType> for NSSet { + fn retained_from_iter>(iter: I) -> Retained { let vec = Vec::from_iter(iter); Self::from_slice(&vec) } } -impl RetainedFromIterator> for NSSet { - fn retained_from_iter>>(iter: I) -> Retained { +impl RetainedFromIterator> for NSSet { + fn retained_from_iter>>(iter: I) -> Retained { let vec = Vec::from_iter(iter); Self::from_retained_slice(&vec) } } -impl<'a, T: Message + 'a> RetainedFromIterator<&'a T> for NSMutableSet { - fn retained_from_iter>(iter: I) -> Retained { +impl<'a, ObjectType: Message + 'a> RetainedFromIterator<&'a ObjectType> + for NSMutableSet +{ + fn retained_from_iter>(iter: I) -> Retained { // TODO: Is this, or is using `initWithCapacity` the most optimal? let vec = Vec::from_iter(iter); Self::from_slice(&vec) } } -impl RetainedFromIterator> for NSMutableSet { - fn retained_from_iter>>(iter: I) -> Retained { +impl RetainedFromIterator> for NSMutableSet { + fn retained_from_iter>>(iter: I) -> Retained { // TODO: Is this, or is using `initWithCapacity` the most optimal? let vec = Vec::from_iter(iter); Self::from_retained_slice(&vec) diff --git a/framework-crates/objc2-foundation/src/to_owned.rs b/framework-crates/objc2-foundation/src/to_owned.rs index a94795c50..7560ca492 100644 --- a/framework-crates/objc2-foundation/src/to_owned.rs +++ b/framework-crates/objc2-foundation/src/to_owned.rs @@ -7,7 +7,7 @@ use objc2::Message; use crate::{NSCopying, NSMutableCopying}; #[cfg(feature = "NSArray")] -impl ToOwned for crate::NSArray { +impl ToOwned for crate::NSArray { type Owned = Retained; fn to_owned(&self) -> Self::Owned { self.copy() @@ -15,7 +15,7 @@ impl ToOwned for crate::NSArray { } #[cfg(feature = "NSArray")] -impl ToOwned for crate::NSMutableArray { +impl ToOwned for crate::NSMutableArray { type Owned = Retained; fn to_owned(&self) -> Self::Owned { self.mutableCopy() @@ -47,7 +47,7 @@ impl ToOwned for crate::NSException { } #[cfg(feature = "NSSet")] -impl ToOwned for crate::NSSet { +impl ToOwned for crate::NSSet { type Owned = Retained; fn to_owned(&self) -> Self::Owned { self.copy() @@ -55,7 +55,7 @@ impl ToOwned for crate::NSSet { } #[cfg(feature = "NSSet")] -impl ToOwned for crate::NSMutableSet { +impl ToOwned for crate::NSMutableSet { type Owned = Retained; fn to_owned(&self) -> Self::Owned { self.mutableCopy()