@@ -12,106 +12,10 @@ pub use self::option::FutureOption;
12
12
#[ cfg( feature = "either" ) ]
13
13
mod either;
14
14
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 ;
114
16
17
+ /// Will probably merge with futures_util::FutureExt
18
+ pub trait CoreFutureExt : Future {
115
19
/// A convenience for calling `Future::poll` on `Unpin` future types.
116
20
fn poll_unpin ( & mut self , cx : & mut task:: Context ) -> Poll < Self :: Output >
117
21
where Self : Unpin
@@ -120,50 +24,8 @@ pub trait Future {
120
24
}
121
25
}
122
26
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
+
167
29
/// A convenience for futures that return `Result` values that includes
168
30
/// a variety of adapters tailored to such futures.
169
31
pub trait TryFuture {
0 commit comments