Skip to content

Commit 6676c2d

Browse files
authored
Merge pull request #1034 from tinaun/0.3
port futures 0.3 to std futures
2 parents b581343 + adbe9f9 commit 6676c2d

File tree

17 files changed

+52
-910
lines changed

17 files changed

+52
-910
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ members = [
99
# "futures-macro-async",
1010
# "futures-macro-await",
1111
"futures-sink",
12-
# "futures-stable",
1312
"futures-util",
1413
]

futures-channel/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! This crate provides channels that can be used to communicate between
44
//! asynchronous tasks.
55
6-
#![feature(pin, arbitrary_self_types)]
6+
#![feature(pin, arbitrary_self_types, futures_api)]
77

88
#![deny(missing_docs, missing_debug_implementations)]
99
#![doc(html_root_url = "https://docs.rs/futures-channel/0.2.0")]

futures-core/src/executor.rs

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,4 @@
11
//! Executors.
22
3-
use task::TaskObj;
3+
pub use core::task::{Executor, SpawnErrorKind, SpawnObjError};
44

5-
/// A task executor.
6-
///
7-
/// A *task* is a `()`-producing future that runs at the top level, and will
8-
/// be `poll`ed until completion. It's also the unit at which wake-up
9-
/// notifications occur. Executors, such as thread pools, allow tasks to be
10-
/// spawned and are responsible for putting tasks onto ready queues when
11-
/// they are woken up, and polling them when they are ready.
12-
pub trait Executor {
13-
/// Spawn the given task object, polling it until completion.
14-
///
15-
/// # Errors
16-
///
17-
/// The executor may be unable to spawn tasks, either because it has
18-
/// been shut down or is resource-constrained.
19-
fn spawn_obj(&mut self, task: TaskObj) -> Result<(), SpawnObjError>;
20-
21-
/// Determine whether the executor is able to spawn new tasks.
22-
///
23-
/// # Returns
24-
///
25-
/// An `Ok` return means the executor is *likely* (but not guaranteed)
26-
/// to accept a subsequent spawn attempt. Likewise, an `Err` return
27-
/// means that `spawn` is likely, but not guaranteed, to yield an error.
28-
fn status(&self) -> Result<(), SpawnErrorKind> {
29-
Ok(())
30-
}
31-
32-
// TODO: downcasting hooks
33-
}
34-
35-
/// Provides the reason that an executor was unable to spawn.
36-
#[derive(Debug)]
37-
pub struct SpawnErrorKind {
38-
_a: ()
39-
}
40-
41-
impl SpawnErrorKind {
42-
/// Spawning is failing because the executor has been shut down.
43-
pub fn shutdown() -> SpawnErrorKind {
44-
SpawnErrorKind { _a: () }
45-
}
46-
47-
/// Check whether this error is the `shutdown` error.
48-
pub fn is_shutdown() -> bool {
49-
true
50-
}
51-
}
52-
53-
/// The result of a failed spawn
54-
#[derive(Debug)]
55-
pub struct SpawnObjError {
56-
/// The kind of error
57-
pub kind: SpawnErrorKind,
58-
59-
/// The task for which spawning was attempted
60-
pub task: TaskObj,
61-
}

futures-core/src/future/either.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
use {task, Future, Stream, Poll};
1+
use {task, Stream, Poll};
22

33
use core::mem::PinMut;
44

55
use either::Either;
66

7-
impl<A, B> Future for Either<A, B>
8-
where A: Future,
9-
B: Future<Output = A::Output>
10-
{
11-
type Output = A::Output;
7+
// impl<A, B> Future for Either<A, B>
8+
// where A: Future,
9+
// B: Future<Output = A::Output>
10+
// {
11+
// type Output = A::Output;
1212

13-
fn poll(self: PinMut<Self>, cx: &mut task::Context) -> Poll<A::Output> {
14-
unsafe {
15-
match PinMut::get_mut(self) {
16-
Either::Left(a) => PinMut::new_unchecked(a).poll(cx),
17-
Either::Right(b) => PinMut::new_unchecked(b).poll(cx),
18-
}
19-
}
20-
}
21-
}
13+
// fn poll(self: PinMut<Self>, cx: &mut task::Context) -> Poll<A::Output> {
14+
// unsafe {
15+
// match PinMut::get_mut(self) {
16+
// Either::Left(a) => PinMut::new_unchecked(a).poll(cx),
17+
// Either::Right(b) => PinMut::new_unchecked(b).poll(cx),
18+
// }
19+
// }
20+
// }
21+
// }
2222

2323
impl<A, B> Stream for Either<A, B>
2424
where A: Stream,

futures-core/src/future/mod.rs

Lines changed: 5 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -12,106 +12,10 @@ pub use self::option::FutureOption;
1212
#[cfg(feature = "either")]
1313
mod either;
1414

