@@ -180,10 +180,20 @@ pub struct DynMetadata<Dyn: ?Sized> {
180
180
phantom : crate :: marker:: PhantomData < Dyn > ,
181
181
}
182
182
183
+ /// Opaque type for accessing vtables.
184
+ ///
185
+ /// Private implementation detail of `DynMetadata::size_of` etc.
186
+ /// Must be zero-sized since there is conceptually not actually any Abstract Machine memory behind this pointer.
187
+ /// However, we can require pointer alignment.
188
+ #[ repr( C ) ]
189
+ #[ cfg( not( bootstrap) ) ]
190
+ struct VTable ( [ usize ; 0 ] ) ;
191
+
183
192
/// The common prefix of all vtables. It is followed by function pointers for trait methods.
184
193
///
185
194
/// Private implementation detail of `DynMetadata::size_of` etc.
186
195
#[ repr( C ) ]
196
+ #[ cfg( bootstrap) ]
187
197
struct VTable {
188
198
drop_in_place : fn ( * mut ( ) ) ,
189
199
size_of : usize ,
@@ -194,13 +204,25 @@ impl<Dyn: ?Sized> DynMetadata<Dyn> {
194
204
/// Returns the size of the type associated with this vtable.
195
205
#[ inline]
196
206
pub fn size_of ( self ) -> usize {
197
- self . vtable_ptr . size_of
207
+ #[ cfg( bootstrap) ]
208
+ return self . vtable_ptr . size_of ;
209
+ #[ cfg( not( bootstrap) ) ]
210
+ // SAFETY: DynMetadata always contains a valid vtable pointer
211
+ return unsafe {
212
+ crate :: intrinsics:: vtable_size ( self . vtable_ptr as * const VTable as * const ( ) )
213
+ } ;
198
214
}
199
215
200
216
/// Returns the alignment of the type associated with this vtable.
201
217
#[ inline]
202
218
pub fn align_of ( self ) -> usize {
203
- self . vtable_ptr . align_of
219
+ #[ cfg( bootstrap) ]
220
+ return self . vtable_ptr . align_of ;
221
+ #[ cfg( not( bootstrap) ) ]
222
+ // SAFETY: DynMetadata always contains a valid vtable pointer
223
+ return unsafe {
224
+ crate :: intrinsics:: vtable_align ( self . vtable_ptr as * const VTable as * const ( ) )
225
+ } ;
204
226
}
205
227
206
228
/// Returns the size and alignment together as a `Layout`
0 commit comments