From 72974c519fc62506dcd086ba4b4b09f972794074 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Thu, 6 Jan 2022 22:28:36 +0100 Subject: [PATCH] Add Id::new_null helper function --- objc2/src/rc/id.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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