Skip to content

Commit

Permalink
Add 'from_ref' and 'from_mut' constructors to 'core::ptr::NonNull'; A…
Browse files Browse the repository at this point in the history
…dd 'must_use' attribute to 'core::ptr::NonNull' constructors;
  • Loading branch information
bjoernager committed Sep 25, 2024
1 parent 4c62024 commit 2a97fdd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
#![feature(isqrt)]
#![feature(lazy_get)]
#![feature(link_cfg)]
#![feature(non_null_from_ref)]
#![feature(offset_of_enum)]
#![feature(panic_internals)]
#![feature(ptr_alignment_type)]
Expand Down
32 changes: 26 additions & 6 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ impl<T: ?Sized> NonNull<T> {
/// ```
#[stable(feature = "nonnull", since = "1.25.0")]
#[rustc_const_stable(feature = "const_nonnull_new_unchecked", since = "1.25.0")]
#[must_use]
#[inline]
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
// SAFETY: the caller must guarantee that `ptr` is non-null.
Expand Down Expand Up @@ -220,6 +221,7 @@ impl<T: ?Sized> NonNull<T> {
/// ```
#[stable(feature = "nonnull", since = "1.25.0")]
#[rustc_const_unstable(feature = "const_nonnull_new", issue = "93235")]
#[must_use]
#[inline]
pub const fn new(ptr: *mut T) -> Option<Self> {
if !ptr.is_null() {
Expand All @@ -230,6 +232,26 @@ impl<T: ?Sized> NonNull<T> {
}
}

/// Converts a reference to a `NonNull` pointer.
#[unstable(feature = "non_null_from_ref", issue = "130823")]
#[rustc_const_unstable(feature = "non_null_from_ref", issue = "130823")]
#[must_use]
#[inline]
pub const fn from_ref(r: &T) -> Self {
// SAFETY: A reference cannot be null.
unsafe { NonNull { pointer: reference as *const T } }
}

/// Converts a mutable reference to a `NonNull` pointer.
#[unstable(feature = "non_null_from_ref", issue = "130823")]
#[rustc_const_unstable(feature = "non_null_from_ref", issue = "130823")]
#[must_use]
#[inline]
pub const fn from_mut(r: &mut T) -> Self {
// SAFETY: A mutable reference cannot be null.
unsafe { NonNull { pointer: reference as *mut T } }
}

/// Performs the same functionality as [`std::ptr::from_raw_parts`], except that a
/// `NonNull` pointer is returned, as opposed to a raw `*const` pointer.
///
Expand Down Expand Up @@ -1753,9 +1775,8 @@ impl<T: ?Sized> From<&mut T> for NonNull<T> {
///
/// This conversion is safe and infallible since references cannot be null.
#[inline]
fn from(reference: &mut T) -> Self {
// SAFETY: A mutable reference cannot be null.
unsafe { NonNull { pointer: reference as *mut T } }
fn from(r: &mut T) -> Self {
NonNull::from_mut(r)
}
}

Expand All @@ -1765,8 +1786,7 @@ impl<T: ?Sized> From<&T> for NonNull<T> {
///
/// This conversion is safe and infallible since references cannot be null.
#[inline]
fn from(reference: &T) -> Self {
// SAFETY: A reference cannot be null.
unsafe { NonNull { pointer: reference as *const T } }
fn from(r: &T) -> Self {
NonNull::from_ref(r)
}
}

0 comments on commit 2a97fdd

Please sign in to comment.