diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs index 28de28c70e4..bd89ec27ae2 100644 --- a/library/core/src/ptr/const_ptr.rs +++ b/library/core/src/ptr/const_ptr.rs @@ -48,6 +48,17 @@ impl *const T { self as _ } + /// Decompose a (possibly wide) pointer into is address and metadata components. + /// + /// The pointer can be later reconstructed with [`from_raw_parts`]. + #[cfg(not(bootstrap))] + #[unstable(feature = "ptr_metadata", issue = /* FIXME */ "none")] + #[rustc_const_unstable(feature = "ptr_metadata", issue = /* FIXME */ "none")] + #[inline] + pub const fn to_raw_parts(self) -> (*const (), ::Metadata) { + (self.cast(), super::metadata(self)) + } + /// Returns `None` if the pointer is null, or else returns a shared reference to /// the value wrapped in `Some`. If the value may be uninitialized, [`as_uninit_ref`] /// must be used instead. diff --git a/library/core/src/ptr/metadata.rs b/library/core/src/ptr/metadata.rs index 136e986b376..0661ccc0200 100644 --- a/library/core/src/ptr/metadata.rs +++ b/library/core/src/ptr/metadata.rs @@ -28,8 +28,9 @@ pub trait Pointee { pub trait Thin = Pointee; /// Extract the metadata component of a pointer. +#[rustc_const_unstable(feature = "ptr_metadata", issue = /* FIXME */ "none")] #[inline] -pub fn metadata(ptr: *const T) -> ::Metadata { +pub const fn metadata(ptr: *const T) -> ::Metadata { // SAFETY: Accessing the value from the `PtrRepr` union is safe since *const T // and PtrComponents have the same memory layouts. Only std can make this // guarantee. diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs index 99744fc7112..731b97a06cb 100644 --- a/library/core/src/ptr/mut_ptr.rs +++ b/library/core/src/ptr/mut_ptr.rs @@ -47,6 +47,17 @@ impl *mut T { self as _ } + /// Decompose a (possibly wide) pointer into is address and metadata components. + /// + /// The pointer can be later reconstructed with [`from_raw_parts_mut`]. + #[cfg(not(bootstrap))] + #[unstable(feature = "ptr_metadata", issue = /* FIXME */ "none")] + #[rustc_const_unstable(feature = "ptr_metadata", issue = /* FIXME */ "none")] + #[inline] + pub const fn to_raw_parts(self) -> (*mut (), ::Metadata) { + (self.cast(), super::metadata(self)) + } + /// Returns `None` if the pointer is null, or else returns a shared reference to /// the value wrapped in `Some`. If the value may be uninitialized, [`as_uninit_ref`] /// must be used instead. diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs index ef7a7b974a6..00cb1e1b271 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -193,6 +193,17 @@ impl NonNull { } } + /// Decompose a (possibly wide) pointer into is address and metadata components. + /// + /// The pointer can be later reconstructed with [`NonNull::from_raw_parts`]. + #[cfg(not(bootstrap))] + #[unstable(feature = "ptr_metadata", issue = /* FIXME */ "none")] + #[rustc_const_unstable(feature = "ptr_metadata", issue = /* FIXME */ "none")] + #[inline] + pub const fn to_raw_parts(self) -> (NonNull<()>, ::Metadata) { + (self.cast(), super::metadata(self.as_ptr())) + } + /// Acquires the underlying `*mut` pointer. #[stable(feature = "nonnull", since = "1.25.0")] #[rustc_const_stable(feature = "const_nonnull_as_ptr", since = "1.32.0")]