jonhoo / rust-for-rustaceans.com

Source for https://rust-for-rustaceans.com/

Home Page:https://rust-for-rustaceans.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Elaborating on why `Waker` has a vtable

joelposti opened this issue · comments

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?

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!

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