Skip to content

Commit

Permalink
Update the future/task API
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Matthias247 committed Feb 3, 2019
1 parent 4f4f4a4 commit d9a4b22
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 411 deletions.
6 changes: 3 additions & 3 deletions src/liballoc/boxed.rs
Expand Up @@ -71,7 +71,7 @@ use core::ops::{
CoerceUnsized, DispatchFromDyn, Deref, DerefMut, Receiver, Generator, GeneratorState
};
use core::ptr::{self, NonNull, Unique};
use core::task::{LocalWaker, Poll};
use core::task::{Waker, Poll};

use crate::vec::Vec;
use crate::raw_vec::RawVec;
Expand Down Expand Up @@ -896,7 +896,7 @@ impl<G: ?Sized + Generator> Generator for Pin<Box<G>> {
impl<F: ?Sized + Future + Unpin> Future for Box<F> {
type Output = F::Output;

fn poll(mut self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<Self::Output> {
F::poll(Pin::new(&mut *self), lw)
fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll<Self::Output> {
F::poll(Pin::new(&mut *self), waker)
}
}
4 changes: 0 additions & 4 deletions src/liballoc/lib.rs
Expand Up @@ -133,10 +133,6 @@ mod macros;

pub mod alloc;

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

// Need to conditionally define the mod from `boxed.rs` to avoid
Expand Down
130 changes: 0 additions & 130 deletions src/liballoc/task.rs

This file was deleted.

36 changes: 12 additions & 24 deletions src/libcore/future/future.rs
Expand Up @@ -5,7 +5,7 @@
use marker::Unpin;
use ops;
use pin::Pin;
use task::{Poll, LocalWaker};
use task::{Poll, Waker};

/// A future represents an asynchronous computation.
///
Expand All @@ -25,7 +25,7 @@ use task::{Poll, LocalWaker};
/// `await!` the value.
#[must_use = "futures do nothing unless polled"]
pub trait Future {
/// The result of the `Future`.
/// The type of value produced on completion.
type Output;

/// Attempt to resolve the future to a final value, registering
Expand All @@ -42,16 +42,16 @@ pub trait Future {
/// Once a future has finished, clients should not `poll` it again.
///
/// When a future is not ready yet, `poll` returns `Poll::Pending` and
/// stores a clone of the [`LocalWaker`] to be woken once the future can
/// stores a clone of the [`Waker`] to be woken once the future can
/// make progress. For example, a future waiting for a socket to become
/// readable would call `.clone()` on the [`LocalWaker`] and store it.
/// readable would call `.clone()` on the [`Waker`] and store it.
/// When a signal arrives elsewhere indicating that the socket is readable,
/// `[LocalWaker::wake]` is called and the socket future's task is awoken.
/// `[Waker::wake]` is called and the socket future's task is awoken.
/// Once a task has been woken up, it should attempt to `poll` the future
/// again, which may or may not produce a final value.
///
/// Note that on multiple calls to `poll`, only the most recent
/// [`LocalWaker`] passed to `poll` should be scheduled to receive a
/// [`Waker`] passed to `poll` should be scheduled to receive a
/// wakeup.
///
/// # Runtime characteristics
Expand All @@ -74,16 +74,6 @@ pub trait Future {
/// thread pool (or something similar) to ensure that `poll` can return
/// quickly.
///
/// # [`LocalWaker`], [`Waker`] and thread-safety
///
/// The `poll` function takes a [`LocalWaker`], an object which knows how to
/// awaken the current task. [`LocalWaker`] is not `Send` nor `Sync`, so in
/// order to make thread-safe futures the [`LocalWaker::into_waker`] method
/// should be used to convert the [`LocalWaker`] into a thread-safe version.
/// [`LocalWaker::wake`] implementations have the ability to be more
/// efficient, however, so when thread safety is not necessary,
/// [`LocalWaker`] should be preferred.
///
/// # Panics
///
/// Once a future has completed (returned `Ready` from `poll`),
Expand All @@ -93,18 +83,16 @@ pub trait Future {
///
/// [`Poll::Pending`]: ../task/enum.Poll.html#variant.Pending
/// [`Poll::Ready(val)`]: ../task/enum.Poll.html#variant.Ready
/// [`LocalWaker`]: ../task/struct.LocalWaker.html
/// [`LocalWaker::into_waker`]: ../task/struct.LocalWaker.html#method.into_waker
/// [`LocalWaker::wake`]: ../task/struct.LocalWaker.html#method.wake
/// [`Waker`]: ../task/struct.Waker.html
fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<Self::Output>;
/// [`Waker::wake`]: ../task/struct.Waker.html#method.wake
fn poll(self: Pin<&mut Self>, waker: &Waker) -> Poll<Self::Output>;
}

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

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

Expand All @@ -115,7 +103,7 @@ where
{
type Output = <<P as ops::Deref>::Target as Future>::Output;

fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<Self::Output> {
Pin::get_mut(self).as_mut().poll(lw)
fn poll(self: Pin<&mut Self>, waker: &Waker) -> Poll<Self::Output> {
Pin::get_mut(self).as_mut().poll(waker)
}
}
2 changes: 1 addition & 1 deletion src/libcore/task/mod.rs
Expand Up @@ -8,4 +8,4 @@ mod poll;
pub use self::poll::Poll;

mod wake;
pub use self::wake::{Waker, LocalWaker, UnsafeWake};
pub use self::wake::{Waker, RawWaker, RawWakerVTable};

0 comments on commit d9a4b22

Please sign in to comment.