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

Id conversion traits #48

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
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, FromId, Id, IntoId};
use objc2_foundation::{
INSArray, INSCopying, INSDictionary, INSString, NSArray, NSDictionary, NSObject, NSString,
};
Expand All @@ -13,14 +13,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_id();
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_id(array);
let obj = objs.pop().unwrap();

// Create an NSString from a str slice
Expand Down
89 changes: 59 additions & 30 deletions objc2-foundation/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ pub unsafe trait INSArray: INSObject {
}
}

fn from_vec(vec: Vec<Id<Self::Item, Self::ItemOwnership>>) -> Id<Self, Self::Ownership> {
unsafe { from_refs(vec.as_slice_ref()) }
}

fn objects_in_range(&self, range: Range<usize>) -> Vec<&Self::Item> {
let range = NSRange::from(range);
let mut vec = Vec::with_capacity(range.length);
Expand All @@ -126,35 +122,12 @@ pub unsafe trait INSArray: INSObject {
vec
}

fn to_vec(&self) -> Vec<&Self::Item> {
self.objects_in_range(0..self.len())
}

// TODO: Take Id<Self, Self::ItemOwnership> ?
fn into_vec(array: Id<Self, Owned>) -> Vec<Id<Self::Item, Self::ItemOwnership>> {
array
.to_vec()
.into_iter()
.map(|obj| unsafe { Id::retain(obj.into()) })
.collect()
}

fn from_slice(slice: &[Id<Self::Item, Shared>]) -> Id<Self, Self::Ownership>
where
Self: INSArray<ItemOwnership = Shared>,
{
unsafe { from_refs(slice.as_slice_ref()) }
}

fn to_shared_vec(&self) -> Vec<Id<Self::Item, Shared>>
where
Self: INSArray<ItemOwnership = Shared>,
{
self.to_vec()
.into_iter()
.map(|obj| unsafe { Id::retain(obj.into()) })
.collect()
}
}

/// TODO
Expand Down Expand Up @@ -211,6 +184,52 @@ impl<T: INSObject, O: Ownership> Index<usize> for NSArray<T, O> {
}
}

impl<T, O> FromId<NSArray<T, O>, Owned> for Vec<Id<T, O>>
where
T: INSObject,
O: Ownership,
{
fn from_id(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, O> IntoId<NSArray<T, O>, Owned> for Vec<Id<T, O>>
where
T: INSObject,
O: Ownership,
{
fn into_id(self) -> Id<NSArray<T, O>, Owned> {
unsafe { from_refs(self.as_slice_ref()) }
}
}

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

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

pub unsafe trait INSMutableArray: INSArray {
#[doc(alias = "addObject:")]
fn push(&mut self, obj: Id<Self::Item, Self::ItemOwnership>) {
Expand Down Expand Up @@ -358,6 +377,16 @@ impl<T: INSObject, O: Ownership> Index<usize> for NSMutableArray<T, O> {
}
}

impl<T, O> IntoId<NSMutableArray<T, O>, Owned> for Vec<Id<T, O>>
where
T: INSObject,
O: Ownership,
{
fn into_id(self) -> Id<NSMutableArray<T, O>, Owned> {
unsafe { from_refs(self.as_slice_ref()) }
}
}

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

fn retain_count<T: INSObject>(obj: &T) -> usize {
Expand Down Expand Up @@ -455,7 +484,7 @@ mod tests {
fn test_into_vec() {
let array = sample_array(4);

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

Expand Down Expand Up @@ -504,7 +533,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_id();

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 @@ -169,13 +169,16 @@ impl<'a, C: INSFastEnumeration> Iterator for NSFastEnumerator<'a, C> {

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

use super::INSFastEnumeration;
use crate::{INSArray, INSValue, NSArray, NSValue};

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

let enumerator = array.iter();
assert!(enumerator.count() == 4);
Expand All @@ -186,8 +189,8 @@ mod tests {

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

let enumerator = array.enumerator();
assert!(enumerator.count() == 4);
Expand Down
50 changes: 50 additions & 0 deletions objc2/src/rc/id_traits.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Helper traits for Id.

use core::convert::Infallible;

use super::{Id, Owned, Ownership};
use crate::Message;

Expand Down Expand Up @@ -70,3 +72,51 @@ impl<T: DefaultId> Default for Id<T, T::Ownership> {
T::default_id()
}
}

pub trait FromId<T, O: Ownership> {
fn from_id(obj: Id<T, O>) -> Self;
}

pub trait TryFromId<T, O: Ownership>: Sized {
type Error;
fn try_from_id(obj: Id<T, O>) -> Result<Self, Self::Error>;
}

impl<T, O: Ownership, U: FromId<T, O>> TryFromId<T, O> for U {
type Error = Infallible;

fn try_from_id(obj: Id<T, O>) -> Result<Self, Self::Error> {
Ok(FromId::from_id(obj))
}
}

/// TODO.
///
/// This is similar to [`Into`] in that it is implemented automatically for
/// all [`Id`] that implement [`FromId`]; but you will have to implement this
/// yourself in many more cases!
pub trait IntoId<T, O: Ownership> {
fn into_id(self) -> Id<T, O>;
}

impl<T, O: Ownership, U> IntoId<U, O> for Id<T, O>
where
Id<U, O>: FromId<T, O>,
{
fn into_id(self) -> Id<U, O> {
FromId::from_id(self)
}
}

pub trait TryIntoId<T, O: Ownership> {
type Error;
fn try_into_id(self) -> Result<Id<T, O>, Self::Error>;
}

impl<T, O: Ownership, U: IntoId<T, O>> TryIntoId<T, O> for U {
type Error = Infallible;

fn try_into_id(self) -> Result<Id<T, O>, Self::Error> {
Ok(IntoId::into_id(self))
}
}
2 changes: 1 addition & 1 deletion objc2/src/rc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mod weak_id;

pub use self::autorelease::{autoreleasepool, AutoreleasePool, AutoreleaseSafe};
pub use self::id::Id;
pub use self::id_traits::{DefaultId, SliceId, SliceIdMut};
pub use self::id_traits::{DefaultId, FromId, IntoId, SliceId, SliceIdMut, TryFromId, TryIntoId};
pub use self::ownership::{Owned, Ownership, Shared};
pub use self::weak_id::WeakId;

Expand Down