3 ms·
Three Kinds of Polymorphism in Rust
- tristan123456 5y agoGood article! If you want the performance of option four with less hassle, have a look at the enum_dispatch crate. https://docs.rs/enum_dispatch/latest/enum_dispatch/ https://docs.rs/enum_dispatch/latest/enum_dispatch/
- deleted 5y ago[deleted]
- almostdeadguy 5y agoOne caveat to this is the "Enum with inner structs" version only avoids overhead when using individual shapes. The inner struct doesn't affect the enum size and alignment, so you still will have 12 bytes per shape in collections (8 for the largest shape variant's fields, 1 for the discriminant, and 3 for padding).
- brundolf 5y agoAh good point, I assume you’re talking about the table at the bottom. Technically that approach is still the most optimal because it gives you the option to choose your trade-offs at usage time instead of definition time, but I can see how the table is misleading on that bit. I’ll clarify it when I have the chance.