What's the metadata of Box<dyn Fn()>

println!("{}",std::mem::size_of::<Box<dyn Fn()>>());
// prints 16 by 64bit ptr

What's the metadata for description?

If you have a reference to a trait object you get a fat pointer consisting of (I) a pointer to the underlying object and (II) a pointer to the vtable of the trait and its supertraits where each method points to the implementation of your concrete type. From the reference:

Due to the opaqueness of which concrete type the value is of, trait objects are dynamically sized types. Like all DSTs, trait objects are used behind some type of pointer; for example &dyn SomeTrait or Box<dyn SomeTrait>. Each instance of a pointer to a trait object includes:

  • a pointer to an instance of a type T that implements SomeTrait
  • a virtual method table, often just called a vtable, which contains, for each method of SomeTrait and its supertraits that T implements, a pointer to T's implementation (i.e. a function pointer).
1 Like

Thanks for your patience! This is helpful

1 Like