Skip to content

Commit c0e3a1b

Browse files
committed
Add to_raw_parts methods to *const, *mut, and NonNull
These are not named `into_` because they do not consume their receiver since raw pointers are `Copy`.
1 parent 937d580 commit c0e3a1b

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

library/core/src/ptr/const_ptr.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ impl<T: ?Sized> *const T {
4848
self as _
4949
}
5050

51+
/// Decompose a (possibly wide) pointer into is address and metadata components.
52+
///
53+
/// The pointer can be later reconstructed with [`from_raw_parts`].
54+
#[cfg(not(bootstrap))]
55+
#[unstable(feature = "ptr_metadata", issue = /* FIXME */ "none")]
56+
#[rustc_const_unstable(feature = "ptr_metadata", issue = /* FIXME */ "none")]
57+
#[inline]
58+
pub const fn to_raw_parts(self) -> (*const (), <T as super::Pointee>::Metadata) {
59+
(self.cast(), super::metadata(self))
60+
}
61+
5162
/// Returns `None` if the pointer is null, or else returns a shared reference to
5263
/// the value wrapped in `Some`. If the value may be uninitialized, [`as_uninit_ref`]
5364
/// must be used instead.

library/core/src/ptr/metadata.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ pub trait Pointee {
2828
pub trait Thin = Pointee<Metadata = ()>;
2929

3030
/// Extract the metadata component of a pointer.
31+
#[rustc_const_unstable(feature = "ptr_metadata", issue = /* FIXME */ "none")]
3132
#[inline]
32-
pub fn metadata<T: ?Sized>(ptr: *const T) -> <T as Pointee>::Metadata {
33+
pub const fn metadata<T: ?Sized>(ptr: *const T) -> <T as Pointee>::Metadata {
3334
// SAFETY: Accessing the value from the `PtrRepr` union is safe since *const T
3435
// and PtrComponents<T> have the same memory layouts. Only std can make this
3536
// guarantee.

library/core/src/ptr/mut_ptr.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ impl<T: ?Sized> *mut T {
4747
self as _
4848
}
4949

50+
/// Decompose a (possibly wide) pointer into is address and metadata components.
51+
///
52+
/// The pointer can be later reconstructed with [`from_raw_parts_mut`].
53+
#[cfg(not(bootstrap))]
54+
#[unstable(feature = "ptr_metadata", issue = /* FIXME */ "none")]
55+
#[rustc_const_unstable(feature = "ptr_metadata", issue = /* FIXME */ "none")]
56+
#[inline]
57+
pub const fn to_raw_parts(self) -> (*mut (), <T as super::Pointee>::Metadata) {
58+
(self.cast(), super::metadata(self))
59+
}
60+
5061
/// Returns `None` if the pointer is null, or else returns a shared reference to
5162
/// the value wrapped in `Some`. If the value may be uninitialized, [`as_uninit_ref`]
5263
/// must be used instead.

library/core/src/ptr/non_null.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,17 @@ impl<T: ?Sized> NonNull<T> {
193193
}
194194
}
195195

196+
/// Decompose a (possibly wide) pointer into is address and metadata components.
197+
///
198+
/// The pointer can be later reconstructed with [`NonNull::from_raw_parts`].
199+
#[cfg(not(bootstrap))]
200+
#[unstable(feature = "ptr_metadata", issue = /* FIXME */ "none")]
201+
#[rustc_const_unstable(feature = "ptr_metadata", issue = /* FIXME */ "none")]
202+
#[inline]
203+
pub const fn to_raw_parts(self) -> (NonNull<()>, <T as super::Pointee>::Metadata) {
204+
(self.cast(), super::metadata(self.as_ptr()))
205+
}
206+
196207
/// Acquires the underlying `*mut` pointer.
197208
#[stable(feature = "nonnull", since = "1.25.0")]
198209
#[rustc_const_stable(feature = "const_nonnull_as_ptr", since = "1.32.0")]

0 commit comments

Comments
 (0)