Skip to content

Commit 2f26ed6

Browse files
committed
Add Id::retain_null helper function
1 parent 643f875 commit 2f26ed6

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

objc2/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ 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 methods `Id::new_null` and `Id::as_ptr`.
14+
* Added convenience methods `Id::new_null`, `Id::as_ptr` and
15+
`Id::retain_null`.
1516

1617

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

objc2/src/rc/id.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,21 @@ impl<T: Message, O: Ownership> Id<T, O> {
251251
unsafe { Self::new(res) }
252252
}
253253

254+
/// Retains an object pointer that may be null.
255+
///
256+
/// This is just a convenience wrapper over [`Id::retain`] so that you
257+
/// don't need to construct a [`NonNull`] when you know the pointer may
258+
/// be null.
259+
///
260+
/// # Safety
261+
///
262+
/// Same as [`Id::retain`].
263+
#[inline]
264+
pub unsafe fn retain_null(ptr: *mut T) -> Option<Id<T, O>> {
265+
// SAFETY: Upheld by the caller
266+
NonNull::new(ptr).map(|ptr| unsafe { Id::retain(ptr) })
267+
}
268+
254269
#[cfg_attr(not(debug_assertions), inline)]
255270
fn autorelease_inner(self) -> *mut T {
256271
// Note that this (and the actual `autorelease`) is not an associated

0 commit comments

Comments
 (0)