4 ms·
Isn’t it possible to just use trait objects for this? You can have default implementation for some methods and implementors of the trait can specialise. Speci
by ozgrakkurt 1y ago
Isn’t it possible to just use trait objects for this?
You can have default implementation for some methods and implementors of the trait can specialise.
Specifically, you can have couple functions that takes a type that implements some stuff (different based on the wanted specialisation) and gives you a trait object.
This should be simpler and should have better compile time than doing macros or adding more generics
- selfmodruntime 1y agoThe length Rust programmers will go to avoid writing `Box<dyn Trait>` is astonishing. The heap is there for a reason :)
- alfiedotwtf 1y agoI’m guilty of this… I’ve doing using Rust for 10 years, and I’ve only ever used `Box<dyn>` maybe 4 times all up. .. yep, I’ll try everything before introducing virtual functions just out of warm fuzzies rather than anything measurable
- ccleve 1y agoThe performance hit you take for doing dynamic dispatch is real and measurable. It's a no-go if you're in a performance sensitive part of your app.
- selfmodruntime 1y agoI write rust for no_std embedded devices and I just use stack_dst for this :) Of course you‘re right if you can‘t alloc, you can‘t use Box or similar stuff. Also you can avoid a lot of performance hits if you just implement most stuff on the dyn Trait directly!
- surajrmal 1y agoSure but most code paths are not hot. This is definitely premature optimization at the expense of everything else.