15-
/// A future represents an asychronous computation.
16-
///
17-
/// A future is a value that may not have finished computing yet. This kind of
18-
/// "asynchronous value" makes it possible for a thread to continue doing useful
19-
/// work while it waits for the value to become available.
20-
///
21-
/// The ergonomics and implementation of the `Future` trait are very similar to
22-
/// the `Iterator` trait in that there is just one method you need to
23-
/// implement, but you get a whole lot of others for free as a result. These
24-
/// other methods allow you to chain together large computations based on
25-
/// futures, which will automatically handle asynchrony for you.
26-
///
27-
/// # The `poll` method
28-
///
29-
/// The core method of future, `poll`, *attempts* to resolve the future into a
30-
/// final value. This method does not block if the value is not ready. Instead,
31-
/// the current task is scheduled to be woken up when it's possible to make
32-
/// further progress by `poll`ing again. The wake up is performed using
33-
/// `cx.waker()`, a handle for waking up the current task.
34-
///
35-
/// When using a future, you generally won't call `poll` directly, but instead
36-
/// use combinators to build up asynchronous computations. A complete
37-
/// computation can then be spawned onto an
38-
/// [executor](../futures_core/executor/trait.Executor.html) as a new, independent
39-
/// task that will automatically be `poll`ed to completion.
40-
///
41-
/// # Combinators
42-
///
43-
/// Like iterators, futures provide a large number of combinators to work with
44-
/// futures to express computations in a much more natural method than
45-
/// scheduling a number of callbacks. As with iterators, the combinators are
46-
/// zero-cost: they compile away. You can find the combinators in the
47-
/// [future-util](https://docs.rs/futures-util) crate.
48-
pub trait Future {
49-
/// The result of the future
50-
type Output;
51-
52-
/// Attempt to resolve the future to a final value, registering
53-
/// the current task for wakeup if the value is not yet available.
54-
///
55-
/// # Return value
56-
///
57-
/// This function returns:
58-
///
59-
/// - `Poll::Pending` if the future is not ready yet
60-
/// - `Poll::Ready(val)` with the result `val` of this future if it finished
61-
/// successfully.
62-
///
63-
/// Once a future has finished, clients should not `poll` it again.
64-
///
65-
/// When a future is not ready yet, `poll` returns
66-
/// [`Poll::Pending`](::Poll). The future will *also* register the
67-
/// interest of the current task in the value being produced. For example,
68-
/// if the future represents the availability of data on a socket, then the
69-
/// task is recorded so that when data arrives, it is woken up (via
70-
/// [`cx.waker()`](::task::Context::waker). Once a task has been woken up,
71-
/// it should attempt to `poll` the future again, which may or may not
72-
/// produce a final value.
73-
///
74-
/// Note that if `Pending` is returned it only means that the *current* task
75-
/// (represented by the argument `cx`) will receive a notification. Tasks
76-
/// from previous calls to `poll` will *not* receive notifications.
77-
///
78-
/// # Runtime characteristics
79-
///
80-
/// Futures alone are *inert*; they must be *actively* `poll`ed to make
81-
/// progress, meaning that each time the current task is woken up, it should
82-
/// actively re-`poll` pending futures that it still has an interest in.
83-
/// Usually this is done by building up a large computation as a single
84-
/// future (using combinators), then spawning that future as a *task* onto
85-
/// an [executor](../futures_core/executor/trait.Executor.html). Executors
86-
/// ensure that each task is `poll`ed every time a future internal to that
87-
/// task is ready to make progress.
88-
///
89-
/// The `poll` function is not called repeatedly in a tight loop for
90-
/// futures, but only whenever the future itself is ready, as signaled via
91-
/// [`cx.waker()`](::task::Context::waker). If you're familiar with the
92-
/// `poll(2)` or `select(2)` syscalls on Unix it's worth noting that futures
93-
/// typically do *not* suffer the same problems of "all wakeups must poll
94-
/// all events"; they are more like `epoll(4)`.
95-
///
96-
/// An implementation of `poll` should strive to return quickly, and must
97-
/// *never* block. Returning quickly prevents unnecessarily clogging up
98-
/// threads or event loops. If it is known ahead of time that a call to
99-
/// `poll` may end up taking awhile, the work should be offloaded to a
100-
/// thread pool (or something similar) to ensure that `poll` can return
101-
/// quickly.
102-
///
103-
/// # Panics
104-
///
105-
/// Once a future has completed (returned `Ready` from `poll`),
106-
/// then any future calls to `poll` may panic, block forever, or otherwise
107-
/// cause bad behavior. The `Future` trait itself provides no guarantees
108-
/// about the behavior of `poll` after a future has completed.
109-
///
110-
/// Callers who may call `poll` too many times may want to consider using
111-
/// the `fuse` adaptor which defines the behavior of `poll`, but comes with
112-
/// a little bit of extra cost.
113-
fn poll(self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output>;
15+
pub use core::future::Future;
11416

17+
/// Will probably merge with futures_util::FutureExt
18+
pub trait CoreFutureExt: Future {
11519
/// A convenience for calling `Future::poll` on `Unpin` future types.
11620
fn poll_unpin(&mut self, cx: &mut task::Context) -> Poll<Self::Output>
11721
where Self: Unpin
@@ -120,50 +24,8 @@ pub trait Future {
12024
}
12125
}
12226

123-
impl<'a, F: ?Sized + Future + Unpin> Future for &'a mut F {
124-
type Output = F::Output;
125-
126-
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
127-
F::poll(PinMut::new(&mut **self), cx)
128-
}
129-
}
130-
131-
impl<'a, F: ?Sized + Future> Future for PinMut<'a, F> {
132-
type Output = F::Output;
133-
134-
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
135-
F::poll((*self).reborrow(), cx)
136-
}
137-
}
138-
139-
if_std! {
140-
use std::boxed::{Box, PinBox};
141-
142-
impl<'a, F: ?Sized + Future + Unpin> Future for Box<F> {
143-
type Output = F::Output;
144-
145-
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
146-
(**self).poll_unpin(cx)
147-
}
148-
}
149-
150-
impl<'a, F: ?Sized + Future> Future for PinBox<F> {
151-
type Output = F::Output;
152-
153-
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
154-
self.as_pin_mut().poll(cx)
155-
}
156-
}
157-
158-
impl<'a, F: Future> Future for ::std::panic::AssertUnwindSafe<F> {
159-
type Output = F::Output;
160-
161-
fn poll(mut self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
162-
unsafe { pinned_field!(self, 0).poll(cx) }
163-
}
164-
}
165-
}
166-
27+
impl<T: ?Sized> CoreFutureExt for T where T: Future {}
28+
16729
/// A convenience for futures that return `Result` values that includes
16830
/// a variety of adapters tailored to such futures.
16931
pub trait TryFuture {

futures-core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Core traits and types for asynchronous operations in Rust.
22
33
#![feature(pin, arbitrary_self_types)]
4+
#![feature(futures_api)]
45

56
#![no_std]
67
#![deny(missing_docs, missing_debug_implementations, warnings)]

futures-core/src/poll.rs

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2+
pub use core::task::Poll;
3+
14
/// A macro for extracting the successful type of a `Poll<Result<T, E>>`.
25
///
36
/// This macro bakes in propagation of `Pending` and `Err` signals by returning early.
@@ -38,73 +41,3 @@ macro_rules! ready {
3841
$crate::Poll::Pending => return $crate::Poll::Pending,
3942
})
4043
}
41-
42-
/// Indicates whether a value is available, or if the current task has been
43-
/// scheduled for later wake-up instead.
44-
#[derive(Copy, Clone, Debug, PartialEq)]
45-
pub enum Poll<T> {
46-
/// Represents that a value is immediately ready.
47-
Ready(T),
48-
49-
/// Represents that a value is not ready yet.
50-
///
51-
/// When a function returns `Pending`, the function *must* also
52-
/// ensure that the current task is scheduled to be awoken when
53-
/// progress can be made.
54-
Pending,
55-
}
56-
57-
impl<T> Poll<T> {
58-
/// Change the ready value of this `Poll` with the closure provided
59-
pub fn map<U, F>(self, f: F) -> Poll<U>
60-
where F: FnOnce(T) -> U
61-
{
62-
match self {
63-
Poll::Ready(t) => Poll::Ready(f(t)),
64-
Poll::Pending => Poll::Pending,
65-
}
66-
}
67-
68-
/// Returns whether this is `Poll::Ready`
69-
pub fn is_ready(&self) -> bool {
70-
match *self {
71-
Poll::Ready(_) => true,
72-
Poll::Pending => false,
73-
}
74-
}
75-
76-
/// Returns whether this is `Poll::Pending`
77-
pub fn is_pending(&self) -> bool {
78-
!self.is_ready()
79-
}
80-
}
81-
82-
impl<T, E> Poll<Result<T, E>> {
83-
/// Change the success value of this `Poll` with the closure provided
84-
pub fn map_ok<U, F>(self, f: F) -> Poll<Result<U, E>>
85-
where F: FnOnce(T) -> U
86-
{
87-
match self {
88-
Poll::Ready(Ok(t)) => Poll::Ready(Ok(f(t))),
89-
Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
90-
Poll::Pending => Poll::Pending,
91-
}
92-
}
93-
94-
/// Change the error value of this `Poll` with the closure provided
95-
pub fn map_err<U, F>(self, f: F) -> Poll<Result<T, U>>
96-
where F: FnOnce(E) -> U
97-
{
98-
match self {
99-
Poll::Ready(Ok(t)) => Poll::Ready(Ok(t)),
100-
Poll::Ready(Err(e)) => Poll::Ready(Err(f(e))),
101-
Poll::Pending => Poll::Pending,
102-
}
103-
}
104-
}
105-
106-
impl<T> From<T> for Poll<T> {
107-
fn from(t: T) -> Poll<T> {
108-
Poll::Ready(t)
109-
}
110-
}

0 commit comments

Comments
 (0)