jonhoo / rust-for-rustaceans.com

Source for https://rust-for-rustaceans.com/
https://rust-for-rustaceans.com/
152 stars 27 forks source link

Elaborating on why `Waker` has a vtable #44

Open joelposti opened 1 year ago

joelposti commented 1 year ago

On page 134 it is mentioned that a Waker has a manually implemented vtable (which functions similarly to the dynamic dispatch). I think a paragraph or an info box elaborating on why Waker has a vtable would be a beneficial addition to the book. With my limited knowledge I fail to see why a vtable is needed. Is static dispatch not possible?

jonhoo commented 1 year ago

Heh, you're not the first to ask. The main answer is that it can't be a trait object because the Clone trait (which Waker implements) isn't object safe. One could hack around this by always requiring Waker to be indirected through an Arc or a Box (which is possible in std today), but if that were required by the interface it wouldn't work in embedded/no_std contexts.

I agree it may be useful to add a brief note to this effect!

joelposti commented 1 year ago

Thank you for taking your time to explain this. Also, thanks for the link to the Reddit discussion.