Add new_checked(…) -> Option<Self> to NonZero, Unique, and Shared.
This commit is contained in:
@@ -1110,10 +1110,15 @@ impl<T: ?Sized> Unique<T> {
|
||||
/// # Safety
|
||||
///
|
||||
/// `ptr` must be non-null.
|
||||
pub const unsafe fn new(ptr: *mut T) -> Unique<T> {
|
||||
pub const unsafe fn new(ptr: *mut T) -> Self {
|
||||
Unique { pointer: NonZero::new(ptr), _marker: PhantomData }
|
||||
}
|
||||
|
||||
/// Creates a new `Unique` if `ptr` is non-null.
|
||||
pub fn new_checked(ptr: *mut T) -> Option<Self> {
|
||||
NonZero::new_checked(ptr as *const T).map(|nz| Unique { pointer: nz, _marker: PhantomData })
|
||||
}
|
||||
|
||||
/// Acquires the underlying `*mut` pointer.
|
||||
pub fn as_ptr(self) -> *mut T {
|
||||
self.pointer.get() as *mut T
|
||||
@@ -1224,10 +1229,15 @@ impl<T: ?Sized> Shared<T> {
|
||||
/// # Safety
|
||||
///
|
||||
/// `ptr` must be non-null.
|
||||
pub unsafe fn new(ptr: *mut T) -> Self {
|
||||
pub const unsafe fn new(ptr: *mut T) -> Self {
|
||||
Shared { pointer: NonZero::new(ptr), _marker: PhantomData }
|
||||
}
|
||||
|
||||
/// Creates a new `Shared` if `ptr` is non-null.
|
||||
pub fn new_checked(ptr: *mut T) -> Option<Self> {
|
||||
NonZero::new_checked(ptr as *const T).map(|nz| Shared { pointer: nz, _marker: PhantomData })
|
||||
}
|
||||
|
||||
/// Acquires the underlying `*mut` pointer.
|
||||
pub fn as_ptr(self) -> *mut T {
|
||||
self.pointer.get() as *mut T
|
||||
|
||||
Reference in New Issue
Block a user