diff --git a/objc2/src/rc/id.rs b/objc2/src/rc/id.rs index 44ecc956f..6f5f53433 100644 --- a/objc2/src/rc/id.rs +++ b/objc2/src/rc/id.rs @@ -173,6 +173,20 @@ impl Id { own: PhantomData, } } + + /// Constructs an [`Id`] from a pointer that may be null. + /// + /// This is just a convenience wrapper over [`Id::new`] so that you don't + /// need to construct a [`NonNull`] when you know the pointer may be null. + /// + /// # Safety + /// + /// Same as [`Id::new`]. + #[inline] + pub unsafe fn new_null(ptr: *mut T) -> Option> { + // SAFETY: Upheld by the caller + NonNull::new(ptr).map(|ptr| unsafe { Id::new(ptr) }) + } } // TODO: Add ?Sized bound