I wonder why the metadata of the raw pointer in box is (), but the metadata of sized, which is stored on the stack, is the metadata of a slice ([T]), which is described as the number of items and has the type usize in Pointees documentation.
I have read the explanation on Pointee, and wrote the following code:
#![feature(box_as_ptr)]
#![feature(ptr_metadata)]
#![allow(unused)]
struct MySuperSliceable<T: ?Sized> {
info: u32,
data: T,
}
fn main() {
let sized: MySuperSliceable<[u8; 8]> = MySuperSliceable {
info: 17,
data: [0; 8],
};
let box_sized = Box::new(MySuperSliceable {
info: 18,
data: [0; 12],
});
let dynamic: &MySuperSliceable<[u8]> = &sized;
let box_dynamic = Box::as_ptr(&box_sized);
assert_eq!(std::ptr::metadata(dynamic), 8_usize);
assert_eq!(std::ptr::metadata("hello, world!"), 13_usize);
let a = 10;
assert_eq!(std::ptr::metadata(&a), ());
assert_eq!(std::ptr::metadata(box_dynamic), ());
assert_eq!(std::ptr::metadata(box_sized.as_ref()), ());
}
I think these assertions
assert_eq!(std::ptr::metadata(box_dynamic), ());
assert_eq!(std::ptr::metadata(box_sized.as_ref()), ());
should be
assert_eq!(std::ptr::metadata(box_dynamic), 12_usize);
assert_eq!(std::ptr::metadata(box_sized.as_ref()), 12_usize);
but it is wrong.
Moreover, I would appreciate it if someone could write an extra example to explain the metadata of DST (Dynamically Sized Types), I also confused about it.