]> git.lizzy.rs Git - rust.git/blob - src/libcore/future/ready.rs
Rollup merge of #73771 - alexcrichton:ignore-unstable, r=estebank,GuillaumeGomez
[rust.git] / src / libcore / future / ready.rs
1 use crate::future::Future;
2 use crate::pin::Pin;
3 use crate::task::{Context, Poll};
4
5 /// Creates a future that is immediately ready with a value.
6 ///
7 /// This `struct` is created by the [`ready`] function. See its
8 /// documentation for more.
9 ///
10 /// [`ready`]: fn.ready.html
11 #[unstable(feature = "future_readiness_fns", issue = "70921")]
12 #[derive(Debug, Clone)]
13 #[must_use = "futures do nothing unless you `.await` or poll them"]
14 pub struct Ready<T>(Option<T>);
15
16 #[unstable(feature = "future_readiness_fns", issue = "70921")]
17 impl<T> Unpin for Ready<T> {}
18
19 #[unstable(feature = "future_readiness_fns", issue = "70921")]
20 impl<T> Future for Ready<T> {
21     type Output = T;
22
23     #[inline]
24     fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<T> {
25         Poll::Ready(self.0.take().expect("Ready polled after completion"))
26     }
27 }
28
29 /// Creates a future that is immediately ready with a value.
30 ///
31 /// # Examples
32 ///
33 /// ```
34 /// #![feature(future_readiness_fns)]
35 /// use core::future;
36 ///
37 /// # async fn run() {
38 /// let a = future::ready(1);
39 /// assert_eq!(a.await, 1);
40 /// # }
41 /// ```
42 #[unstable(feature = "future_readiness_fns", issue = "70921")]
43 pub fn ready<T>(t: T) -> Ready<T> {
44     Ready(Some(t))
45 }