Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve NSValue and add NSNumber #226

Merged
merged 2 commits into from
Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions objc2/CHANGELOG_FOUNDATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@ 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.
* Added functions `get_range`, `get_point`, `get_size` and `get_rect` to
`NSValue` to help safely returning various types it will commonly contain.

### 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));
}
}
6 changes: 3 additions & 3 deletions objc2/src/foundation/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ pub type CGFloat = InnerFloat;
not(all(target_os = "macos", target_pointer_width = "32"))
))]
mod names {
pub(super) const POINT: &str = "_CGPoint";
pub(super) const SIZE: &str = "_CGSize";
pub(super) const RECT: &str = "_CGRect";
pub(super) const POINT: &str = "CGPoint";
pub(super) const SIZE: &str = "CGSize";
pub(super) const RECT: &str = "CGRect";
Comment on lines +28 to +30
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit unsure about this, but it seems like it is what NSValue::encoding returns when created from one of these

}

#[cfg(any(
Expand Down
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