Skip to content

Commit 1e39e34

Browse files
committed
Add 'from_ref' and 'from_mut' constructors to 'core::ptr::NonNull'; Add 'must_use' attribute to 'core::ptr::NonNull' constructors;
1 parent 4c62024 commit 1e39e34

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

Diff for: library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
#![feature(isqrt)]
175175
#![feature(lazy_get)]
176176
#![feature(link_cfg)]
177+
#![feature(non_null_from_ref)]
177178
#![feature(offset_of_enum)]
178179
#![feature(panic_internals)]
179180
#![feature(ptr_alignment_type)]

Diff for: library/core/src/ptr/non_null.rs

+26-6
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ impl<T: ?Sized> NonNull<T> {
191191
/// ```
192192
#[stable(feature = "nonnull", since = "1.25.0")]
193193
#[rustc_const_stable(feature = "const_nonnull_new_unchecked", since = "1.25.0")]
194+
#[must_use]
194195
#[inline]
195196
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
196197
// SAFETY: the caller must guarantee that `ptr` is non-null.
@@ -220,6 +221,7 @@ impl<T: ?Sized> NonNull<T> {
220221
/// ```
221222
#[stable(feature = "nonnull", since = "1.25.0")]
222223
#[rustc_const_unstable(feature = "const_nonnull_new", issue = "93235")]
224+
#[must_use]
223225
#[inline]
224226
pub const fn new(ptr: *mut T) -> Option<Self> {
225227
if !ptr.is_null() {
@@ -230,6 +232,26 @@ impl<T: ?Sized> NonNull<T> {
230232
}
231233
}
232234

235+
/// Converts a reference to a `NonNull` pointer.
236+
#[unstable(feature = "non_null_from_ref", issue = "130823")]
237+
#[rustc_const_unstable(feature = "non_null_from_ref", issue = "130823")]
238+
#[must_use]
239+
#[inline]
240+
pub const fn from_ref(r: &T) -> Self {
241+
// SAFETY: A reference cannot be null.
242+
unsafe { NonNull { pointer: r as *const T } }
243+
}
244+
245+
/// Converts a mutable reference to a `NonNull` pointer.
246+
#[unstable(feature = "non_null_from_ref", issue = "130823")]
247+
#[rustc_const_unstable(feature = "non_null_from_ref", issue = "130823")]
248+
#[must_use]
249+
#[inline]
250+
pub const fn from_mut(r: &mut T) -> Self {
251+
// SAFETY: A mutable reference cannot be null.
252+
unsafe { NonNull { pointer: r as *mut T } }
253+
}
254+
233255
/// Performs the same functionality as [`std::ptr::from_raw_parts`], except that a
234256
/// `NonNull` pointer is returned, as opposed to a raw `*const` pointer.
235257
///
@@ -1753,9 +1775,8 @@ impl<T: ?Sized> From<&mut T> for NonNull<T> {
17531775
///
17541776
/// This conversion is safe and infallible since references cannot be null.
17551777
#[inline]
1756-
fn from(reference: &mut T) -> Self {
1757-
// SAFETY: A mutable reference cannot be null.
1758-
unsafe { NonNull { pointer: reference as *mut T } }
1778+
fn from(r: &mut T) -> Self {
1779+
NonNull::from_mut(r)
17591780
}
17601781
}
17611782

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

0 commit comments

Comments
 (0)