Replacing #[cfg] with Traits: The Inlinable Dyn Extension Trait Pattern
Talkat RustConf 2026 · on Wednesday, 2026-09-09· at 11:55· Room 510ac | Palais des Congrès de Montréal, Palais des Congrès de Montréal·
summary
Compares six concrete implementations (cfgs, sidecar flags, option-args, function-pointer tables, trait-backed tables, plus a try_as_dyn variant) and shows the inlinable dyn extension trait pattern used for ~5–6 years in gdbstub with reproducible benchmarks and assembly analysis; rustc optimizes to zero-cost via dead-code-elimination and devirtualization so dyn-backed extension traits compile away in release, yielding compile-time capability checks while keeping runtime dynamism, though a remaining pitfall is forgetting to wire an impl and try_as_dyn is still a nightly, shifting feature.
AI-written summary · about
chapters (13)
- 0:00Introduction and motivation
- 1:56Target controller libraries explained
- 5:21Architectural pressures in protocol libraries
- 8:46Six ways to implement the target trait
- 10:37First approach using cfg gates
- 13:14Limitations of cfg-based approach
- 13:37Sidecar methods for capability reporting
- 16:33Option-based handler sniffing
- 18:57Function pointer tables approach
- 23:01Inlinable dyn extension trait pattern
- 26:44Transferability to other domains
- 28:12Nightly try_as_dyn feature
- 30:33Q&A session
related talks
58mRust Traits In C++
25mType System Tips for the Real World
15mSupercharging Your Code With Five Little-Known Attributes
36mRust's trait system is a proof engine, let's make it prove us an ABI!
36mThe language that isn't
30mTrials, traits, and tribulations
22mTraits and You: A Deep Dive
27mRe-inventing trait objects
