1

When building async futures, it's common to implement the ArcWake trait in wakers. I understand this is done to avoid writing boilerplate, creating vtables and writing unsafe code (according to the tokio tutorial).

Now, (and this feels like a silly question) what I don't understand is how does implementing a trait avoid writing that other code? When I check the source of the trait, it contains practically nothing: only the declaration of one small function that needs to be implemented to fulfil the trait and the instantiation of another function that calls that one. So, where are all those things we're avoiding writing coming from? Where's the boilerplate, vtables, unsafe code and all that and how does it get executed just because we implemented a trait on a waker?

jgpaiva
  • 369
  • 2
  • 11
  • 1
    The magic you're looking for is in other files in the same repo, e.g. [here](https://github.com/rust-lang/futures-rs/blob/master/futures-task/src/waker.rs). – user4815162342 Aug 31 '21 at 10:32

1 Answers1

1

OK, I think I got it now. The code I couldn't find is here (thanks for the comment on the question that pointed this out!). That code is all called on the tutorial when the future is polled, specifically on the line

 let waker = task::waker(self.clone());

On this line, we're obtaining a waker from an ArcWake by calling the task::waker function which deals with all the complexity for us.

jgpaiva
  • 369
  • 2
  • 11