diff --git a/objc2/src/message/mod.rs b/objc2/src/message/mod.rs index 3b2e530b0..aff01ac08 100644 --- a/objc2/src/message/mod.rs +++ b/objc2/src/message/mod.rs @@ -252,8 +252,7 @@ unsafe impl MessageReceiver for NonNull { unsafe impl MessageReceiver for Id { #[inline] fn as_raw_receiver(&self) -> *mut Object { - // TODO: Maybe don't dereference here, just to be safe? - (&**self).as_raw_receiver() + self.as_ptr() as *mut Object } } diff --git a/objc2/src/rc/id.rs b/objc2/src/rc/id.rs index 6f5f53433..4183fc962 100644 --- a/objc2/src/rc/id.rs +++ b/objc2/src/rc/id.rs @@ -187,6 +187,19 @@ impl Id { // SAFETY: Upheld by the caller NonNull::new(ptr).map(|ptr| unsafe { Id::new(ptr) }) } + + /// Returns a raw pointer to the object. + /// + /// The pointer is valid for at least as long as the `Id` is held. + #[inline] + pub fn as_ptr(&self) -> *mut T { + // Note: This is not an associated function, which breaks the + // guideline that smart pointers shouldn't add inherent methods! + // + // However, this method is quite useful when migrating old codebases, + // so I think we'll let it be here for now. + self.ptr.as_ptr() + } } // TODO: Add ?Sized bound @@ -246,7 +259,7 @@ impl Id { // retained objects it is hard to imagine a case where the inner type // has a method with the same name. - let ptr = ManuallyDrop::new(self).ptr.as_ptr() as *mut ffi::objc_object; + let ptr = ManuallyDrop::new(self).as_ptr() as *mut ffi::objc_object; // SAFETY: The `ptr` is guaranteed to be valid and have at least one // retain count. // And because of the ManuallyDrop, we don't call the Drop diff --git a/objc2/src/rc/weak_id.rs b/objc2/src/rc/weak_id.rs index 509e223b2..e245b486d 100644 --- a/objc2/src/rc/weak_id.rs +++ b/objc2/src/rc/weak_id.rs @@ -38,7 +38,7 @@ impl WeakId { // allow loading an `Id` later on. // SAFETY: `obj` is valid - unsafe { Self::new_inner(&**obj as *const T as *mut T) } + unsafe { Self::new_inner(obj.as_ptr()) } } /// # Safety