diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2020-03-23 19:04:43 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-23 19:04:43 +0100 |
| commit | e4d2f747e56e52cbd4ee90222fb455c6488ba683 (patch) | |
| tree | b2ffad434ce968422776356d812240ad49f56355 /src/libstd/sys/unix/stack_overflow.rs | |
| parent | 8549cfed4bbcdd28ef3a36e5de72c000e32f650e (diff) | |
| parent | 32f5724e8ac35e5a314313c6053ff46702223b27 (diff) | |
| download | rust-e4d2f747e56e52cbd4ee90222fb455c6488ba683.tar.gz rust-e4d2f747e56e52cbd4ee90222fb455c6488ba683.zip | |
Rollup merge of #68700 - withoutboats:wake-trait, r=withoutboats
Add Wake trait for safe construction of Wakers.
Currently, constructing a waker requires calling the unsafe `Waker::from_raw` API. This API requires the user to manually construct a vtable for the waker themself - which is both cumbersome and very error prone. This API would provide an ergonomic, straightforward and guaranteed memory-safe way of constructing a waker.
It has been our longstanding intention that the `Waker` type essentially function as an `Arc<dyn Wake>`, with a `Wake` trait as defined here. Two considerations prevented the original API from being shipped as simply an `Arc<dyn Wake>`:
- We want to support futures on embedded systems, which may not have an allocator, and in optimized executors for which this API may not be best-suited. Therefore, we have always explicitly supported the maximally-flexible (but also memory-unsafe) `RawWaker` API, and `Waker` has always lived in libcore.
- Because `Waker` lives in libcore and `Arc` lives in liballoc, it has not been feasible to provide a constructor for `Waker` from `Arc<dyn Wake>`.
Therefore, the Wake trait was left out of the initial version of the task waker API.
However, as Rust 1.41, it is possible under the more flexible orphan rules to implement `From<Arc<W>> for Waker where W: Wake` in liballoc. Therefore, we can now define this constructor even though `Waker` lives in libcore.
This PR adds these APIs:
- A `Wake` trait, which contains two methods
- A required method `wake`, which is called by `Waker::wake`
- A provided method `wake_by_ref`, which is called by `Waker::wake_by_ref` and which implementors can override if they can optimize this use case.
- An implementation of `From<Arc<W>> for Waker where W: Wake + Send + Sync + 'static`
- A similar implementation of `From<Arc<W>> for RawWaker`.
Diffstat (limited to 'src/libstd/sys/unix/stack_overflow.rs')
0 files changed, 0 insertions, 0 deletions
