1 use crate::future::Future;
3 use crate::task::{Context, Poll};
5 /// A future that is immediately ready with a value.
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"]
12 pub struct Ready<T>(Option<T>);
14 #[stable(feature = "future_readiness_fns", since = "1.48.0")]
15 impl<T> Unpin for Ready<T> {}
17 #[stable(feature = "future_readiness_fns", since = "1.48.0")]
18 impl<T> Future for Ready<T> {
22 fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<T> {
23 Poll::Ready(self.0.take().expect("`Ready` polled after completion"))
27 /// Creates a future that is immediately ready with a value.
29 /// Futures created through this function are functionally similar to those
30 /// created through `async {}`. The main difference is that futures created
31 /// through this function are named and implement `Unpin`.
38 /// # async fn run() {
39 /// let a = future::ready(1);
40 /// assert_eq!(a.await, 1);
43 #[stable(feature = "future_readiness_fns", since = "1.48.0")]
44 pub fn ready<T>(t: T) -> Ready<T> {