From 8f5b8a0b4d792283a932ab4835adfe40e30ba7a3 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Thu, 6 Jan 2022 22:27:48 +0100 Subject: [PATCH] Add Id::retain_null helper function --- objc2/src/rc/id.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/objc2/src/rc/id.rs b/objc2/src/rc/id.rs index 4183fc962..a16a8d79b 100644 --- a/objc2/src/rc/id.rs +++ b/objc2/src/rc/id.rs @@ -251,6 +251,21 @@ impl Id { unsafe { Self::new(res) } } + /// Retains an object pointer that may be null. + /// + /// This is just a convenience wrapper over [`Id::retain`] so that you + /// don't need to construct a [`NonNull`] when you know the pointer may + /// be null. + /// + /// # Safety + /// + /// Same as [`Id::retain`]. + #[inline] + pub unsafe fn retain_null(ptr: *mut T) -> Option> { + // SAFETY: Upheld by the caller + NonNull::new(ptr).map(|ptr| unsafe { Id::retain(ptr) }) + } + #[cfg_attr(not(debug_assertions), inline)] fn autorelease_inner(self) -> *mut T { // Note that this (and the actual `autorelease`) is not an associated