diff options
| author | bors <bors@rust-lang.org> | 2024-10-16 17:58:25 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-10-16 17:58:25 +0000 |
| commit | 7342830c05ec0996e9e4b7df550b1043dca7829c (patch) | |
| tree | 8299ba13bde1b11d0062029a84f63c73e736dbbb /library/core/src/ptr | |
| parent | bed75e7c21e8d18bd536a0f7c9e479d2f6707db3 (diff) | |
| parent | 06cd22ceab1a5dc01626958c5f41401ec237807a (diff) | |
Auto merge of #131792 - matthiaskrgr:rollup-480nwg4, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - #130822 (Add `from_ref` and `from_mut` constructors to `core::ptr::NonNull`.) - #131381 (Implement edition 2024 match ergonomics restrictions) - #131594 (rustdoc: Rename "object safe" to "dyn compatible") - #131686 (Add fast-path when computing the default visibility) - #131699 (Try to improve error messages involving aliases in the solver) - #131757 (Ignore lint-non-snake-case-crate#proc_macro_ on targets without unwind) - #131783 (Fix explicit_iter_loop in rustc_serialize) - #131788 (Fix mismatched quotation mark) r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'library/core/src/ptr')
| -rw-r--r-- | library/core/src/ptr/non_null.rs | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs index dfd0bd8fd1e..3e4cae2b3ca 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -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. /// @@ -1749,9 +1767,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) } } @@ -1761,8 +1778,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) } } |
