[−][src]Trait wakeful::Wake
Zero-cost helper trait that makes it easier to implement wakers.
Implementing this trait provides you with Wake::into_waker
, which allows
you to construct a Waker
from any type implementing Wake
. The only
method you must implement is Wake::wake_by_ref
which can encapsulate all
your custom wake-up behavior.
Your custom wakers must also implement Clone
, Send
, and Sync
to
comply with the contract of Waker
. You are free to choose any strategy
you like to handle cloning; bundling your state in an inner Arc
is
common and plays nicely with this trait.
Provided implementations
A simple waker implementation is provided for std::thread::Thread
, which
merely calls unpark()
. This almost trivializes implementing your own
single-threaded block_on
executor. An example of this is provided in the
examples/
directory.
Optimizations
If the size of Self
is less than or equal to pointer size, as an
optimization the underlying implementation will pass self
in directly to
RawWakerVTable
functions. For types larger than a pointer, an allocation
will be made on creation and when cloning.
Examples
use wakeful::Wake; /// Doesn't actually do anything except print a message when wake is called. #[derive(Clone)] struct PrintWaker; impl Wake for PrintWaker { fn wake_by_ref(&self) { println!("wake called!"); } } let waker = PrintWaker.into_waker(); waker.wake(); // prints "wake called!"
use std::task::Waker; use wakeful::Wake; /// Delegates wake calls to multiple wakers. #[derive(Clone)] struct MultiWaker(Vec<Waker>); impl Wake for MultiWaker { fn wake(self) { for waker in self.0 { waker.wake(); } } fn wake_by_ref(&self) { for waker in &self.0 { waker.wake_by_ref(); } } }
Required methods
fn wake_by_ref(&self)
Wake up the task associated with this waker, consuming the waker. When
converted into a waker handle, this method is invoked whenever
Waker::wake_by_ref
is called.
Provided methods
fn wake(self)
Wake up the task associated with this waker, consuming the waker. When
converted into a waker handle, this method is invoked whenever
Waker::wake
is called.
By default, this delegates to Wake::wake_by_ref
, but can be
overridden if a more efficient owned implementation is possible.
fn into_waker(self) -> Waker
Convert this into a Waker
handle.