Generics and Compile-Time in Rust
There are plenty of Rust features I&#x27;d love to see finalized, like GAT ( HKT, sort of), generators, async trait methods, custom test frameworks, ...<p>But there is an area that could have a big impact on certain (mostly higher level) domains, yet doesn&#x27;t seem to get much attention: better trait objects.<p>They are severely limited in a few aspects:<p>* only a single trait&#x2F;vtable<p>* casting is only available with Any and you can&#x27;t cast between different traits, requiring really awkward super-traits with manual conversion methods or hacks like mopa [1]<p>* object safety rules are cumbersome and prevent certain important traits like Clone to be available, leading to clone_boxed, clone_arc everywhere, or proc macro solutions like dyn-clone [2]<p>* ...<p>Doing anything more fancy with them usually feels annoying. Therefore the standard library and entire ecosystem strongly favor generics and monomorphization.<p>This is generally fine and has worked out well for the language, but there are plenty of use cases where more advanced trait objects could reduce code size and compile times with very small impact on performance, while also enabling some interesting new patterns.<p>I realize there are plenty of implementation challenges that make work in this area far from trivial in the current language, but it&#x27;s frustrating to miss out on part of a toolbox.<p>I think Swift is an interesting comparsion. The languages are similar in quite a few aspects, but Swift often prioritizes small code size and dynamic dispatch over monomorphization. Compile times aren&#x27;t that great either, though...<p>ps: it is briefly mentioned in the post, but switching to LLD has provided noticeable build time improvements on most of the binary crates I am working on.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;chris-morgan&#x2F;mopa" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;chris-morgan&#x2F;mopa</a>
[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;dtolnay&#x2F;dyn-clone" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;dtolnay&#x2F;dyn-clone</a>