|
3 | 3 | use crate::fmt;
|
4 | 4 | use crate::hash::{Hash, Hasher};
|
5 | 5 | use crate::intrinsics::aggregate_raw_ptr;
|
| 6 | +#[cfg(not(bootstrap))] |
| 7 | +use crate::intrinsics::ptr_metadata; |
6 | 8 | use crate::marker::Freeze;
|
7 | 9 |
|
8 | 10 | /// Provides the pointer metadata type of any pointed-to type.
|
@@ -94,10 +96,17 @@ pub trait Thin = Pointee<Metadata = ()>;
|
94 | 96 | #[rustc_const_unstable(feature = "ptr_metadata", issue = "81513")]
|
95 | 97 | #[inline]
|
96 | 98 | pub const fn metadata<T: ?Sized>(ptr: *const T) -> <T as Pointee>::Metadata {
|
97 |
| - // SAFETY: Accessing the value from the `PtrRepr` union is safe since *const T |
98 |
| - // and PtrComponents<T> have the same memory layouts. Only std can make this |
99 |
| - // guarantee. |
100 |
| - unsafe { PtrRepr { const_ptr: ptr }.components.metadata } |
| 99 | + #[cfg(bootstrap)] |
| 100 | + { |
| 101 | + // SAFETY: Accessing the value from the `PtrRepr` union is safe since *const T |
| 102 | + // and PtrComponents<T> have the same memory layouts. Only std can make this |
| 103 | + // guarantee. |
| 104 | + unsafe { PtrRepr { const_ptr: ptr }.components.metadata } |
| 105 | + } |
| 106 | + #[cfg(not(bootstrap))] |
| 107 | + { |
| 108 | + ptr_metadata(ptr) |
| 109 | + } |
101 | 110 | }
|
102 | 111 |
|
103 | 112 | /// Forms a (possibly-wide) raw pointer from a data pointer and metadata.
|
@@ -132,22 +141,26 @@ pub const fn from_raw_parts_mut<T: ?Sized>(
|
132 | 141 | }
|
133 | 142 |
|
134 | 143 | #[repr(C)]
|
| 144 | +#[cfg(bootstrap)] |
135 | 145 | union PtrRepr<T: ?Sized> {
|
136 | 146 | const_ptr: *const T,
|
137 | 147 | mut_ptr: *mut T,
|
138 | 148 | components: PtrComponents<T>,
|
139 | 149 | }
|
140 | 150 |
|
141 | 151 | #[repr(C)]
|
| 152 | +#[cfg(bootstrap)] |
142 | 153 | struct PtrComponents<T: ?Sized> {
|
143 | 154 | data_pointer: *const (),
|
144 | 155 | metadata: <T as Pointee>::Metadata,
|
145 | 156 | }
|
146 | 157 |
|
147 | 158 | // Manual impl needed to avoid `T: Copy` bound.
|
| 159 | +#[cfg(bootstrap)] |
148 | 160 | impl<T: ?Sized> Copy for PtrComponents<T> {}
|
149 | 161 |
|
150 | 162 | // Manual impl needed to avoid `T: Clone` bound.
|
| 163 | +#[cfg(bootstrap)] |
151 | 164 | impl<T: ?Sized> Clone for PtrComponents<T> {
|
152 | 165 | fn clone(&self) -> Self {
|
153 | 166 | *self
|
|
0 commit comments