Skip to content

Commit

Permalink
Add Id::as_mut_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jun 6, 2022
1 parent 1158e4c commit d9a328e
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 21 deletions.
5 changes: 4 additions & 1 deletion objc2-foundation/src/attributed_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ mod tests {
assert!(s2.is_kind_of(NSAttributedString::class()));

let s3 = s1.mutable_copy();
assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s3) as *mut NSAttributedString);
assert_ne!(
Id::as_ptr(&s1),
Id::as_ptr(&s3) as *const NSAttributedString
);
assert!(s3.is_kind_of(NSMutableAttributedString::class()));
}
}
5 changes: 4 additions & 1 deletion objc2-foundation/src/mutable_attributed_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ mod tests {
fn test_copy() {
let s1 = NSMutableAttributedString::from_nsstring(&NSString::from_str("abc"));
let s2 = s1.copy();
assert_ne!(Id::as_ptr(&s1) as *mut NSAttributedString, Id::as_ptr(&s2));
assert_ne!(
Id::as_ptr(&s1) as *const NSAttributedString,
Id::as_ptr(&s2)
);
assert!(s2.is_kind_of(NSAttributedString::class()));

let s3 = s1.mutable_copy();
Expand Down
2 changes: 1 addition & 1 deletion objc2-foundation/src/mutable_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ mod tests {
fn test_copy() {
let s1 = NSMutableString::from_str("abc");
let s2 = s1.copy();
assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s2) as *mut NSMutableString);
assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s2) as *const NSMutableString);
assert!(s2.is_kind_of(NSString::class()));

let s3 = s1.mutable_copy();
Expand Down
2 changes: 1 addition & 1 deletion objc2-foundation/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ mod tests {
assert!(s2.is_kind_of(NSString::class()));

let s3 = s1.mutable_copy();
assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s3) as *mut NSString);
assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s3) as *const NSString);
assert!(s3.is_kind_of(NSMutableString::class()));
}

Expand Down
2 changes: 1 addition & 1 deletion objc2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
upgrading easier.
* Allow using `From`/`TryFrom` to convert between `rc::Id` and `rc::WeakId`.
* Added `Bool::as_bool` (more descriptive name than `Bool::is_true`).
* Added convenience method `Id::as_ptr`.
* Added convenience method `Id::as_ptr` and `Id::as_mut_ptr`.
* The `objc2-encode` dependency is now exposed as `objc2::encode`.
* Added `Id::retain_autoreleased` to allow following Cocoas memory management
rules more efficiently.
Expand Down
16 changes: 8 additions & 8 deletions objc2/benches/autorelease.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ fn new_nsdata() -> Id<Object, Shared> {
unsafe { Id::new(obj).unwrap_unchecked() }
}

