]> git.lizzy.rs Git - rust.git/blob - library/core/src/future/future.rs
Auto merge of #75566 - alasher:master, r=oli-obk
[rust.git] / library / core / src / future / future.rs
1 #![stable(feature = "futures_api", since = "1.36.0")]
2
3 use crate::marker::Unpin;
4 use crate::ops;
5 use crate::pin::Pin;
6 use crate::task::{Context, Poll};
7
8 /// A future represents an asynchronous computation.
9 ///
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.
13 ///
14 /// # The `poll` method
15 ///
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
21 /// task.
22 ///
23 /// When using a future, you generally won't call `poll` directly, but instead
24 /// `.await` the value.
25 ///
26 /// [`Waker`]: ../task/struct.Waker.html
27 #[doc(spotlight)]
28 #[must_use = "futures do nothing unless you `.await` or poll them"]
29 #[stable(feature = "futures_api", since = "1.36.0")]
30 #[lang = "future_trait"]
31 #[rustc_on_unimplemented(label = "`{Self}` is not a future", message = "`{Self}` is not a future")]
32 pub trait Future {
33     /// The type of value produced on completion.
34     #[stable(feature = "futures_api", since = "1.36.0")]
35     type Output;
36
37     /// Attempt to resolve the future to a final value, registering
38     /// the current task for wakeup if the value is not yet available.
39     ///
40     /// # Return value
41     ///
42     /// This function returns:
43     ///
44     /// - [`Poll::Pending`] if the future is not ready yet
45     /// - [`Poll::Ready(val)`] with the result `val` of this future if it
46     ///   finished successfully.
47     ///
48     /// Once a future has finished, clients should not `poll` it again.
49     ///
50     /// When a future is not ready yet, `poll` returns `Poll::Pending` and
51     /// stores a clone of the [`Waker`] copied from the current [`Context`].
52     /// This [`Waker`] is then woken once the future can make progress.
53     /// For example, a future waiting for a socket to become
54     /// readable would call `.clone()` on the [`Waker`] and store it.
55     /// When a signal arrives elsewhere indicating that the socket is readable,
56     /// [`Waker::wake`] is called and the socket future's task is awoken.
57     /// Once a task has been woken up, it should attempt to `poll` the future
58     /// again, which may or may not produce a final value.
59     ///
60     /// Note that on multiple calls to `poll`, only the [`Waker`] from the
61     /// [`Context`] passed to the most recent call should be scheduled to
62     /// receive a wakeup.
63     ///
64     /// # Runtime characteristics
65     ///
66     /// Futures alone are *inert*; they must be *actively* `poll`ed to make
67     /// progress, meaning that each time the current task is woken up, it should
68     /// actively re-`poll` pending futures that it still has an interest in.
69     ///
70     /// The `poll` function is not called repeatedly in a tight loop -- instead,
71     /// it should only be called when the future indicates that it is ready to
72     /// make progress (by calling `wake()`). If you're familiar with the
73     /// `poll(2)` or `select(2)` syscalls on Unix it's worth noting that futures
74     /// typically do *not* suffer the same problems of "all wakeups must poll
75     /// all events"; they are more like `epoll(4)`.
76     ///
77     /// An implementation of `poll` should strive to return quickly, and should
78     /// not block. Returning quickly prevents unnecessarily clogging up
79     /// threads or event loops. If it is known ahead of time that a call to
80     /// `poll` may end up taking awhile, the work should be offloaded to a
81     /// thread pool (or something similar) to ensure that `poll` can return
82     /// quickly.
83     ///
84     /// # Panics
85     ///
86     /// Once a future has completed (returned `Ready` from `poll`), calling its
87     /// `poll` method again may panic, block forever, or cause other kinds of
88     /// problems; the `Future` trait places no requirements on the effects of
89     /// such a call. However, as the `poll` method is not marked `unsafe`,
90     /// Rust's usual rules apply: calls must never cause undefined behavior
91     /// (memory corruption, incorrect use of `unsafe` functions, or the like),
92     /// regardless of the future's state.
93     ///
94     /// [`Poll::Pending`]: ../task/enum.Poll.html#variant.Pending
95     /// [`Poll::Ready(val)`]: ../task/enum.Poll.html#variant.Ready
96     /// [`Context`]: ../task/struct.Context.html
97     /// [`Waker`]: ../task/struct.Waker.html
98     /// [`Waker::wake`]: ../task/struct.Waker.html#method.wake
99     #[cfg_attr(not(bootstrap), lang = "poll")]
100     #[stable(feature = "futures_api", since = "1.36.0")]
101     fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>;
102 }
103
104 #[stable(feature = "futures_api", since = "1.36.0")]
105 impl<F: ?Sized + Future + Unpin> Future for &mut F {
106     type Output = F::Output;
107
108     fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
109         F::poll(Pin::new(&mut **self), cx)
110     }
111 }
112
113 #[stable(feature = "futures_api", since = "1.36.0")]
114 impl<P> Future for Pin<P>
115 where
116     P: Unpin + ops::DerefMut<Target: Future>,
117 {
118     type Output = <<P as ops::Deref>::Target as Future>::Output;
119
120     fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
121         Pin::get_mut(self).as_mut().poll(cx)
122     }
123 }