Skip to content

Commit f6b341e

Browse files
committed
Remove ShareId and default ownership for Id
This is a usability degradation, but before we know more about actual usage patterns this is the safer option; users will have to explicitly specify the ownership they're expecting. See SSheldon/rust-objc-id#4
1 parent e08e87d commit f6b341e

File tree

9 files changed

+40
-43
lines changed

9 files changed

+40
-43
lines changed

objc2/src/rc/id.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ use crate::Message;
5959
/// ```no_run
6060
/// use objc2::msg_send;
6161
/// use objc2::runtime::{Class, Object};
62-
/// use objc2::rc::{Id, WeakId, Shared};
62+
/// use objc2::rc::{Id, Owned, Shared, WeakId};
6363
///
6464
/// let cls = Class::get("NSObject").unwrap();
65-
/// let obj: Id<Object> = unsafe {
65+
/// let obj: Id<Object, Owned> = unsafe {
6666
/// Id::new(msg_send![cls, new])
6767
/// };
6868
/// // obj will be released when it goes out of scope
@@ -97,7 +97,7 @@ use crate::Message;
9797
#[repr(transparent)]
9898
// TODO: Figure out if `Message` bound on `T` would be better here?
9999
// TODO: Add `?Sized + ptr::Thin` bound on `T` to allow for extern types
100-
pub struct Id<T, O: Ownership = Owned> {
100+
pub struct Id<T, O: Ownership> {
101101
/// A pointer to the contained object. The pointer is always retained.
102102
///
103103
/// It is important that this is `NonNull`, since we want to dereference
@@ -296,7 +296,7 @@ impl<T: Message> Clone for Id<T, Shared> {
296296
#[doc(alias = "objc_retain")]
297297
#[doc(alias = "retain")]
298298
#[inline]
299-
fn clone(&self) -> ShareId<T> {
299+
fn clone(&self) -> Self {
300300
// SAFETY: The pointer is valid
301301
unsafe { Id::retain(self.ptr) }
302302
}
@@ -509,14 +509,11 @@ impl<T, O: Ownership> Unpin for Id<T, O> {}
509509

510510
// TODO: When stabilized impl Fn traits & CoerceUnsized
511511

512-
/// A convenient alias for a shared [`Id`].
513-
pub type ShareId<T> = Id<T, Shared>;
514-
515512
#[cfg(test)]
516513
mod tests {
517514
use core::ptr::NonNull;
518515

519-
use super::{Id, Shared};
516+
use super::{Id, Owned, Shared};
520517
use crate::rc::autoreleasepool;
521518
use crate::runtime::Object;
522519
use crate::{class, msg_send};
@@ -543,7 +540,7 @@ mod tests {
543540
#[test]
544541
fn test_clone() {
545542
let cls = class!(NSObject);
546-
let obj: Id<Object> = unsafe {
543+
let obj: Id<Object, Owned> = unsafe {
547544
let obj: *mut Object = msg_send![cls, alloc];
548545
let obj: *mut Object = msg_send![obj, init];
549546
Id::new(NonNull::new_unchecked(obj))

objc2/src/rc/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ mod ownership;
3232
mod weak_id;
3333

3434
pub use self::autorelease::{autoreleasepool, AutoreleasePool, AutoreleaseSafe};
35-
pub use self::id::{Id, ShareId};
35+
pub use self::id::Id;
3636
pub use self::ownership::{Owned, Ownership, Shared};
3737
pub use self::weak_id::WeakId;
3838

objc2_foundation/src/array.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use core::marker::PhantomData;
55
use core::ops::{Index, Range};
66
use core::ptr::NonNull;
77

8-
use objc2::rc::{Id, Owned, Ownership, ShareId, Shared};
8+
use objc2::rc::{Id, Owned, Ownership, Shared};
99
use objc2::runtime::{Class, Object};
1010
use objc2::{class, msg_send};
1111
use objc2::{Encode, Encoding};
@@ -71,7 +71,7 @@ unsafe impl Encode for NSRange {
7171
Encoding::Struct("_NSRange", &[usize::ENCODING, usize::ENCODING]);
7272
}
7373

74-
unsafe fn from_refs<A>(refs: &[&A::Item]) -> Id<A>
74+
unsafe fn from_refs<A>(refs: &[&A::Item]) -> Id<A, Owned>
7575
where
7676
A: INSArray,
7777
{
@@ -126,7 +126,7 @@ pub trait INSArray: INSObject {
126126
}
127127
}
128128

129-
fn from_vec(vec: Vec<Id<Self::Item, Self::Own>>) -> Id<Self> {
129+
fn from_vec(vec: Vec<Id<Self::Item, Self::Own>>) -> Id<Self, Owned> {
130130
let refs: Vec<&Self::Item> = vec.iter().map(|obj| &**obj).collect();
131131
unsafe { from_refs(&refs) }
132132
}
@@ -145,7 +145,7 @@ pub trait INSArray: INSObject {
145145
self.objects_in_range(0..self.count())
146146
}
147147

148-
fn into_vec(array: Id<Self>) -> Vec<Id<Self::Item, Self::Own>> {
148+
fn into_vec(array: Id<Self, Owned>) -> Vec<Id<Self::Item, Self::Own>> {
149149
array
150150
.to_vec()
151151
.into_iter()
@@ -163,23 +163,23 @@ pub trait INSArray: INSObject {
163163
}
164164
}
165165

166-
fn shared_object_at(&self, index: usize) -> ShareId<Self::Item>
166+
fn shared_object_at(&self, index: usize) -> Id<Self::Item, Shared>
167167
where
168168
Self: INSArray<Own = Shared>,
169169
{
170170
let obj = self.object_at(index);
171171
unsafe { Id::retain(obj.into()) }
172172
}
173173

174-
fn from_slice(slice: &[ShareId<Self::Item>]) -> Id<Self>
174+
fn from_slice(slice: &[Id<Self::Item, Shared>]) -> Id<Self, Owned>
175175
where
176176
Self: INSArray<Own = Shared>,
177177
{
178178
let refs: Vec<&Self::Item> = slice.iter().map(|obj| &**obj).collect();
179179
unsafe { from_refs(&refs) }
180180
}
181181

182-
fn to_shared_vec(&self) -> Vec<ShareId<Self::Item>>
182+
fn to_shared_vec(&self) -> Vec<Id<Self::Item, Shared>>
183183
where
184184
Self: INSArray<Own = Shared>,
185185
{
@@ -415,9 +415,9 @@ mod tests {
415415

416416
use super::{INSArray, INSMutableArray, NSArray, NSMutableArray};
417417
use crate::{INSObject, INSString, NSObject, NSString};
418-
use objc2::rc::Id;
418+
use objc2::rc::{Id, Owned};
419419

420-
fn sample_array(len: usize) -> Id<NSArray<NSObject>> {
420+
fn sample_array(len: usize) -> Id<NSArray<NSObject>, Owned> {
421421
let mut vec = Vec::with_capacity(len);
422422
for _ in 0..len {
423423
vec.push(NSObject::new());
@@ -441,7 +441,7 @@ mod tests {
441441
assert!(array.first_object().unwrap() == array.object_at(0));
442442
assert!(array.last_object().unwrap() == array.object_at(3));
443443

444-
let empty_array: Id<NSArray<NSObject>> = INSObject::new();
444+
let empty_array: Id<NSArray<NSObject>, Owned> = INSObject::new();
445445
assert!(empty_array.first_object().is_none());
446446
assert!(empty_array.last_object().is_none());
447447
}

objc2_foundation/src/data.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use core::{ffi::c_void, ptr::NonNull};
66

77
use super::{INSCopying, INSMutableCopying, INSObject, NSRange};
88
use objc2::msg_send;
9-
use objc2::rc::Id;
9+
use objc2::rc::{Id, Owned};
1010
#[cfg(feature = "block")]
1111
use objc2_block::{Block, ConcreteBlock};
1212

@@ -30,7 +30,7 @@ pub trait INSData: INSObject {
3030
unsafe { slice::from_raw_parts(ptr, len) }
3131
}
3232

33-
fn with_bytes(bytes: &[u8]) -> Id<Self> {
33+
fn with_bytes(bytes: &[u8]) -> Id<Self, Owned> {
3434
let cls = Self::class();
3535
let bytes_ptr = bytes.as_ptr() as *const c_void;
3636
unsafe {
@@ -42,7 +42,7 @@ pub trait INSData: INSObject {
4242
}
4343

4444
#[cfg(feature = "block")]
45-
fn from_vec(bytes: Vec<u8>) -> Id<Self> {
45+
fn from_vec(bytes: Vec<u8>) -> Id<Self, Owned> {
4646
let capacity = bytes.capacity();
4747
let dealloc = ConcreteBlock::new(move |bytes: *mut c_void, len: usize| unsafe {
4848
// Recreate the Vec and let it drop

objc2_foundation/src/dictionary.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use core::marker::PhantomData;
44
use core::ops::Index;
55
use core::ptr::{self, NonNull};
66

7-
use objc2::rc::{Id, Owned, Ownership, ShareId};
7+
use objc2::rc::{Id, Owned, Ownership, Shared};
88
use objc2::runtime::Class;
99
use objc2::{class, msg_send};
1010

1111
use super::{INSCopying, INSFastEnumeration, INSObject, NSArray, NSEnumerator, NSSharedArray};
1212

13-
unsafe fn from_refs<D, T>(keys: &[&T], vals: &[&D::Value]) -> Id<D>
13+
unsafe fn from_refs<D, T>(keys: &[&T], vals: &[&D::Value]) -> Id<D, Owned>
1414
where
1515
D: INSDictionary,
1616
T: INSCopying<Output = D::Key>,
@@ -93,22 +93,22 @@ pub trait INSDictionary: INSObject {
9393
}
9494
}
9595

96-
fn keys_array(&self) -> Id<NSSharedArray<Self::Key>> {
96+
fn keys_array(&self) -> Id<NSSharedArray<Self::Key>, Owned> {
9797
unsafe {
9898
let keys = msg_send![self, allKeys];
9999
Id::retain(NonNull::new_unchecked(keys))
100100
}
101101
}
102102

103-
fn from_keys_and_objects<T>(keys: &[&T], vals: Vec<Id<Self::Value, Self::Own>>) -> Id<Self>
103+
fn from_keys_and_objects<T>(keys: &[&T], vals: Vec<Id<Self::Value, Self::Own>>) -> Id<Self, Owned>
104104
where
105105
T: INSCopying<Output = Self::Key>,
106106
{
107107
let vals_refs: Vec<&Self::Value> = vals.iter().map(|obj| &**obj).collect();
108108
unsafe { from_refs(keys, &vals_refs) }
109109
}
110110

111-
fn into_values_array(dict: Id<Self>) -> Id<NSArray<Self::Value, Self::Own>> {
111+
fn into_values_array(dict: Id<Self, Owned>) -> Id<NSArray<Self::Value, Self::Own>, Owned> {
112112
unsafe {
113113
let vals = msg_send![dict, allValues];
114114
Id::retain(NonNull::new_unchecked(vals))
@@ -117,8 +117,8 @@ pub trait INSDictionary: INSObject {
117117
}
118118

119119
pub struct NSDictionary<K, V> {
120-
key: PhantomData<ShareId<K>>,
121-
obj: PhantomData<Id<V>>,
120+
key: PhantomData<Id<K, Shared>>,
121+
obj: PhantomData<Id<V, Owned>>,
122122
}
123123

124124
object_impl!(NSDictionary<K, V>);
@@ -166,12 +166,12 @@ where
166166
#[cfg(test)]
167167
mod tests {
168168
use alloc::vec;
169-
use objc2::rc::Id;
169+
use objc2::rc::{Owned, Id};
170170

171171
use super::{INSDictionary, NSDictionary};
172172
use crate::{INSArray, INSObject, INSString, NSObject, NSString};
173173

174-
fn sample_dict(key: &str) -> Id<NSDictionary<NSString, NSObject>> {
174+
fn sample_dict(key: &str) -> Id<NSDictionary<NSString, NSObject>, Owned> {
175175
let string = NSString::from_str(key);
176176
let obj = NSObject::new();
177177
NSDictionary::from_keys_and_objects(&[&*string], vec![obj])

objc2_foundation/src/enumerator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use core::ptr::NonNull;
55
use core::slice;
66
use std::os::raw::c_ulong;
77

8-
use objc2::rc::Id;
8+
use objc2::rc::{Id, Owned};
99
use objc2::runtime::Object;
1010
use objc2::{msg_send, Encode, Encoding, RefEncode};
1111

@@ -15,7 +15,7 @@ pub struct NSEnumerator<'a, T>
1515
where
1616
T: INSObject,
1717
{
18-
id: Id<Object>,
18+
id: Id<Object, Owned>,
1919
item: PhantomData<&'a T>,
2020
}
2121

objc2_foundation/src/object.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::any::Any;
22
use core::ptr::NonNull;
33

44
use objc2::msg_send;
5-
use objc2::rc::{Id, ShareId};
5+
use objc2::rc::{Id, Owned, Shared};
66
use objc2::runtime::{Class, BOOL, NO};
77
use objc2::Message;
88

@@ -29,7 +29,7 @@ pub trait INSObject: Any + Sized + Message {
2929
result != NO
3030
}
3131

32-
fn description(&self) -> ShareId<NSString> {
32+
fn description(&self) -> Id<NSString, Shared> {
3333
unsafe {
3434
let result: *mut NSString = msg_send![self, description];
3535
// TODO: Verify that description always returns a non-null string
@@ -42,7 +42,7 @@ pub trait INSObject: Any + Sized + Message {
4242
result != NO
4343
}
4444

45-
fn new() -> Id<Self> {
45+
fn new() -> Id<Self, Owned> {
4646
let cls = Self::class();
4747
unsafe { Id::new(msg_send![cls, new]) }
4848
}

objc2_foundation/src/string.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ use core::str;
66
use std::os::raw::c_char;
77

88
use objc2::msg_send;
9-
use objc2::rc::{Id, ShareId};
9+
use objc2::rc::{Id, Owned, Shared};
1010

1111
use super::INSObject;
1212

1313
pub trait INSCopying: INSObject {
1414
type Output: INSObject;
1515

16-
fn copy(&self) -> ShareId<Self::Output> {
16+
fn copy(&self) -> Id<Self::Output, Shared> {
1717
unsafe {
1818
let obj: *mut Self::Output = msg_send![self, copy];
1919
Id::new(NonNull::new_unchecked(obj))
@@ -24,7 +24,7 @@ pub trait INSCopying: INSObject {
2424
pub trait INSMutableCopying: INSObject {
2525
type Output: INSObject;
2626

27-
fn mutable_copy(&self) -> Id<Self::Output> {
27+
fn mutable_copy(&self) -> Id<Self::Output, Owned> {
2828
unsafe {
2929
let obj: *mut Self::Output = msg_send![self, mutableCopy];
3030
Id::new(NonNull::new_unchecked(obj))
@@ -58,7 +58,7 @@ pub trait INSString: INSObject {
5858
}
5959
}
6060

61-
fn from_str(string: &str) -> Id<Self> {
61+
fn from_str(string: &str) -> Id<Self, Owned> {
6262
let cls = Self::class();
6363
let bytes = string.as_ptr() as *const c_void;
6464
unsafe {

objc2_foundation/src/value.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use core::str;
88
use std::ffi::{CStr, CString};
99
use std::os::raw::c_char;
1010

11-
use objc2::rc::Id;
11+
use objc2::rc::{Id, Owned};
1212
use objc2::runtime::Class;
1313
use objc2::Encode;
1414
use objc2::{class, msg_send};
@@ -36,7 +36,7 @@ pub trait INSValue: INSObject {
3636
}
3737
}
3838

39-
fn from_value(value: Self::Value) -> Id<Self> {
39+
fn from_value(value: Self::Value) -> Id<Self, Owned> {
4040
let cls = Self::class();
4141
let value_ptr: *const Self::Value = &value;
4242
let bytes = value_ptr as *const c_void;

0 commit comments

Comments
 (0)