From 1b8a9ee04ae8ecfee46a5b532e2a71762738fe24 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 7 Jan 2024 13:12:32 +0100 Subject: [PATCH] Add `Id::into_raw` --- crates/objc2/CHANGELOG.md | 1 + .../src/__macro_helpers/declare_class.rs | 2 +- crates/objc2/src/__macro_helpers/msg_send.rs | 2 +- crates/objc2/src/rc/id.rs | 34 +++++++++++++++---- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/crates/objc2/CHANGELOG.md b/crates/objc2/CHANGELOG.md index dd35b43b5..71abca3b9 100644 --- a/crates/objc2/CHANGELOG.md +++ b/crates/objc2/CHANGELOG.md @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - `AnyClass::instance_variables`. - `AnyProtocol::protocols`. - `AnyProtocol::adopted_protocols`. +* Added `Id::into_raw` as the oppositve of `Id::from_raw`. ### Changed * Moved `ClassBuilder` and `ProtocolBuilder` from the `declare` module to the diff --git a/crates/objc2/src/__macro_helpers/declare_class.rs b/crates/objc2/src/__macro_helpers/declare_class.rs index da9ae3392..6b25a9d96 100644 --- a/crates/objc2/src/__macro_helpers/declare_class.rs +++ b/crates/objc2/src/__macro_helpers/declare_class.rs @@ -108,7 +108,7 @@ pub trait MaybeOptionId: MaybeUnwrap { impl MaybeOptionId for Id { #[inline] fn consumed_return(self) -> IdReturnValue { - let ptr: *mut T = Id::consume_as_ptr(self); + let ptr: *mut T = Id::into_raw(self); IdReturnValue(ptr.cast()) } diff --git a/crates/objc2/src/__macro_helpers/msg_send.rs b/crates/objc2/src/__macro_helpers/msg_send.rs index 953be81c1..54b87af67 100644 --- a/crates/objc2/src/__macro_helpers/msg_send.rs +++ b/crates/objc2/src/__macro_helpers/msg_send.rs @@ -181,7 +181,7 @@ impl MsgSend for ManuallyDrop> { #[inline] fn into_raw_receiver(self) -> *mut AnyObject { - Id::consume_as_ptr(ManuallyDrop::into_inner(self)).cast() + Id::into_raw(ManuallyDrop::into_inner(self)).cast() } } diff --git a/crates/objc2/src/rc/id.rs b/crates/objc2/src/rc/id.rs index 15236b6a2..088976436 100644 --- a/crates/objc2/src/rc/id.rs +++ b/crates/objc2/src/rc/id.rs @@ -227,6 +227,33 @@ impl Id { unsafe { Self::from_raw(ptr) } } + /// Consumes the `Id`, returning a raw pointer with +1 retain count. + /// + /// After calling this function, the caller is responsible for the memory + /// previously managed by the `Id`. + /// + /// This is effectively the opposite of [`Id::from_raw`], see that for + /// more details on when this function is useful. + /// + /// + /// # Examples + /// + /// Converting an `Id` to a pointer and back. + /// + /// ``` + /// use objc2::rc::Id; + /// use objc2::runtime::NSObject; + /// + /// let obj = NSObject::new(); + /// let ptr = Id::into_raw(obj); + /// // SAFETY: The pointer is valid, and has +1 retain count from above. + /// let obj = unsafe { Id::from_raw(ptr) }.unwrap(); + /// ``` + #[inline] + pub fn into_raw(this: Self) -> *mut T { + ManuallyDrop::new(this).ptr.as_ptr() + } + /// Returns a raw pointer to the object. /// /// The pointer is valid for at least as long as the `Id` is held. @@ -257,17 +284,12 @@ impl Id { this.ptr.as_ptr() } - #[inline] - pub(crate) fn consume_as_ptr(this: Self) -> *mut T { - ManuallyDrop::new(this).ptr.as_ptr() - } - #[inline] pub(crate) fn consume_as_ptr_option(this: Option) -> *mut T where T: Sized, { - this.map(|this| Id::consume_as_ptr(this)) + this.map(|this| Id::into_raw(this)) .unwrap_or_else(ptr::null_mut) } }