Hacker News new | past | comments | ask | show | jobs | submit login
Three Kinds of Polymorphism in Rust (brandons.me)
34 points by brundolf on Jan 6, 2022 | hide | past | favorite | 3 comments



Good 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/


One 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).


Ah 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.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: