1use crate::future::Future;
2use crate::pin::Pin;
3use crate::task::{Context, Poll};
45/// A future that is immediately ready with a value.
6///
7/// This `struct` is created by [`ready()`]. See its
8/// documentation for more.
9#[stable(feature = "future_readiness_fns", since = "1.48.0")]
10#[derive(Debug, Clone)]
11#[must_use = "futures do nothing unless you `.await` or poll them"]
12pub struct Ready<T>(Option<T>);
1314#[stable(feature = "future_readiness_fns", since = "1.48.0")]
15impl<T> Unpin for Ready<T> {}
1617#[stable(feature = "future_readiness_fns", since = "1.48.0")]
18impl<T> Future for Ready<T> {
19type Output = T;
2021#[inline]
22fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<T> {
23 Poll::Ready(self.0.take().expect("`Ready` polled after completion"))
24 }
25}
2627impl<T> Ready<T> {
28/// Consumes the `Ready`, returning the wrapped value.
29 ///
30 /// # Panics
31 ///
32 /// Will panic if this [`Ready`] was already polled to completion.
33 ///
34 /// # Examples
35 ///
36 /// ```
37 /// use std::future;
38 ///
39 /// let a = future::ready(1);
40 /// assert_eq!(a.into_inner(), 1);
41 /// ```
42#[stable(feature = "ready_into_inner", since = "1.82.0")]
43 #[must_use]
44 #[inline]
45pub fn into_inner(self) -> T {
46self.0.expect("Called `into_inner()` on `Ready` after completion")
47 }
48}
4950/// Creates a future that is immediately ready with a value.
51///
52/// Futures created through this function are functionally similar to those
53/// created through `async {}`. The main difference is that futures created
54/// through this function are named and implement `Unpin`.
55///
56/// # Examples
57///
58/// ```
59/// use std::future;
60///
61/// # async fn run() {
62/// let a = future::ready(1);
63/// assert_eq!(a.await, 1);
64/// # }
65/// ```
66#[stable(feature = "future_readiness_fns", since = "1.48.0")]
67pub fn ready<T>(t: T) -> Ready<T> {
68 Ready(Some(t))
69}