|
| 1 | +//! This encapsulates the layout knowledge for pointers, only exporting the two |
| 2 | +//! safe functions that can be used to interact with the metadata directly. |
| 3 | +
|
| 4 | +use super::{NonNull, Pointee, Thin}; |
| 5 | + |
| 6 | +#[inline] |
| 7 | +pub(super) const fn metadata<P: RawPointer>(ptr: P) -> <P::Pointee as Pointee>::Metadata { |
| 8 | + // SAFETY: Transmuting like this is safe since `P` and `PtrComponents<P, _>` |
| 9 | + // have the same memory layouts. Only std can make this guarantee. |
| 10 | + unsafe { |
| 11 | + crate::intrinsics::transmute_unchecked::< |
| 12 | + P, |
| 13 | + PtrComponents<P::Family, <P::Pointee as Pointee>::Metadata>, |
| 14 | + >(ptr) |
| 15 | + .metadata |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +/// Just like [`from_raw_parts`] and [`from_raw_parts_mut`], but more flexible |
| 20 | +/// in terms of which types it can take, allowing smaller MIR. |
| 21 | +// See <https://github.com./rust-lang/rust/issues/123174> |
| 22 | +#[rustc_const_unstable(feature = "ptr_metadata", issue = "81513")] |
| 23 | +#[inline] |
| 24 | +pub(crate) const fn from_raw_parts<P: RawPointer>( |
| 25 | + data_pointer: impl RawPointer<Pointee: Thin, Family = P::Family>, |
| 26 | + metadata: <P::Pointee as Pointee>::Metadata, |
| 27 | +) -> P { |
| 28 | + // SAFETY: Transmuting like this is safe since `P` and `PtrComponents<P, _>` |
| 29 | + // have the same memory layouts. Only std can make this guarantee. |
| 30 | + unsafe { |
| 31 | + crate::intrinsics::transmute_unchecked::< |
| 32 | + PtrComponents<_, <P::Pointee as Pointee>::Metadata>, |
| 33 | + P, |
| 34 | + >(PtrComponents { data_pointer, metadata }) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// Intentionally private with no derives, as it's only used via transmuting. |
| 39 | +// This layout is not stable; only std can rely on it. |
| 40 | +// (And should only do so in the two functions in this module.) |
| 41 | +#[repr(C)] |
| 42 | +struct PtrComponents<P, M = ()> { |
| 43 | + data_pointer: P, |
| 44 | + metadata: M, |
| 45 | +} |
| 46 | + |
| 47 | +/// Internal trait to avoid bad instantiations of [`PtrComponents`] |
| 48 | +/// |
| 49 | +/// # Safety |
| 50 | +/// |
| 51 | +/// Must have the same layout as `*const Self::Pointee` and be able to hold provenance. |
| 52 | +/// |
| 53 | +/// Every type with the same associated `Family` must be soundly transmutable |
| 54 | +/// between each other when the metadata is the same. |
| 55 | +pub unsafe trait RawPointer: Copy { |
| 56 | + type Pointee: ?Sized + super::Pointee; |
| 57 | + type Family: RawPointer<Pointee = (), Family = Self::Family>; |
| 58 | +} |
| 59 | + |
| 60 | +// SAFETY: `*const T` is obviously a raw pointer |
| 61 | +unsafe impl<T: ?Sized> RawPointer for *const T { |
| 62 | + type Pointee = T; |
| 63 | + type Family = *const (); |
| 64 | +} |
| 65 | +// SAFETY: `*mut T` is obviously a raw pointer |
| 66 | +unsafe impl<T: ?Sized> RawPointer for *mut T { |
| 67 | + type Pointee = T; |
| 68 | + type Family = *mut (); |
| 69 | +} |
| 70 | +// SAFETY: `NonNull<T>` is a transparent newtype around a `*const T`. |
| 71 | +unsafe impl<T: ?Sized> RawPointer for NonNull<T> { |
| 72 | + type Pointee = T; |
| 73 | + type Family = NonNull<()>; |
| 74 | +} |
0 commit comments