Skip to content

Commit

Permalink
Change null handling when creating Ids
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Mar 2, 2022
1 parent 4856ef7 commit 5a52c83
Show file tree
Hide file tree
Showing 27 changed files with 122 additions and 166 deletions.
3 changes: 1 addition & 2 deletions objc2-foundation/examples/class_with_lifetime.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::marker::PhantomData;
use std::ptr::NonNull;
use std::sync::Once;

use objc2::declare::ClassDecl;
Expand Down Expand Up @@ -30,7 +29,7 @@ impl<'a> MyObject<'a> {
unsafe {
let obj: *mut Self = msg_send![Self::class(), alloc];
let obj: *mut Self = msg_send![obj, initWithPtr: number_ptr];
Id::new(NonNull::new_unchecked(obj))
Id::new(obj).unwrap()
}
}

Expand Down
3 changes: 1 addition & 2 deletions objc2-foundation/examples/custom_class.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::ptr::NonNull;
use std::sync::Once;

use objc2::declare::ClassDecl;
Expand Down Expand Up @@ -26,7 +25,7 @@ static MYOBJECT_REGISTER_CLASS: Once = Once::new();
impl MYObject {
fn new() -> Id<Self, Owned> {
let cls = Self::class();
unsafe { Id::new(NonNull::new_unchecked(msg_send![cls, new])) }
unsafe { Id::new(msg_send![cls, new]).unwrap() }
}

fn number(&self) -> u32 {
Expand Down
28 changes: 13 additions & 15 deletions objc2-foundation/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use core::cmp::Ordering;
use core::ffi::c_void;
use core::marker::PhantomData;
use core::ops::{Index, IndexMut, Range};
use core::ptr::NonNull;

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

unsafe fn from_refs<T: Message + ?Sized>(cls: &Class, refs: &[&T]) -> NonNull<Object> {
unsafe fn from_refs<T: Message + ?Sized>(cls: &Class, refs: &[&T]) -> *mut Object {
let obj: *mut Object = unsafe { msg_send![cls, alloc] };
let obj: *mut Object = unsafe {
unsafe {
msg_send![
obj,
initWithObjects: refs.as_ptr(),
count: refs.len(),
]
};
unsafe { NonNull::new_unchecked(obj) }
}
}

impl<T: Message, O: Ownership> NSArray<T, O> {
Expand Down Expand Up @@ -106,7 +104,7 @@ impl<T: Message, O: Ownership> NSArray<T, O> {
}

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

pub fn objects_in_range(&self, range: Range<usize>) -> Vec<&T> {
Expand All @@ -128,27 +126,27 @@ impl<T: Message, O: Ownership> NSArray<T, O> {
array
.to_vec()
.into_iter()
.map(|obj| unsafe { Id::retain(obj.into()) })
.map(|obj| unsafe { Id::retain(obj as *const T as *mut T).unwrap_unchecked() })
.collect()
}
}

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

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

pub fn to_shared_vec(&self) -> Vec<Id<T, Shared>> {
self.to_vec()
.into_iter()
.map(|obj| unsafe { Id::retain(obj.into()) })
.map(|obj| unsafe { Id::retain(obj as *const T as *mut T).unwrap_unchecked() })
.collect()
}
}
Expand Down Expand Up @@ -212,13 +210,13 @@ impl<T: Message, O: Ownership> NSMutableArray<T, O> {
unsafe_def_fn!(pub fn new -> Owned);

pub fn from_vec(vec: Vec<Id<T, O>>) -> Id<Self, Owned> {
unsafe { Id::new(from_refs(Self::class(), vec.as_slice_ref()).cast()) }
unsafe { Id::new(from_refs(Self::class(), vec.as_slice_ref()).cast()).unwrap() }
}
}

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

Expand Down Expand Up @@ -249,7 +247,7 @@ impl<T: Message, O: Ownership> NSMutableArray<T, O> {
pub fn replace(&mut self, index: usize, obj: Id<T, O>) -> Id<T, O> {
let old_obj = unsafe {
let obj = self.get(index).unwrap();
Id::retain(obj.into())
Id::retain(obj as *const T as *mut T).unwrap_unchecked()
};
unsafe {
let _: () = msg_send![
Expand All @@ -264,7 +262,7 @@ impl<T: Message, O: Ownership> NSMutableArray<T, O> {
#[doc(alias = "removeObjectAtIndex:")]
pub fn remove(&mut self, index: usize) -> Id<T, O> {
let obj = if let Some(obj) = self.get(index) {
unsafe { Id::retain(obj.into()) }
unsafe { Id::retain(obj as *const T as *mut T).unwrap_unchecked() }
} else {
panic!("removal index should be < len");
};
Expand All @@ -277,7 +275,7 @@ impl<T: Message, O: Ownership> NSMutableArray<T, O> {
#[doc(alias = "removeLastObject")]
pub fn pop(&mut self) -> Option<Id<T, O>> {
self.last().map(|obj| {
let obj = unsafe { Id::retain(obj.into()) };
let obj = unsafe { Id::retain(obj as *const T as *mut T).unwrap_unchecked() };
unsafe {
let _: () = msg_send![self, removeLastObject];
}
Expand Down
6 changes: 2 additions & 4 deletions objc2-foundation/src/attributed_string.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::ptr::NonNull;

use objc2::msg_send;
use objc2::rc::{DefaultId, Id, Shared};
use objc2::runtime::Object;
Expand Down Expand Up @@ -51,7 +49,7 @@ impl NSAttributedString {
unsafe {
let obj: *mut Self = msg_send![Self::class(), alloc];
let obj: *mut Self = msg_send![obj, initWithString: string, attributes: attributes];
Id::new(NonNull::new_unchecked(obj))
Id::new(obj).unwrap()
}
}

Expand All @@ -61,7 +59,7 @@ impl NSAttributedString {
unsafe {
let obj: *mut Self = msg_send![Self::class(), alloc];
let obj: *mut Self = msg_send![obj, initWithString: string];
Id::new(NonNull::new_unchecked(obj))
Id::new(obj).unwrap()
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions objc2-foundation/src/copying.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::ptr::NonNull;

use objc2::rc::{Id, Owned, Ownership};
use objc2::{msg_send, Message};

Expand Down Expand Up @@ -35,7 +33,7 @@ pub unsafe trait NSCopying: Message {
fn copy(&self) -> Id<Self::Output, Self::Ownership> {
unsafe {
let obj: *mut Self::Output = msg_send![self, copy];
Id::new(NonNull::new_unchecked(obj))
Id::new(obj).unwrap()
}
}
}
Expand All @@ -50,7 +48,7 @@ pub unsafe trait NSMutableCopying: Message {
fn mutable_copy(&self) -> Id<Self::Output, Owned> {
unsafe {
let obj: *mut Self::Output = msg_send![self, mutableCopy];
Id::new(NonNull::new_unchecked(obj))
Id::new(obj).unwrap()
}
}
}
28 changes: 13 additions & 15 deletions objc2-foundation/src/data.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[cfg(feature = "block")]
use alloc::vec::Vec;
use core::ffi::c_void;
use core::ops::{Index, IndexMut, Range};
use core::slice::{self, SliceIndex};
use core::{ffi::c_void, ptr::NonNull};
use std::io;

use objc2::msg_send;
Expand Down Expand Up @@ -62,7 +62,7 @@ impl NSData {
}

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

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

unsafe { Id::new(data_from_vec(cls, bytes).cast()) }
unsafe { Id::new(data_from_vec(cls, bytes).cast()).unwrap() }
}
}

Expand Down Expand Up @@ -122,12 +122,12 @@ impl NSMutableData {
unsafe_def_fn!(fn new -> Owned);

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

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

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

Expand All @@ -147,7 +147,7 @@ impl NSMutableData {
unsafe {
let obj: *mut Self = msg_send![Self::class(), alloc];
let obj: *mut Self = msg_send![obj, initWithCapacity: capacity];
Id::new(NonNull::new_unchecked(obj))
Id::new(obj).unwrap()
}
}
}
Expand Down Expand Up @@ -289,21 +289,20 @@ impl DefaultId for NSMutableData {
}
}

unsafe fn data_with_bytes(cls: &Class, bytes: &[u8]) -> NonNull<Object> {
unsafe fn data_with_bytes(cls: &Class, bytes: &[u8]) -> *mut Object {
let bytes_ptr = bytes.as_ptr() as *const c_void;
unsafe {
let obj: *mut Object = msg_send![cls, alloc];
let obj: *mut Object = msg_send![
msg_send![
obj,
initWithBytes: bytes_ptr,
length: bytes.len(),
];
NonNull::new_unchecked(obj)
]
}
}

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

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

unsafe {
let obj: *mut Object = msg_send![cls, alloc];
let obj: *mut Object = msg_send![
msg_send![
obj,
initWithBytesNoCopy: bytes.as_mut_ptr() as *mut c_void,
length: bytes.len(),
deallocator: dealloc,
];
NonNull::new_unchecked(obj)
]
}
}

Expand Down
9 changes: 4 additions & 5 deletions objc2-foundation/src/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloc::vec::Vec;
use core::cmp::min;
use core::marker::PhantomData;
use core::ops::Index;
use core::ptr::{self, NonNull};
use core::ptr;

use objc2::rc::{DefaultId, Id, Owned, Shared, SliceId};
use objc2::{msg_send, Message};
Expand Down Expand Up @@ -103,7 +103,7 @@ impl<K: Message, V: Message> NSDictionary<K, V> {
pub fn keys_array(&self) -> Id<NSArray<K, Shared>, Shared> {
unsafe {
let keys = msg_send![self, allKeys];
Id::retain(NonNull::new_unchecked(keys))
Id::retain(keys).unwrap()
}
}

Expand All @@ -124,14 +124,13 @@ impl<K: Message, V: Message> NSDictionary<K, V> {
count: count,
]
};
let obj = unsafe { NonNull::new_unchecked(obj) };
unsafe { Id::new(obj) }
unsafe { Id::new(obj).unwrap() }
}

pub fn into_values_array(dict: Id<Self, Owned>) -> Id<NSArray<V, Owned>, Shared> {
unsafe {
let vals = msg_send![dict, allValues];
Id::retain(NonNull::new_unchecked(vals))
Id::retain(vals).unwrap()
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions objc2-foundation/src/enumerator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use core::marker::PhantomData;
use core::mem;
use core::ptr;
use core::ptr::NonNull;
use core::slice;
use std::os::raw::c_ulong;

Expand All @@ -23,9 +22,8 @@ impl<'a, T: Message> NSEnumerator<'a, T> {
/// The object pointer must be a valid `NSEnumerator` with `Owned`
/// ownership.
pub unsafe fn from_ptr(ptr: *mut Object) -> Self {
let ptr = NonNull::new(ptr).unwrap();
Self {
id: unsafe { Id::retain(ptr) },
id: unsafe { Id::retain(ptr) }.unwrap(),
item: PhantomData,
}
}
Expand Down
2 changes: 1 addition & 1 deletion objc2-foundation/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ macro_rules! unsafe_def_fn {
$(#[$m])*
$v fn new() -> Id<Self, $o> {
let cls = Self::class();
unsafe { Id::new(NonNull::new_unchecked(msg_send![cls, new])) }
unsafe { Id::new(msg_send![cls, new]).unwrap() }
}
};
}
6 changes: 2 additions & 4 deletions objc2-foundation/src/mutable_attributed_string.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::ptr::NonNull;

use objc2::msg_send;
use objc2::rc::{DefaultId, Id, Owned, Shared};

Expand Down Expand Up @@ -30,7 +28,7 @@ impl NSMutableAttributedString {
unsafe {
let obj: *mut Self = msg_send![Self::class(), alloc];
let obj: *mut Self = msg_send![obj, initWithString: string];
Id::new(NonNull::new_unchecked(obj))
Id::new(obj).unwrap()
}
}

Expand All @@ -39,7 +37,7 @@ impl NSMutableAttributedString {
unsafe {
let obj: *mut Self = msg_send![Self::class(), alloc];
let obj: *mut Self = msg_send![obj, initWithAttributedString: attributed_string];
Id::new(NonNull::new_unchecked(obj))
Id::new(obj).unwrap()
}
}
}
Expand Down
Loading

0 comments on commit 5a52c83

Please sign in to comment.