Skip to content

Commit 643f875

Browse files
committed
Add Id::as_ptr helper function
1 parent d876e88 commit 643f875

File tree

5 files changed

+19
-7
lines changed

5 files changed

+19
-7
lines changed

objc2/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1111
upgrading easier.
1212
* Allow using `From`/`TryFrom` to convert between `rc::Id` and `rc::WeakId`.
1313
* Added `Bool::as_bool` (more descriptive name than `Bool::is_true`).
14-
* Added convenience method `Id::new_null`.
14+
* Added convenience methods `Id::new_null` and `Id::as_ptr`.
1515

1616

1717
## 0.3.0-alpha.6 - 2022-01-03

objc2/src/exception.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ extern "C" {
6868
#[inline]
6969
pub unsafe fn throw(exception: Option<&Id<Object, Shared>>) -> ! {
7070
let exception = match exception {
71-
Some(id) => &**id as *const Object as *mut ffi::objc_object,
71+
Some(id) => id.as_ptr() as *mut ffi::objc_object,
7272
None => ptr::null_mut(),
7373
};
7474
unsafe { ffi::objc_exception_throw(exception) }

objc2/src/message/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,7 @@ unsafe impl<T: Message + ?Sized> MessageReceiver for NonNull<T> {
252252
unsafe impl<T: Message + ?Sized, O: Ownership> MessageReceiver for Id<T, O> {
253253
#[inline]
254254
fn as_raw_receiver(&self) -> *mut Object {
255-
// TODO: Maybe don't dereference here, just to be safe?
256-
(&**self).as_raw_receiver()
255+
self.as_ptr() as *mut Object
257256
}
258257
}
259258

@@ -449,7 +448,7 @@ mod tests {
449448
};
450449
assert_eq!(result, 4);
451450

452-
let obj: *const ManuallyDrop<Object> = (&**obj as *const Object).cast();
451+
let obj: *const ManuallyDrop<Object> = obj.as_ptr().cast();
453452
let result: u32 = unsafe { msg_send![obj, foo] };
454453
assert_eq!(result, 4);
455454
}

objc2/src/rc/id.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,19 @@ impl<T: Message + ?Sized, O: Ownership> Id<T, O> {
187187
// SAFETY: Upheld by the caller
188188
NonNull::new(ptr).map(|ptr| unsafe { Id::new(ptr) })
189189
}
190+
191+
/// Returns a raw pointer to the object.
192+
///
193+
/// The pointer is valid for at least as long as the `Id` is held.
194+
#[inline]
195+
pub fn as_ptr(&self) -> *mut T {
196+
// Note: This is not an associated function, which breaks the
197+
// guideline that smart pointers shouldn't add inherent methods!
198+
//
199+
// However, this method is quite useful when migrating old codebases,
200+
// so I think we'll let it be here for now.
201+
self.ptr.as_ptr()
202+
}
190203
}
191204

192205
// TODO: Add ?Sized bound
@@ -246,7 +259,7 @@ impl<T: Message, O: Ownership> Id<T, O> {
246259
// retained objects it is hard to imagine a case where the inner type
247260
// has a method with the same name.
248261

249-
let ptr = ManuallyDrop::new(self).ptr.as_ptr() as *mut ffi::objc_object;
262+
let ptr = ManuallyDrop::new(self).as_ptr() as *mut ffi::objc_object;
250263
// SAFETY: The `ptr` is guaranteed to be valid and have at least one
251264
// retain count.
252265
// And because of the ManuallyDrop, we don't call the Drop

objc2/src/rc/weak_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl<T: Message> WeakId<T> {
4040
// allow loading an `Id<T, Shared>` later on.
4141

4242
// SAFETY: `obj` is valid
43-
unsafe { Self::new_inner(&**obj as *const T as *mut T) }
43+
unsafe { Self::new_inner(obj.as_ptr()) }
4444
}
4545

4646
/// # Safety

0 commit comments

Comments
 (0)