fn new_leaked_nsdata() -> *mut Object {
fn new_leaked_nsdata() -> *const Object {
Id::as_ptr(&*ManuallyDrop::new(new_nsdata()))
}

fn autoreleased_nsdata() -> *mut Object {
fn autoreleased_nsdata() -> *const Object {
// let bytes_ptr = BYTES.as_ptr() as *const c_void;
// unsafe {
// msg_send![
Expand All @@ -83,20 +83,20 @@ fn new_nsstring() -> Id<Object, Shared> {
unsafe { Id::new(obj).unwrap_unchecked() }
}

fn new_leaked_nsstring() -> *mut Object {
fn new_leaked_nsstring() -> *const Object {
Id::as_ptr(&*ManuallyDrop::new(new_nsstring()))
}

fn autoreleased_nsstring() -> *mut Object {
fn autoreleased_nsstring() -> *const Object {
// unsafe { msg_send![class!(NSString), string] }
unsafe { msg_send![new_leaked_nsstring(), autorelease] }
}

fn retain_autoreleased(obj: *mut Object) -> Id<Object, Shared> {
unsafe { Id::retain_autoreleased(obj.cast()).unwrap_unchecked() }
fn retain_autoreleased(obj: *const Object) -> Id<Object, Shared> {
unsafe { Id::retain_autoreleased((obj as *mut Object).cast()).unwrap_unchecked() }
}

fn autoreleased_nsdata_pool_cleanup() -> *mut Object {
fn autoreleased_nsdata_pool_cleanup() -> *const Object {
autoreleasepool(|_| autoreleased_nsdata())
}

Expand All @@ -108,7 +108,7 @@ fn autoreleased_nsdata_fast_caller_cleanup_pool_cleanup() -> Id<Object, Shared>
autoreleasepool(|_| retain_autoreleased(autoreleased_nsdata()))
}

fn autoreleased_nsstring_pool_cleanup() -> *mut Object {
fn autoreleased_nsstring_pool_cleanup() -> *const Object {
autoreleasepool(|_| autoreleased_nsstring())
}

Expand Down
4 changes: 2 additions & 2 deletions objc2/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ unsafe impl<'a, T: Message + ?Sized, O: Ownership> MessageReceiver for &'a Id<T,
unsafe impl<'a, T: Message + ?Sized> MessageReceiver for &'a mut Id<T, Owned> {
#[inline]
fn as_raw_receiver(self) -> *mut Object {
Id::as_ptr(self) as *mut Object
Id::as_mut_ptr(self) as *mut Object
}
}

Expand All @@ -277,7 +277,7 @@ unsafe impl<'a, T: Message + ?Sized, O: Ownership> MessageReceiver for &'a Manua
unsafe impl<'a, T: Message + ?Sized> MessageReceiver for &'a mut ManuallyDrop<Id<T, Owned>> {
#[inline]
fn as_raw_receiver(self) -> *mut Object {
Id::as_ptr(&mut **self) as *mut Object
Id::as_mut_ptr(&mut **self) as *mut Object
}
}

Expand Down
19 changes: 18 additions & 1 deletion objc2/src/rc/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,26 @@ impl<T: Message + ?Sized, O: Ownership> Id<T, O> {
///
/// The pointer is valid for at least as long as the `Id` is held.
///
/// See [`Id::as_mut_ptr`] for the mutable equivalent.
///
/// This is an associated method, and must be called as `Id::as_ptr(obj)`.
#[inline]
pub fn as_ptr(this: &Id<T, O>) -> *mut T {
pub fn as_ptr(this: &Id<T, O>) -> *const T {
this.ptr.as_ptr()
}
}

impl<T: Message + ?Sized> Id<T, Owned> {
/// Returns a raw mutable pointer to the object.
///
/// The pointer is valid for at least as long as the `Id` is held.
///
/// See [`Id::as_ptr`] for the immutable equivalent.
///
/// This is an associated method, and must be called as
/// `Id::as_mut_ptr(obj)`.
#[inline]
pub fn as_mut_ptr(this: &mut Id<T, Owned>) -> *mut T {
this.ptr.as_ptr()
}
}
Expand Down
6 changes: 3 additions & 3 deletions objc2/src/rc/weak_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ impl<T: Message> WeakId<T> {
/// # Safety
///
/// The object must be valid or null.
unsafe fn new_inner(obj: *mut T) -> Self {
unsafe fn new_inner(obj: *const T) -> Self {
let inner = Box::new(UnsafeCell::new(ptr::null_mut()));
// SAFETY: `ptr` will never move, and the caller verifies `obj`
let _ = unsafe { ffi::objc_initWeak(inner.get(), obj as *mut ffi::objc_object) };
let _ = unsafe { ffi::objc_initWeak(inner.get(), obj as *mut T as *mut ffi::objc_object) };
Self {
inner,
item: PhantomData,
Expand Down Expand Up @@ -108,7 +108,7 @@ impl<T: Message> Default for WeakId<T> {
#[inline]
fn default() -> Self {
// SAFETY: The pointer is null
unsafe { Self::new_inner(ptr::null_mut()) }
unsafe { Self::new_inner(ptr::null()) }
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/src/test_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ fn test_object() {
assert!(obj.var3().is_null());
assert!(obj.var3_ivar().is_null());

let obj2 = Id::as_ptr(&mut *ManuallyDrop::new(NSObject::new())).cast::<Object>();
let obj2 = Id::as_mut_ptr(&mut *ManuallyDrop::new(NSObject::new())).cast::<Object>();
obj.set_var3(obj2);
assert_eq!(obj.var3(), obj2);
assert_eq!(*obj.var3_ivar(), obj2);

let obj3 = Id::as_ptr(&mut *ManuallyDrop::new(NSObject::new())).cast::<Object>();
let obj3 = Id::as_mut_ptr(&mut *ManuallyDrop::new(NSObject::new())).cast::<Object>();
*obj.var3_ivar_mut() = obj3;
assert_ne!(obj.var3(), obj2);
assert_ne!(*obj.var3_ivar(), obj2);
Expand Down

0 comments on commit d9a328e

Please sign in to comment.