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

Make Id #[fundamental] #50

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ jobs:
# Use --no-fail-fast, except with dinghy
TESTARGS: ${{ matrix.dinghy && ' ' || '--no-fail-fast' }} ${{ matrix.test-args }}
FEATURES: ${{ matrix.features || 'malloc,block,exception,catch_all,verify_message' }}
UNSTABLE_FEATURES: ${{ matrix.unstable-features || 'unstable-autoreleasesafe' }}
UNSTABLE_FEATURES: ${{ matrix.unstable-features || 'unstable-autoreleasesafe,unstable-id-fundamental' }}

runs-on: ${{ matrix.os }}

Expand Down
6 changes: 3 additions & 3 deletions objc2-foundation/examples/basic_usage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use objc2::rc::autoreleasepool;
use objc2::rc::{autoreleasepool, Id};
use objc2_foundation::{NSArray, NSCopying, NSDictionary, NSObject, NSString};

fn main() {
Expand All @@ -14,14 +14,14 @@ fn main() {

// Create an NSArray from a Vec
let objs = vec![obj, obj2];
let array = NSArray::from_vec(objs);
let array: Id<NSArray<_, _>, _> = objs.into();
for obj in array.iter() {
println!("{:?}", obj);
}
println!("{}", array.len());

// Turn the NSArray back into a Vec
let mut objs = NSArray::into_vec(array);
let mut objs = Vec::from(array);
let obj = objs.pop().unwrap();

// Create an NSString from a str slice
Expand Down
43 changes: 40 additions & 3 deletions objc2-foundation/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,37 @@ impl<T: Message> DefaultId for NSArray<T, Shared> {
}
}

impl<T: Message, O: Ownership> From<Id<NSArray<T, O>, Owned>> for Vec<Id<T, O>> {
fn from(array: Id<NSArray<T, O>, Owned>) -> Self {
let vec: Vec<&T> = (&*array).into();
vec.into_iter()
.map(|obj| unsafe { Id::retain(obj.into()) })
.collect()
}
}

impl<T: Message, O: Ownership> From<Vec<Id<T, O>>> for Id<NSArray<T, O>, Owned> {
fn from(vec: Vec<Id<T, O>>) -> Self {
unsafe { from_refs(vec.as_slice_ref()) }
}
}

impl<'a, T: Message, O: Ownership> From<&'a NSArray<T, O>> for Vec<&'a T> {
fn from(array: &'a NSArray<T, O>) -> Self {
array.objects_in_range(0..array.count())
}
}

impl<T: Message> From<&'_ NSArray<T, Shared>> for Vec<Id<T, Shared>> {
fn from(array: &NSArray<T, Shared>) -> Self {
array
.objects_in_range(0..array.count())
.into_iter()
.map(|obj| unsafe { Id::retain(obj.into()) })
.collect()
}
}

impl<T: Message, O: Ownership> NSMutableArray<T, O> {
unsafe_def_fn!(pub fn new -> Owned);

Expand Down Expand Up @@ -378,6 +409,12 @@ impl<T: Message, O: Ownership> DefaultId for NSMutableArray<T, O> {
}
}

impl<T: Message, O: Ownership> From<Vec<Id<T, O>>> for Id<NSMutableArray<T, O>, Owned> {
fn from(vec: Vec<Id<T, O>>) -> Self {
unsafe { from_refs(vec.as_slice_ref()) }
}
}

#[cfg(test)]
mod tests {
use alloc::format;
Expand All @@ -395,7 +432,7 @@ mod tests {
for _ in 0..len {
vec.push(NSObject::new());
}
NSArray::from_vec(vec)
vec.into()
}

fn sample_number_array(len: u8) -> Id<NSArray<NSValue<u8>, Shared>, Shared> {
Expand Down Expand Up @@ -517,7 +554,7 @@ mod tests {
fn test_into_vec() {
let array = sample_array(4);

let vec = NSArray::into_vec(array);
let vec = Vec::from(array);
assert_eq!(vec.len(), 4);
}

Expand Down Expand Up @@ -566,7 +603,7 @@ mod tests {
#[test]
fn test_sort() {
let strings = vec![NSString::from_str("hello"), NSString::from_str("hi")];
let mut strings = NSMutableArray::from_vec(strings);
let mut strings: Id<NSMutableArray<_, _>, Owned> = strings.into();

autoreleasepool(|pool| {
strings.sort_by(|s1, s2| s1.as_str(pool).len().cmp(&s2.as_str(pool).len()));
Expand Down
11 changes: 7 additions & 4 deletions objc2-foundation/src/enumerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,16 @@ impl<'a, C: NSFastEnumeration + ?Sized> Iterator for NSFastEnumerator<'a, C> {

#[cfg(test)]
mod tests {
use alloc::vec::Vec;
use objc2::rc::Id;

use super::NSFastEnumeration;
use crate::{NSArray, NSValue};

#[test]
fn test_enumerator() {
let vec = (0usize..4).map(NSValue::new).collect();
let array = NSArray::from_vec(vec);
let vec: Vec<_> = (0usize..4).map(NSValue::new).collect();
let array: Id<NSArray<_, _>, _> = vec.into();

let enumerator = array.iter();
assert_eq!(enumerator.count(), 4);
Expand All @@ -184,8 +187,8 @@ mod tests {

#[test]
fn test_fast_enumerator() {
let vec = (0usize..4).map(NSValue::new).collect();
let array = NSArray::from_vec(vec);
let vec: Vec<_> = (0usize..4).map(NSValue::new).collect();
let array: Id<NSArray<_, _>, _> = vec.into();

let enumerator = array.iter_fast();
assert_eq!(enumerator.count(), 4);
Expand Down
3 changes: 3 additions & 0 deletions objc2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ malloc = ["malloc_buf"]
# Uses nightly features to make AutoreleasePool zero-cost even in debug mode
unstable-autoreleasesafe = []

# Marks `rc::Id` with nightly `fundamental` feature.
unstable-id-fundamental = []

# Runtime selection. See `objc-sys` for details.
apple = ["objc-sys/apple"]
gnustep-1-7 = ["objc-sys/gnustep-1-7"]
Expand Down
1 change: 1 addition & 0 deletions objc2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
feature = "unstable-autoreleasesafe",
feature(negative_impls, auto_traits)
)]
#![cfg_attr(feature = "unstable-id-fundamental", feature(fundamental))]
#![warn(elided_lifetimes_in_paths)]
#![warn(missing_docs)]
#![deny(non_ascii_idents)]
Expand Down
1 change: 1 addition & 0 deletions objc2/src/rc/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ use crate::Message;
/// let cloned: Id<T, Shared> = shared.clone();
/// // Do something with `&T` here
/// ```
#[cfg_attr(feature = "unstable-id-fundamental", fundamental)]
#[repr(transparent)]
// TODO: Figure out if `Message` bound on `T` would be better here?
// TODO: Add `ptr::Thin` bound on `T` to allow for only extern types
Expand Down