6

Causing problems with Rust traits (then fixing them)

 2 years ago
source link: https://www.youtube.com/watch?v=sNyEgAGkDN8
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

18 Comments

Sort by
Add a public comment...
Thanks for the video! Great explanations and examples as always :D Honestly, I don't only use enum dispatch as a last resort for better performance but rather because I think it's often more straight forward to work with. The only thing to keep in mind when it comes to efficiency is that a fat enum is always at least as big as the largest inner type. So if there are few way larger variants that occur less often, it might make sense to box them. I think that's a neat way to add indirection very selectively.

Read more 2 weeks ago

Just this week I was annoyed at the need for boxing and dynamic dispatch in many cases, and was thinking about how to solve it.

I was actually considering essentially creating enum_dispatch.

Well, and some shit that's probably not possible or would break all kinds of security guarantees, a kind of dynamic dispatch on stack objects, where instead of the enum discriminator you'd have the vtable pointer, and after that it would behave like a enum, with enough size for the largest variant.

But since I likely don't have the knowledge to pull that off, even if it's possible without extending rustc, since this likely would not be faster than enum variants, and since this would cause issues when converting these values back to known types, I eventually came to the conclusion that simple enums with some kind of automatic trait implementation, probably macro based, for a common trait would be the best solution.

Thats where I was, in all likelyhood it wouldn't have gone further, but now I learn that OF COURSE someone already implemented it. Even if it is a bit of a downer, on the bright side, it means I choose a decent implementation approach, and I can use this crate without having to figure it out myself (which would've took a long time, or never happened).

So thanks for enlightening me!

Read more 2 weeks ago (edited)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK