Skip to content

Commit 919cf42

Browse files
authored
Rollup merge of rust-lang#57992 - Matthias247:waker4, r=cramertj
Update the future/task API This change updates the future and task API as discussed in the stabilization RFC at rust-lang/rfcs#2592. Changes: - Replacing UnsafeWake with RawWaker and RawWakerVtable - Removal of LocalWaker - Removal of Arc-based Wake trait
2 parents aa896f3 + 871338c commit 919cf42

File tree

14 files changed

+239
-470
lines changed

14 files changed

+239
-470
lines changed

src/liballoc/boxed.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ use core::ops::{
7171
CoerceUnsized, DispatchFromDyn, Deref, DerefMut, Receiver, Generator, GeneratorState
7272
};
7373
use core::ptr::{self, NonNull, Unique};
74-
use core::task::{LocalWaker, Poll};
74+
use core::task::{Waker, Poll};
7575

7676
use crate::vec::Vec;
7777
use crate::raw_vec::RawVec;
@@ -896,7 +896,7 @@ impl<G: ?Sized + Generator> Generator for Pin<Box<G>> {
896896
impl<F: ?Sized + Future + Unpin> Future for Box<F> {
897897
type Output = F::Output;
898898

899-
fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<Self::Output> {
900-
F::poll(Pin::new(&mut *self), lw)
899+
fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll<Self::Output> {
900+
F::poll(Pin::new(&mut *self), waker)
901901
}
902902
}

src/liballoc/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,6 @@ mod macros;
132132

133133
pub mod alloc;
134134

135-
#[unstable(feature = "futures_api",
136-
reason = "futures in libcore are unstable",
137-
issue = "50547")]
138-
pub mod task;
139135
// Primitive types using the heaps above
140136

141137
// Need to conditionally define the mod from `boxed.rs` to avoid

src/liballoc/task.rs

-130
This file was deleted.

src/libcore/future/future.rs

+20-28
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use marker::Unpin;
66
use ops;
77
use pin::Pin;
8-
use task::{Poll, LocalWaker};
8+
use task::{Poll, Waker};
99

