1 #![stable(feature = "futures_api", since = "1.36.0")]
3 use crate::marker::Unpin;
6 use crate::task::{Context, Poll};
8 /// A future represents an asynchronous computation.
10 /// A future is a value that may not have finished computing yet. This kind of
11 /// "asynchronous value" makes it possible for a thread to continue doing useful
12 /// work while it waits for the value to become available.
14 /// # The `poll` method
16 /// The core method of future, `poll`, *attempts* to resolve the future into a
17 /// final value. This method does not block if the value is not ready. Instead,
18 /// the current task is scheduled to be woken up when it's possible to make
19 /// further progress by `poll`ing again. The `context` passed to the `poll`
20 /// method can provide a [`Waker`], which is a handle for waking up the current
23 /// When using a future, you generally won't call `poll` directly, but instead
24 /// `.await` the value.
26 /// [`Waker`]: ../task/struct.Waker.html
27 #[must_use = "futures do nothing unless you `.await` or poll them"]
28 #[stable(feature = "futures_api", since = "1.36.0")]
29 #[lang = "future_trait"]
31 /// The type of value produced on completion.
32 #[stable(feature = "futures_api", since = "1.36.0")]
35 /// Attempt to resolve the future to a final value, registering
36 /// the current task for wakeup if the value is not yet available.
40 /// This function returns:
42 /// - [`Poll::Pending`] if the future is not ready yet
43 /// - [`Poll::Ready(val)`] with the result `val` of this future if it
44 /// finished successfully.
46 /// Once a future has finished, clients should not `poll` it again.
48 /// When a future is not ready yet, `poll` returns `Poll::Pending` and
49 /// stores a clone of the [`Waker`] copied from the current [`Context`].
50 /// This [`Waker`] is then woken once the future can make progress.
51 /// For example, a future waiting for a socket to become
52 /// readable would call `.clone()` on the [`Waker`] and store it.
53 /// When a signal arrives elsewhere indicating that the socket is readable,
54 /// [`Waker::wake`] is called and the socket future's task is awoken.
55 /// Once a task has been woken up, it should attempt to `poll` the future
56 /// again, which may or may not produce a final value.
58 /// Note that on multiple calls to `poll`, only the [`Waker`] from the
59 /// [`Context`] passed to the most recent call should be scheduled to
62 /// # Runtime characteristics
64 /// Futures alone are *inert*; they must be *actively* `poll`ed to make
65 /// progress, meaning that each time the current task is woken up, it should
66 /// actively re-`poll` pending futures that it still has an interest in.
68 /// The `poll` function is not called repeatedly in a tight loop -- instead,
69 /// it should only be called when the future indicates that it is ready to
70 /// make progress (by calling `wake()`). If you're familiar with the
71 /// `poll(2)` or `select(2)` syscalls on Unix it's worth noting that futures
72 /// typically do *not* suffer the same problems of "all wakeups must poll
73 /// all events"; they are more like `epoll(4)`.
75 /// An implementation of `poll` should strive to return quickly, and should
76 /// not block. Returning quickly prevents unnecessarily clogging up
77 /// threads or event loops. If it is known ahead of time that a call to
78 /// `poll` may end up taking awhile, the work should be offloaded to a
79 /// thread pool (or something similar) to ensure that `poll` can return
84 /// Once a future has completed (returned `Ready` from `poll`), calling its
85 /// `poll` method again may panic, block forever, or cause other kinds of
86 /// problems; the `Future` trait places no requirements on the effects of
87 /// such a call. However, as the `poll` method is not marked `unsafe`,
88 /// Rust's usual rules apply: calls must never cause undefined behavior
89 /// (memory corruption, incorrect use of `unsafe` functions, or the like),
90 /// regardless of the future's state.
92 /// [`Poll::Pending`]: ../task/enum.Poll.html#variant.Pending
93 /// [`Poll::Ready(val)`]: ../task/enum.Poll.html#variant.Ready
94 /// [`Context`]: ../task/struct.Context.html
95 /// [`Waker`]: ../task/struct.Waker.html
96 /// [`Waker::wake`]: ../task/struct.Waker.html#method.wake
97 #[stable(feature = "futures_api", since = "1.36.0")]
98 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>;
101 #[stable(feature = "futures_api", since = "1.36.0")]
102 impl<F: ?Sized + Future + Unpin> Future for &mut F {
103 type Output = F::Output;
105 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
106 F::poll(Pin::new(&mut **self), cx)
110 #[stable(feature = "futures_api", since = "1.36.0")]
111 impl<P> Future for Pin<P>
113 P: Unpin + ops::DerefMut<Target: Future>,
115 type Output = <<P as ops::Deref>::Target as Future>::Output;
117 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
118 Pin::get_mut(self).as_mut().poll(cx)