Skip to content

Commit 08ae307

Browse files
committed
Change null handling when creating Ids
1 parent 60f905b commit 08ae307

27 files changed

+122
-166
lines changed

objc2-foundation/examples/class_with_lifetime.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::marker::PhantomData;
2-
use std::ptr::NonNull;
32
use std::sync::Once;
43

54
use objc2::declare::ClassDecl;
@@ -30,7 +29,7 @@ impl<'a> MyObject<'a> {
3029
unsafe {
3130
let obj: *mut Self = msg_send![Self::class(), alloc];
3231
let obj: *mut Self = msg_send![obj, initWithPtr: number_ptr];
33-
Id::new(NonNull::new_unchecked(obj))
32+
Id::new(obj).unwrap()
3433
}
3534
}
3635

objc2-foundation/examples/custom_class.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::ptr::NonNull;
21
use std::sync::Once;
32

43
use objc2::declare::ClassDecl;
@@ -29,7 +28,7 @@ static MYOBJECT_REGISTER_CLASS: Once = Once::new();
2928
impl MYObject {
3029
fn new() -> Id<Self, Owned> {
3130
let cls = Self::class();
32-
unsafe { Id::new(NonNull::new_unchecked(msg_send![cls, new])) }
31+
unsafe { Id::new(msg_send![cls, new]).unwrap() }
3332
}
3433

3534
fn number(&self) -> u32 {

objc2-foundation/src/array.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use core::cmp::Ordering;
33
use core::ffi::c_void;
44
use core::marker::PhantomData;
55
use core::ops::{Index, IndexMut, Range};
6-
use core::ptr::NonNull;
76

87
use objc2::msg_send;
98
use objc2::rc::{DefaultId, Id, Owned, Ownership, Shared, SliceId};
@@ -47,16 +46,15 @@ object! {
4746
unsafe pub struct NSMutableArray<T, O: Ownership>: NSArray<T, O> {}
4847
}
4948

50-
unsafe fn from_refs<T: Message + ?Sized>(cls: &Class, refs: &[&T]) -> NonNull<Object> {
49+
unsafe fn from_refs<T: Message + ?Sized>(cls: &Class, refs: &[&T]) -> *mut Object {
5150
let obj: *mut Object = unsafe { msg_send![cls, alloc] };
52-
let obj: *mut Object = unsafe {
51+
unsafe {
5352
msg_send![
5453
obj,
5554
initWithObjects: refs.as_ptr(),
5655
count: refs.len(),
5756
]
58-
};
59-
unsafe { NonNull::new_unchecked(obj) }
57+
}
6058
}
6159

6260
impl<T: Message, O: Ownership> NSArray<T, O> {
@@ -106,7 +104,7 @@ impl<T: Message, O: Ownership> NSArray<T, O> {
106104
}
107105

108106
pub fn from_vec(vec: Vec<Id<T, O>>) -> Id<Self, O> {
109-
unsafe { Id::new(from_refs(Self::class(), vec.as_slice_ref()).cast()) }
107+
unsafe { Id::new(from_refs(Self::class(), vec.as_slice_ref()).cast()).unwrap() }
110108
}
111109

112110
pub fn objects_in_range(&self, range: Range<usize>) -> Vec<&T> {
@@ -128,27 +126,27 @@ impl<T: Message, O: Ownership> NSArray<T, O> {
128126
array
129127
.to_vec()
130128
.into_iter()
131-
.map(|obj| unsafe { Id::retain(obj.into()) })
129+
.map(|obj| unsafe { Id::retain(obj as *const T as *mut T).unwrap_unchecked() })
132130
.collect()
133131
}
134132
}
135133

136134
impl<T: Message> NSArray<T, Shared> {
137135
pub fn from_slice(slice: &[Id<T, Shared>]) -> Id<Self, Shared> {
138-
unsafe { Id::new(from_refs(Self::class(), slice.as_slice_ref()).cast()) }
136+
unsafe { Id::new(from_refs(Self::class(), slice.as_slice_ref()).cast()).unwrap() }
139137
}
140138

141139
#[doc(alias = "objectAtIndex:")]
142140
pub fn get_retained(&self, index: usize) -> Id<T, Shared> {
143141
let obj = self.get(index).unwrap();
144142
// SAFETY: The object is originally shared (see `where` bound).
145-
unsafe { Id::retain(obj.into()) }
143+
unsafe { Id::retain(obj as *const T as *mut T).unwrap_unchecked() }
146144
}
147145

148146
pub fn to_shared_vec(&self) -> Vec<Id<T, Shared>> {
149147
self.to_vec()
150148
.into_iter()
151-
.map(|obj| unsafe { Id::retain(obj.into()) })
149+
.map(|obj| unsafe { Id::retain(obj as *const T as *mut T).unwrap_unchecked() })
152150
.collect()
153151
}
154152
}
@@ -212,13 +210,13 @@ impl<T: Message, O: Ownership> NSMutableArray<T, O> {
212210
unsafe_def_fn!(pub fn new -> Owned);
213211

214212
pub fn from_vec(vec: Vec<Id<T, O>>) -> Id<Self, Owned> {
215-
unsafe { Id::new(from_refs(Self::class(), vec.as_slice_ref()).cast()) }
213+
unsafe { Id::new(from_refs(Self::class(), vec.as_slice_ref()).cast()).unwrap() }
216214
}
217215
}
218216

219217
impl<T: Message> NSMutableArray<T, Shared> {
220218
pub fn from_slice(slice: &[Id<T, Shared>]) -> Id<Self, Owned> {
221-
unsafe { Id::new(from_refs(Self::class(), slice.as_slice_ref()).cast()) }
219+
unsafe { Id::new(from_refs(Self::class(), slice.as_slice_ref()).cast()).unwrap() }
222220
}
223221
}
224222

@@ -249,7 +247,7 @@ impl<T: Message, O: Ownership> NSMutableArray<T, O> {
249247
pub fn replace(&mut self, index: usize, obj: Id<T, O>) -> Id<T, O> {
250248
let old_obj = unsafe {
251249
let obj = self.get(index).unwrap();
252-
Id::retain(obj.into())
250+
Id::retain(obj as *const T as *mut T).unwrap_unchecked()
253251
};
254252
unsafe {
255253
let _: () = msg_send![
@@ -264,7 +262,7 @@ impl<T: Message, O: Ownership> NSMutableArray<T, O> {
264262
#[doc(alias = "removeObjectAtIndex:")]
265263
pub fn remove(&mut self, index: usize) -> Id<T, O> {
266264
let obj = if let Some(obj) = self.get(index) {
267-
unsafe { Id::retain(obj.into()) }
265+
unsafe { Id::retain(obj as *const T as *mut T).unwrap_unchecked() }
268266
} else {
269267
panic!("removal index should be < len");
270268
};
@@ -277,7 +275,7 @@ impl<T: Message, O: Ownership> NSMutableArray<T, O> {
277275
#[doc(alias = "removeLastObject")]
278276
pub fn pop(&mut self) -> Option<Id<T, O>> {
279277
self.last().map(|obj| {
280-
let obj = unsafe { Id::retain(obj.into()) };
278+
let obj = unsafe { Id::retain(obj as *const T as *mut T).unwrap_unchecked() };
281279
unsafe {
282280
let _: () = msg_send![self, removeLastObject];
283281
}

objc2-foundation/src/attributed_string.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use core::ptr::NonNull;
2-
31
use objc2::msg_send;
42
use objc2::rc::{DefaultId, Id, Shared};
53
use objc2::runtime::Object;
@@ -51,7 +49,7 @@ impl NSAttributedString {
5149
unsafe {
5250
let obj: *mut Self = msg_send![Self::class(), alloc];
5351
let obj: *mut Self = msg_send![obj, initWithString: string, attributes: attributes];
54-
Id::new(NonNull::new_unchecked(obj))
52+
Id::new(obj).unwrap()
5553
}
5654
}
5755

@@ -61,7 +59,7 @@ impl NSAttributedString {
6159
unsafe {
6260
let obj: *mut Self = msg_send![Self::class(), alloc];
6361
let obj: *mut Self = msg_send![obj, initWithString: string];
64-
Id::new(NonNull::new_unchecked(obj))
62+
Id::new(obj).unwrap()
6563
}
6664
}
6765
}

objc2-foundation/src/copying.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use core::ptr::NonNull;
2-
31
use objc2::rc::{Id, Owned, Ownership};
42
use objc2::{msg_send, Message};
53

@@ -35,7 +33,7 @@ pub unsafe trait NSCopying: Message {
3533
fn copy(&self) -> Id<Self::Output, Self::Ownership> {
3634
unsafe {
3735
let obj: *mut Self::Output = msg_send![self, copy];
38-
Id::new(NonNull::new_unchecked(obj))
36+
Id::new(obj).unwrap()
3937
}
4038
}
4139
}
@@ -50,7 +48,7 @@ pub unsafe trait NSMutableCopying: Message {
5048
fn mutable_copy(&self) -> Id<Self::Output, Owned> {
5149
unsafe {
5250
let obj: *mut Self::Output = msg_send![self, mutableCopy];
53-
Id::new(NonNull::new_unchecked(obj))
51+
Id::new(obj).unwrap()
5452
}
5553
}
5654
}

objc2-foundation/src/data.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#[cfg(feature = "block")]
22
use alloc::vec::Vec;
3+
use core::ffi::c_void;
34
use core::ops::{Index, IndexMut, Range};
45
use core::slice::{self, SliceIndex};
5-
use core::{ffi::c_void, ptr::NonNull};
66
use std::io;
77

88
use objc2::msg_send;
@@ -62,7 +62,7 @@ impl NSData {
6262
}
6363

6464
pub fn with_bytes(bytes: &[u8]) -> Id<Self, Shared> {
65-
unsafe { Id::new(data_with_bytes(Self::class(), bytes).cast()) }
65+
unsafe { Id::new(data_with_bytes(Self::class(), bytes).cast()).unwrap() }
6666
}
6767

6868
#[cfg(feature = "block")]
@@ -79,7 +79,7 @@ impl NSData {
7979
#[cfg(not(gnustep))]
8080
let cls = Self::class();
8181

82-
unsafe { Id::new(data_from_vec(cls, bytes).cast()) }
82+
unsafe { Id::new(data_from_vec(cls, bytes).cast()).unwrap() }
8383
}
8484
}
8585

@@ -122,12 +122,12 @@ impl NSMutableData {
122122
unsafe_def_fn!(fn new -> Owned);
123123

124124
pub fn with_bytes(bytes: &[u8]) -> Id<Self, Owned> {
125-
unsafe { Id::new(data_with_bytes(Self::class(), bytes).cast()) }
125+
unsafe { Id::new(data_with_bytes(Self::class(), bytes).cast()).unwrap() }
126126
}
127127

128128
#[cfg(feature = "block")]
129129
pub fn from_vec(bytes: Vec<u8>) -> Id<Self, Owned> {
130-
unsafe { Id::new(data_from_vec(Self::class(), bytes).cast()) }
130+
unsafe { Id::new(data_from_vec(Self::class(), bytes).cast()).unwrap() }
131131
}
132132

133133
// TODO: Use malloc_buf/mbox and `initWithBytesNoCopy:...`?
@@ -138,7 +138,7 @@ impl NSMutableData {
138138
unsafe {
139139
let obj: *mut Self = msg_send![Self::class(), alloc];
140140
let obj: *mut Self = msg_send![obj, initWithData: data];
141-
Id::new(NonNull::new_unchecked(obj))
141+
Id::new(obj).unwrap()
142142
}
143143
}
144144

@@ -147,7 +147,7 @@ impl NSMutableData {
147147
unsafe {
148148
let obj: *mut Self = msg_send![Self::class(), alloc];
149149
let obj: *mut Self = msg_send![obj, initWithCapacity: capacity];
150-
Id::new(NonNull::new_unchecked(obj))
150+
Id::new(obj).unwrap()
151151
}
152152
}
153153
}
@@ -289,21 +289,20 @@ impl DefaultId for NSMutableData {
289289
}
290290
}
291291

292-
unsafe fn data_with_bytes(cls: &Class, bytes: &[u8]) -> NonNull<Object> {
292+
unsafe fn data_with_bytes(cls: &Class, bytes: &[u8]) -> *mut Object {
293293
let bytes_ptr = bytes.as_ptr() as *const c_void;
294294
unsafe {
295295
let obj: *mut Object = msg_send![cls, alloc];
296-
let obj: *mut Object = msg_send![
296+
msg_send![
297297
obj,
298298
initWithBytes: bytes_ptr,
299299
length: bytes.len(),
300-
];
301-
NonNull::new_unchecked(obj)
300+
]
302301
}
303302
}
304303

305304
#[cfg(feature = "block")]
306-
unsafe fn data_from_vec(cls: &Class, bytes: Vec<u8>) -> NonNull<Object> {
305+
unsafe fn data_from_vec(cls: &Class, bytes: Vec<u8>) -> *mut Object {
307306
use core::mem::ManuallyDrop;
308307

309308
use block2::{Block, ConcreteBlock};
@@ -321,13 +320,12 @@ unsafe fn data_from_vec(cls: &Class, bytes: Vec<u8>) -> NonNull<Object> {
321320

322321
unsafe {
323322
let obj: *mut Object = msg_send![cls, alloc];
324-
let obj: *mut Object = msg_send![
323+
msg_send![
325324
obj,
326325
initWithBytesNoCopy: bytes.as_mut_ptr() as *mut c_void,
327326
length: bytes.len(),
328327
deallocator: dealloc,
329-
];
330-
NonNull::new_unchecked(obj)
328+
]
331329
}
332330
}
333331

objc2-foundation/src/dictionary.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use alloc::vec::Vec;
22
use core::cmp::min;
33
use core::marker::PhantomData;
44
use core::ops::Index;
5-
use core::ptr::{self, NonNull};
5+
use core::ptr;
66

77
use objc2::rc::{DefaultId, Id, Owned, Shared, SliceId};
88
use objc2::{msg_send, Message};
@@ -103,7 +103,7 @@ impl<K: Message, V: Message> NSDictionary<K, V> {
103103
pub fn keys_array(&self) -> Id<NSArray<K, Shared>, Shared> {
104104
unsafe {
105105
let keys = msg_send![self, allKeys];
106-
Id::retain(NonNull::new_unchecked(keys))
106+
Id::retain(keys).unwrap()
107107
}
108108
}
109109

@@ -124,14 +124,13 @@ impl<K: Message, V: Message> NSDictionary<K, V> {
124124
count: count,
125125
]
126126
};
127-
let obj = unsafe { NonNull::new_unchecked(obj) };
128-
unsafe { Id::new(obj) }
127+
unsafe { Id::new(obj).unwrap() }
129128
}
130129

131130
pub fn into_values_array(dict: Id<Self, Owned>) -> Id<NSArray<V, Owned>, Shared> {
132131
unsafe {
133132
let vals = msg_send![dict, allValues];
134-
Id::retain(NonNull::new_unchecked(vals))
133+
Id::retain(vals).unwrap()
135134
}
136135
}
137136
}

objc2-foundation/src/enumerator.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use core::marker::PhantomData;
22
use core::mem;
33
use core::ptr;
4-
use core::ptr::NonNull;
54
use core::slice;
65
use std::os::raw::c_ulong;
76

@@ -22,9 +21,8 @@ impl<'a, T: Message> NSEnumerator<'a, T> {
2221
/// The object pointer must be a valid `NSEnumerator` with `Owned`
2322
/// ownership.
2423
pub unsafe fn from_ptr(ptr: *mut Object) -> Self {
25-
let ptr = NonNull::new(ptr).unwrap();
2624
Self {
27-
id: unsafe { Id::retain(ptr) },
25+
id: unsafe { Id::retain(ptr) }.unwrap(),
2826
item: PhantomData,
2927
}
3028
}

objc2-foundation/src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ macro_rules! unsafe_def_fn {
156156
$(#[$m])*
157157
$v fn new() -> Id<Self, $o> {
158158
let cls = Self::class();
159-
unsafe { Id::new(NonNull::new_unchecked(msg_send![cls, new])) }
159+
unsafe { Id::new(msg_send![cls, new]).unwrap() }
160160
}
161161
};
162162
}

objc2-foundation/src/mutable_attributed_string.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use core::ptr::NonNull;
2-
31
use objc2::msg_send;
42
use objc2::rc::{DefaultId, Id, Owned, Shared};
53

@@ -30,7 +28,7 @@ impl NSMutableAttributedString {
3028
unsafe {
3129
let obj: *mut Self = msg_send![Self::class(), alloc];
3230
let obj: *mut Self = msg_send![obj, initWithString: string];
33-
Id::new(NonNull::new_unchecked(obj))
31+
Id::new(obj).unwrap()
3432
}
3533
}
3634

@@ -39,7 +37,7 @@ impl NSMutableAttributedString {
3937
unsafe {
4038
let obj: *mut Self = msg_send![Self::class(), alloc];
4139
let obj: *mut Self = msg_send![obj, initWithAttributedString: attributed_string];
42-
Id::new(NonNull::new_unchecked(obj))
40+
Id::new(obj).unwrap()
4341
}
4442
}
4543
}

0 commit comments

Comments
 (0)