Skip to content

Commit

Permalink
Add Id::new_null helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jan 6, 2022
1 parent ec2b90b commit 72974c5
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions objc2/src/rc/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@ impl<T: Message + ?Sized, O: Ownership> Id<T, O> {
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<Id<T, O>> {
// SAFETY: Upheld by the caller
NonNull::new(ptr).map(|ptr| unsafe { Id::new(ptr) })
}
}

// TODO: Add ?Sized bound
Expand Down

0 comments on commit 72974c5

Please sign in to comment.