Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add from_ref and from_mut constructors to core::ptr::NonNull. #130822

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
28 changes: 22 additions & 6 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,24 @@ 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")]
#[inline]
pub const fn from_ref(r: &T) -> Self {
// SAFETY: A reference cannot be null.
unsafe { NonNull { pointer: r 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")]
#[inline]
pub const fn from_mut(r: &mut T) -> Self {
// SAFETY: A mutable reference cannot be null.
unsafe { NonNull { pointer: r 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 +1771,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 +1782,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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,30 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
debug i => _22;
debug x => _23;
}
scope 17 (inlined <Enumerate<std::slice::Iter<'_, T>> as Iterator>::next) {
scope 18 (inlined <Enumerate<std::slice::Iter<'_, T>> as Iterator>::next) {
let mut _14: &mut std::slice::Iter<'_, T>;
let mut _15: std::option::Option<&T>;
let mut _19: (usize, bool);
let mut _20: (usize, &T);
scope 18 {
scope 19 {
let _18: usize;
scope 23 {
scope 24 {
}
}
scope 19 {
scope 20 {
scope 26 (inlined <Option<(usize, &T)> as FromResidual<Option<Infallible>>>::from_residual) {
scope 20 {
scope 21 {
scope 27 (inlined <Option<(usize, &T)> as FromResidual<Option<Infallible>>>::from_residual) {
}
}
}
scope 21 {
scope 22 {
scope 22 {
scope 23 {
}
}
scope 24 (inlined <Option<&T> as Try>::branch) {
scope 25 (inlined <Option<&T> as Try>::branch) {
let mut _16: isize;
let _17: &T;
scope 25 {
scope 26 {
}
}
}
Expand All @@ -59,29 +59,31 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
let _9: *const T;
scope 7 {
}
scope 11 (inlined without_provenance::<T>) {
scope 12 (inlined without_provenance::<T>) {
}
scope 12 (inlined NonNull::<T>::as_ptr) {
scope 13 (inlined NonNull::<T>::as_ptr) {
}
scope 13 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
scope 14 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
}
}
scope 8 (inlined <NonNull<[T]> as From<&[T]>>::from) {
let mut _4: *const [T];
scope 9 (inlined NonNull::<[T]>::from_ref) {
let mut _4: *const [T];
}
}
scope 9 (inlined NonNull::<[T]>::cast::<T>) {
scope 10 (inlined NonNull::<[T]>::cast::<T>) {
let mut _5: *const T;
scope 10 (inlined NonNull::<[T]>::as_ptr) {
scope 11 (inlined NonNull::<[T]>::as_ptr) {
}
}
}
}
}
scope 14 (inlined <std::slice::Iter<'_, T> as Iterator>::enumerate) {
scope 15 (inlined Enumerate::<std::slice::Iter<'_, T>>::new) {
scope 15 (inlined <std::slice::Iter<'_, T> as Iterator>::enumerate) {
scope 16 (inlined Enumerate::<std::slice::Iter<'_, T>>::new) {
}
}
scope 16 (inlined <Enumerate<std::slice::Iter<'_, T>> as IntoIterator>::into_iter) {
scope 17 (inlined <Enumerate<std::slice::Iter<'_, T>> as IntoIterator>::into_iter) {
}

bb0: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,31 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
let _9: *const T;
scope 7 {
}
scope 11 (inlined without_provenance::<T>) {
scope 12 (inlined without_provenance::<T>) {
}
scope 12 (inlined NonNull::<T>::as_ptr) {
scope 13 (inlined NonNull::<T>::as_ptr) {
}
scope 13 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
scope 14 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
}
}
scope 8 (inlined <NonNull<[T]> as From<&[T]>>::from) {
let mut _4: *const [T];
scope 9 (inlined NonNull::<[T]>::from_ref) {
let mut _4: *const [T];
}
}
scope 9 (inlined NonNull::<[T]>::cast::<T>) {
scope 10 (inlined NonNull::<[T]>::cast::<T>) {
let mut _5: *const T;
scope 10 (inlined NonNull::<[T]>::as_ptr) {
scope 11 (inlined NonNull::<[T]>::as_ptr) {
}
}
}
}
}
scope 14 (inlined <std::slice::Iter<'_, T> as Iterator>::enumerate) {
scope 15 (inlined Enumerate::<std::slice::Iter<'_, T>>::new) {
scope 15 (inlined <std::slice::Iter<'_, T> as Iterator>::enumerate) {
scope 16 (inlined Enumerate::<std::slice::Iter<'_, T>>::new) {
}
}
scope 16 (inlined <Enumerate<std::slice::Iter<'_, T>> as IntoIterator>::into_iter) {
scope 17 (inlined <Enumerate<std::slice::Iter<'_, T>> as IntoIterator>::into_iter) {
}

bb0: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,27 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
let _9: *const T;
scope 7 {
}
scope 11 (inlined without_provenance::<T>) {
scope 12 (inlined without_provenance::<T>) {
}
scope 12 (inlined NonNull::<T>::as_ptr) {
scope 13 (inlined NonNull::<T>::as_ptr) {
}
scope 13 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
scope 14 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
}
}
scope 8 (inlined <NonNull<[T]> as From<&[T]>>::from) {
let mut _4: *const [T];
scope 9 (inlined NonNull::<[T]>::from_ref) {
let mut _4: *const [T];
}
}
scope 9 (inlined NonNull::<[T]>::cast::<T>) {
scope 10 (inlined NonNull::<[T]>::cast::<T>) {
let mut _5: *const T;
scope 10 (inlined NonNull::<[T]>::as_ptr) {
scope 11 (inlined NonNull::<[T]>::as_ptr) {
}
}
}
}
}
scope 14 (inlined <std::slice::Iter<'_, T> as IntoIterator>::into_iter) {
scope 15 (inlined <std::slice::Iter<'_, T> as IntoIterator>::into_iter) {
}

bb0: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,27 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
let _9: *const T;
scope 7 {
}
scope 11 (inlined without_provenance::<T>) {
scope 12 (inlined without_provenance::<T>) {
}
scope 12 (inlined NonNull::<T>::as_ptr) {
scope 13 (inlined NonNull::<T>::as_ptr) {
}
scope 13 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
scope 14 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
}
}
scope 8 (inlined <NonNull<[T]> as From<&[T]>>::from) {
let mut _4: *const [T];
scope 9 (inlined NonNull::<[T]>::from_ref) {
let mut _4: *const [T];
}
}
scope 9 (inlined NonNull::<[T]>::cast::<T>) {
scope 10 (inlined NonNull::<[T]>::cast::<T>) {
let mut _5: *const T;
scope 10 (inlined NonNull::<[T]>::as_ptr) {
scope 11 (inlined NonNull::<[T]>::as_ptr) {
}
}
}
}
}
scope 14 (inlined <std::slice::Iter<'_, T> as IntoIterator>::into_iter) {
scope 15 (inlined <std::slice::Iter<'_, T> as IntoIterator>::into_iter) {
}

bb0: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
scope 2 {
debug x => _17;
}
scope 17 (inlined <Rev<std::slice::Iter<'_, T>> as Iterator>::next) {
scope 18 (inlined <Rev<std::slice::Iter<'_, T>> as Iterator>::next) {
let mut _14: &mut std::slice::Iter<'_, T>;
}
}
Expand All @@ -34,29 +34,31 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
let _9: *const T;
scope 7 {
}
scope 11 (inlined without_provenance::<T>) {
scope 12 (inlined without_provenance::<T>) {
}
scope 12 (inlined NonNull::<T>::as_ptr) {
scope 13 (inlined NonNull::<T>::as_ptr) {
}
scope 13 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
scope 14 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
}
}
scope 8 (inlined <NonNull<[T]> as From<&[T]>>::from) {
let mut _4: *const [T];
scope 9 (inlined NonNull::<[T]>::from_ref) {
let mut _4: *const [T];
}
}
scope 9 (inlined NonNull::<[T]>::cast::<T>) {
scope 10 (inlined NonNull::<[T]>::cast::<T>) {
let mut _5: *const T;
scope 10 (inlined NonNull::<[T]>::as_ptr) {
scope 11 (inlined NonNull::<[T]>::as_ptr) {
}
}
}
}
}
scope 14 (inlined <std::slice::Iter<'_, T> as Iterator>::rev) {
scope 15 (inlined Rev::<std::slice::Iter<'_, T>>::new) {
scope 15 (inlined <std::slice::Iter<'_, T> as Iterator>::rev) {
scope 16 (inlined Rev::<std::slice::Iter<'_, T>>::new) {
}
}
scope 16 (inlined <Rev<std::slice::Iter<'_, T>> as IntoIterator>::into_iter) {
scope 17 (inlined <Rev<std::slice::Iter<'_, T>> as IntoIterator>::into_iter) {
}

bb0: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
scope 2 {
debug x => _17;
}
scope 17 (inlined <Rev<std::slice::Iter<'_, T>> as Iterator>::next) {
scope 18 (inlined <Rev<std::slice::Iter<'_, T>> as Iterator>::next) {
let mut _14: &mut std::slice::Iter<'_, T>;
}
}
Expand All @@ -34,29 +34,31 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
let _9: *const T;
scope 7 {
}
scope 11 (inlined without_provenance::<T>) {
scope 12 (inlined without_provenance::<T>) {
}
scope 12 (inlined NonNull::<T>::as_ptr) {
scope 13 (inlined NonNull::<T>::as_ptr) {
}
scope 13 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
scope 14 (inlined std::ptr::mut_ptr::<impl *mut T>::add) {
}
}
scope 8 (inlined <NonNull<[T]> as From<&[T]>>::from) {
let mut _4: *const [T];
scope 9 (inlined NonNull::<[T]>::from_ref) {
let mut _4: *const [T];
}
}
scope 9 (inlined NonNull::<[T]>::cast::<T>) {
scope 10 (inlined NonNull::<[T]>::cast::<T>) {
let mut _5: *const T;
scope 10 (inlined NonNull::<[T]>::as_ptr) {
scope 11 (inlined NonNull::<[T]>::as_ptr) {
}
}
}
}
}
scope 14 (inlined <std::slice::Iter<'_, T> as Iterator>::rev) {
scope 15 (inlined Rev::<std::slice::Iter<'_, T>>::new) {
scope 15 (inlined <std::slice::Iter<'_, T> as Iterator>::rev) {
scope 16 (inlined Rev::<std::slice::Iter<'_, T>>::new) {
}
}
scope 16 (inlined <Rev<std::slice::Iter<'_, T>> as IntoIterator>::into_iter) {
scope 17 (inlined <Rev<std::slice::Iter<'_, T>> as IntoIterator>::into_iter) {
}

bb0: {
Expand Down
Loading