diff options
| author | Scott McMurray <scottmcm@users.noreply.github.com> | 2024-04-21 16:11:01 -0700 |
|---|---|---|
| committer | Scott McMurray <scottmcm@users.noreply.github.com> | 2024-05-28 09:28:51 -0700 |
| commit | 459ce3f6bb5d8827835ed9b9f916eb189420b38a (patch) | |
| tree | dac17e010317dc8ca899361aa9d0904c3ce3eeb7 /library/core/src/ptr | |
| parent | 7717a306b2678ba9ece19b723c76a6b3a89ba931 (diff) | |
Add an intrinsic for `ptr::metadata`
Diffstat (limited to 'library/core/src/ptr')
| -rw-r--r-- | library/core/src/ptr/metadata.rs | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/library/core/src/ptr/metadata.rs b/library/core/src/ptr/metadata.rs index e501970b580..84287ec3f51 100644 --- a/library/core/src/ptr/metadata.rs +++ b/library/core/src/ptr/metadata.rs @@ -3,6 +3,8 @@ use crate::fmt; use crate::hash::{Hash, Hasher}; use crate::intrinsics::aggregate_raw_ptr; +#[cfg(not(bootstrap))] +use crate::intrinsics::ptr_metadata; use crate::marker::Freeze; /// Provides the pointer metadata type of any pointed-to type. @@ -94,10 +96,17 @@ pub trait Thin = Pointee<Metadata = ()>; #[rustc_const_unstable(feature = "ptr_metadata", issue = "81513")] #[inline] 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. - unsafe { PtrRepr { const_ptr: ptr }.components.metadata } + #[cfg(bootstrap)] + { + // 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. + unsafe { PtrRepr { const_ptr: ptr }.components.metadata } + } + #[cfg(not(bootstrap))] + { + ptr_metadata(ptr) + } } /// Forms a (possibly-wide) raw pointer from a data pointer and metadata. @@ -132,6 +141,7 @@ pub const fn from_raw_parts_mut<T: ?Sized>( } #[repr(C)] +#[cfg(bootstrap)] union PtrRepr<T: ?Sized> { const_ptr: *const T, mut_ptr: *mut T, @@ -139,15 +149,18 @@ union PtrRepr<T: ?Sized> { } #[repr(C)] +#[cfg(bootstrap)] struct PtrComponents<T: ?Sized> { data_pointer: *const (), metadata: <T as Pointee>::Metadata, } // Manual impl needed to avoid `T: Copy` bound. +#[cfg(bootstrap)] impl<T: ?Sized> Copy for PtrComponents<T> {} // Manual impl needed to avoid `T: Clone` bound. +#[cfg(bootstrap)] impl<T: ?Sized> Clone for PtrComponents<T> { fn clone(&self) -> Self { *self |