1010
/// A future represents an asynchronous computation.
1111
///
@@ -19,13 +19,14 @@ use task::{Poll, LocalWaker};
1919
/// final value. This method does not block if the value is not ready. Instead,
2020
/// the current task is scheduled to be woken up when it's possible to make
2121
/// further progress by `poll`ing again. The wake up is performed using
22-
/// `cx.waker()`, a handle for waking up the current task.
22+
/// the `waker` argument of the `poll()` method, which is a handle for waking
23+
/// up the current task.
2324
///
2425
/// When using a future, you generally won't call `poll` directly, but instead
2526
/// `await!` the value.
2627
#[must_use = "futures do nothing unless polled"]
2728
pub trait Future {
28-
/// The result of the `Future`.
29+
/// The type of value produced on completion.
2930
type Output;
3031

3132
/// Attempt to resolve the future to a final value, registering
@@ -42,16 +43,16 @@ pub trait Future {
4243
/// Once a future has finished, clients should not `poll` it again.
4344
///
4445
/// When a future is not ready yet, `poll` returns `Poll::Pending` and
45-
/// stores a clone of the [`LocalWaker`] to be woken once the future can
46+
/// stores a clone of the [`Waker`] to be woken once the future can
4647
/// make progress. For example, a future waiting for a socket to become
47-
/// readable would call `.clone()` on the [`LocalWaker`] and store it.
48+
/// readable would call `.clone()` on the [`Waker`] and store it.
4849
/// When a signal arrives elsewhere indicating that the socket is readable,
49-
/// `[LocalWaker::wake]` is called and the socket future's task is awoken.
50+
/// `[Waker::wake]` is called and the socket future's task is awoken.
5051
/// Once a task has been woken up, it should attempt to `poll` the future
5152
/// again, which may or may not produce a final value.
5253
///
5354
/// Note that on multiple calls to `poll`, only the most recent
54-
/// [`LocalWaker`] passed to `poll` should be scheduled to receive a
55+
/// [`Waker`] passed to `poll` should be scheduled to receive a
5556
/// wakeup.
5657
///
5758
/// # Runtime characteristics
@@ -67,44 +68,35 @@ pub trait Future {
6768
/// typically do *not* suffer the same problems of "all wakeups must poll
6869
/// all events"; they are more like `epoll(4)`.
6970
///
70-
/// An implementation of `poll` should strive to return quickly, and must
71-
/// *never* block. Returning quickly prevents unnecessarily clogging up
71+
/// An implementation of `poll` should strive to return quickly, and should
72+
/// not block. Returning quickly prevents unnecessarily clogging up
7273
/// threads or event loops. If it is known ahead of time that a call to
7374
/// `poll` may end up taking awhile, the work should be offloaded to a
7475
/// thread pool (or something similar) to ensure that `poll` can return
7576
/// quickly.
7677
///
77-
/// # [`LocalWaker`], [`Waker`] and thread-safety
78-
///
79-
/// The `poll` function takes a [`LocalWaker`], an object which knows how to
80-
/// awaken the current task. [`LocalWaker`] is not `Send` nor `Sync`, so in
81-
/// order to make thread-safe futures the [`LocalWaker::into_waker`] method
82-
/// should be used to convert the [`LocalWaker`] into a thread-safe version.
83-
/// [`LocalWaker::wake`] implementations have the ability to be more
84-
/// efficient, however, so when thread safety is not necessary,
85-
/// [`LocalWaker`] should be preferred.
78+
/// An implementation of `poll` may also never cause memory unsafety.
8679
///
8780
/// # Panics
8881
///
8982
/// Once a future has completed (returned `Ready` from `poll`),
9083
/// then any future calls to `poll` may panic, block forever, or otherwise
91-
/// cause bad behavior. The `Future` trait itself provides no guarantees
92-
/// about the behavior of `poll` after a future has completed.
84+
/// cause any kind of bad behavior expect causing memory unsafety.
85+
/// The `Future` trait itself provides no guarantees about the behavior
86+
/// of `poll` after a future has completed.
9387
///
9488
/// [`Poll::Pending`]: ../task/enum.Poll.html#variant.Pending
9589
/// [`Poll::Ready(val)`]: ../task/enum.Poll.html#variant.Ready
96-
/// [`LocalWaker`]: ../task/struct.LocalWaker.html
97-
/// [`LocalWaker::into_waker`]: ../task/struct.LocalWaker.html#method.into_waker
98-
/// [`LocalWaker::wake`]: ../task/struct.LocalWaker.html#method.wake
9990
/// [`Waker`]: ../task/struct.Waker.html
100-
fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<Self::Output>;
91+
/// [`Waker::wake`]: ../task/struct.Waker.html#method.wake
92+
fn poll(self: Pin<&mut Self>, waker: &Waker) -> Poll<Self::Output>;
10193
}
10294

10395
impl<'a, F: ?Sized + Future + Unpin> Future for &'a mut F {
10496
type Output = F::Output;
10597

106-
fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<Self::Output> {
107-
F::poll(Pin::new(&mut **self), lw)
98+
fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll<Self::Output> {
99+
F::poll(Pin::new(&mut **self), waker)
108100
}
109101
}
110102

@@ -115,7 +107,7 @@ where
115107
{
116108
type Output = <<P as ops::Deref>::Target as Future>::Output;
117109

118-
fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<Self::Output> {
119-
Pin::get_mut(self).as_mut().poll(lw)
110+
fn poll(self: Pin<&mut Self>, waker: &Waker) -> Poll<Self::Output> {
111+
Pin::get_mut(self).as_mut().poll(waker)
120112
}
121113
}

src/libcore/task/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ mod poll;
88
pub use self::poll::Poll;
99

1010
mod wake;
11-
pub use self::wake::{Waker, LocalWaker, UnsafeWake};
11+
pub use self::wake::{Waker, RawWaker, RawWakerVTable};

0 commit comments

Comments
 (0)