Skip to content

Commit d876e88

Browse files
committed
Add Id::new_null helper function
1 parent 9fa2ef5 commit d876e88

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

objc2/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +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`.
1415

1516

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

objc2/src/rc/id.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ impl<T: Message + ?Sized, O: Ownership> Id<T, O> {
173173
own: PhantomData,
174174
}
175175
}
176+
177+
/// Constructs an [`Id`] from a pointer that may be null.
178+
///
179+
/// This is just a convenience wrapper over [`Id::new`] so that you don't
180+
/// need to construct a [`NonNull`] when you know the pointer may be null.
181+
///
182+
/// # Safety
183+
///
184+
/// Same as [`Id::new`].
185+
#[inline]
186+
pub unsafe fn new_null(ptr: *mut T) -> Option<Id<T, O>> {
187+
// SAFETY: Upheld by the caller
188+
NonNull::new(ptr).map(|ptr| unsafe { Id::new(ptr) })
189+
}
176190
}
177191

178192
// TODO: Add ?Sized bound

0 commit comments

Comments
 (0)