Skip to content

Commit

Permalink
Add to_raw_parts methods to *const, *mut, and NonNull
Browse files Browse the repository at this point in the history
These are not named `into_` because they do not consume their receiver
since raw pointers are `Copy`.
  • Loading branch information
SimonSapin committed Feb 15, 2021
1 parent 937d580 commit c0e3a1b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
11 changes: 11 additions & 0 deletions library/core/src/ptr/const_ptr.rs
Expand Up @@ -48,6 +48,17 @@ impl<T: ?Sized> *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 (), <T as super::Pointee>::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.
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/ptr/metadata.rs
Expand Up @@ -28,8 +28,9 @@ pub trait Pointee {
pub trait Thin = Pointee<Metadata = ()>;

/// Extract the metadata component of a pointer.
#[rustc_const_unstable(feature = "ptr_metadata", issue = /* FIXME */ "none")]
#[inline]
pub fn metadata<T: ?Sized>(ptr: *const T) -> <T as Pointee>::Metadata {
pub const fn metadata<T: ?Sized>(ptr: *const T) -> <T as Pointee>::Metadata {
// SAFETY: Accessing the value from the `PtrRepr` union is safe since *const T
// and PtrComponents<T> have the same memory layouts. Only std can make this
// guarantee.
Expand Down
11 changes: 11 additions & 0 deletions library/core/src/ptr/mut_ptr.rs
Expand Up @@ -47,6 +47,17 @@ impl<T: ?Sized> *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 (), <T as super::Pointee>::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.
Expand Down
11 changes: 11 additions & 0 deletions library/core/src/ptr/non_null.rs
Expand Up @@ -193,6 +193,17 @@ impl<T: ?Sized> NonNull<T> {
}
}

/// 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<()>, <T as super::Pointee>::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")]
Expand Down

0 comments on commit c0e3a1b

Please sign in to comment.