Easy Waker API

It was always our intent to add these APIs to std:

trait Wake {
    fn wake(self: Arc<Self>);

    fn wake_by_ref(self: &Arc<Self>) {
        self.clone().wake()
    }
}

impl Waker {
    pub fn new<W: Wake>(waker: Arc<W>) -> Waker { ... }
}

However, this is incompatible with the current core/alloc/std set-up: Wake must be in alloc or std, because it references Arc, but Waker::new must be defined in core, and it references Arc. This is an example of where using cargo features and conditional compilation would improve std's APIs.

But it occurred to me this week that this impl could be defined in alloc:

impl<W: Wake> From<Arc<W>> for Waker {
    fn from(waker: Arc<W>) -> Waker  { ... }
}

I am not driven to spearhead this at the moment, but someone could add a task module to alloc which contains the Wake trait and this impl, and then for the common case of wakers which are arcs, the user could write:

struct MyWaker {
    ...
}

impl Wake for MyWaker {
    fn wake(self: Arc<Self>) { ... }
}

Waker::from(Arc::new(MyWaker { ... }))

This API is not only convenient for this common case it is also 100% safe (unlike the current RawWaker based API).

cc @anon19897381

13 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.