Skip to content

Commit

Permalink
Add Id::as_ptr helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jan 6, 2022
1 parent 72974c5 commit ee4d475
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
3 changes: 1 addition & 2 deletions objc2/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,7 @@ unsafe impl<T: Message + ?Sized> MessageReceiver for NonNull<T> {
unsafe impl<T: Message + ?Sized, O: Ownership> MessageReceiver for Id<T, O> {
#[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
}
}

Expand Down
15 changes: 14 additions & 1 deletion objc2/src/rc/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,19 @@ impl<T: Message + ?Sized, O: Ownership> Id<T, O> {
// 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
Expand Down Expand Up @@ -246,7 +259,7 @@ impl<T: Message, O: Ownership> Id<T, O> {
// 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
Expand Down
2 changes: 1 addition & 1 deletion objc2/src/rc/weak_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl<T: Message> WeakId<T> {
// allow loading an `Id<T, Shared>` 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
Expand Down

0 comments on commit ee4d475

Please sign in to comment.