Re-inventing trait objects
summary
Trait objects store a data pointer and a pointer to a static vtable that holds method pointers plus drop, size and alignment (five pointer-sized fields originally), keeping per-(type,trait) metadata out of object memory. Super-trait casts optimize by arranging a subtrait’s vtable to prefix its supertrait’s entries so identity casts work; multiple supertraits use extra virtual pointers for non-prefixes. Associated constants aren’t in vtables today, vtables are emitted eagerly, and the layout is verified against the compiler implementation.
AI-written summary · about
chapters (7)
- 0:00Introduction
- 1:21Memory layout constraints
- 5:40Advanced feature: supertraits
- 12:32Trait object upcasting
- 13:08Optimization techniques
- 13:21Final details and Q&A
- 20:37Closing remarks
related talks
22mTraits and You: A Deep Dive
56m“Type Traits - what are they and why should I use them?"
01h 00mTemplate Metaprogramming: Type Traits (part 1 of 2)
01h 01mThe promise of static reflection in C++26: Type Traits without compiler intrinsics
59mTemplate Metaprogramming: Type Traits (part 2 of 2)
33mReplacing #[cfg] with Traits: The Inlinable Dyn Extension Trait Pattern
01h 01mThe promise of static reflection in C++26: Type Traits without compiler intrinsics
36mRust's trait system is a proof engine, let's make it prove us an ABI!
