Skip to content

Commit

Permalink
Remove generics from NSValue, and add NSNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jul 29, 2022
1 parent ed4f140 commit 29d26f5
Show file tree
Hide file tree
Showing 12 changed files with 643 additions and 148 deletions.
6 changes: 6 additions & 0 deletions objc2/CHANGELOG_FOUNDATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## Unreleased - YYYY-MM-DD

### Added
* Added `NSNumber`.
* Implement `UnwindSafe` and `RefUnwindSafe` for all objects.
* Implemented `IntoIterator` for references to `NSArray`, `NSMutableArray`,
`NSData` and `NSMutableData`.
* Implemented `Extend` for `NSMutableArray`.
* Add extra `Extend<&u8>` impl for `NSMutableData`.
* Added function `NSValue::contains_encoding` for determining if the encoding
of the `NSValue` matches the encoding of the given type.

### Changed
* **BREAKING**: Moved from external crate `objc2_foundation` into
`objc2::foundation`.
* **BREAKING**: Made `NSValue` not generic any more. While we loose some
type-safety from this, it makes `NSValue` much more useful in the real
world!

### Fixed
* Made `Debug` impls for all objects print something useful.
Expand Down
6 changes: 3 additions & 3 deletions objc2/src/foundation/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ mod tests {
use alloc::vec::Vec;

use super::*;
use crate::foundation::{NSString, NSValue};
use crate::foundation::{NSNumber, NSString};
use crate::rc::autoreleasepool;

fn sample_array(len: usize) -> Id<NSArray<NSObject, Owned>, Owned> {
Expand All @@ -254,10 +254,10 @@ mod tests {
NSArray::from_vec(vec)
}

fn sample_number_array(len: u8) -> Id<NSArray<NSValue<u8>, Shared>, Shared> {
fn sample_number_array(len: u8) -> Id<NSArray<NSNumber, Shared>, Shared> {
let mut vec = Vec::with_capacity(len as usize);
for i in 0..len {
vec.push(NSValue::new(i));
vec.push(NSNumber::new_u8(i));
}
NSArray::from_vec(vec)
}
Expand Down
10 changes: 5 additions & 5 deletions objc2/src/foundation/enumerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,29 +168,29 @@ impl<'a, C: NSFastEnumeration + ?Sized> Iterator for NSFastEnumerator<'a, C> {
#[cfg(test)]
mod tests {
use super::NSFastEnumeration;
use crate::foundation::{NSArray, NSValue};
use crate::foundation::{NSArray, NSNumber};

#[test]
fn test_enumerator() {
let vec = (0usize..4).map(NSValue::new).collect();
let vec = (0..4).map(NSNumber::new_usize).collect();
let array = NSArray::from_vec(vec);

let enumerator = array.iter();
assert_eq!(enumerator.count(), 4);

let enumerator = array.iter();
assert!(enumerator.enumerate().all(|(i, obj)| obj.get() == i));
assert!(enumerator.enumerate().all(|(i, obj)| obj.as_usize() == i));
}

#[test]
fn test_fast_enumerator() {
let vec = (0usize..4).map(NSValue::new).collect();
let vec = (0..4).map(NSNumber::new_usize).collect();
let array = NSArray::from_vec(vec);

let enumerator = array.iter_fast();
assert_eq!(enumerator.count(), 4);

let enumerator = array.iter_fast();
assert!(enumerator.enumerate().all(|(i, obj)| obj.get() == i));
assert!(enumerator.enumerate().all(|(i, obj)| obj.as_usize() == i));
}
}
5 changes: 4 additions & 1 deletion objc2/src/foundation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub use self::mutable_array::NSMutableArray;
pub use self::mutable_attributed_string::NSMutableAttributedString;
pub use self::mutable_data::NSMutableData;
pub use self::mutable_string::NSMutableString;
pub use self::number::NSNumber;
pub use self::object::NSObject;
pub use self::process_info::NSProcessInfo;
pub use self::range::NSRange;
Expand Down Expand Up @@ -77,6 +78,7 @@ mod mutable_array;
mod mutable_attributed_string;
mod mutable_data;
mod mutable_string;
mod number;
mod object;
mod process_info;
mod range;
Expand Down Expand Up @@ -141,6 +143,7 @@ mod tests {
assert_auto_traits::<NSMutableAttributedString>();
assert_auto_traits::<NSMutableData>();
assert_auto_traits::<NSMutableString>();
assert_auto_traits::<NSNumber>();
// assert_auto_traits::<NSObject>(); // Intentional
assert_auto_traits::<NSProcessInfo>();
assert_auto_traits::<NSRange>();
Expand All @@ -149,7 +152,7 @@ mod tests {
assert_auto_traits::<NSThread>();
#[cfg(not(macos_10_7))]
assert_auto_traits::<NSUUID>();
assert_auto_traits::<NSValue<i32>>();
// assert_auto_traits::<NSValue>(); // Intentional
assert_unwindsafe::<NSZone>(); // Intentional
}
}
Loading

0 comments on commit 29d26f5

Please sign in to comment.