futures_util/future/
ready.rs
1use super::assert_future;
2use core::pin::Pin;
3use futures_core::future::{FusedFuture, Future};
4use futures_core::task::{Context, Poll};
5
6#[derive(Debug, Clone)]
8#[must_use = "futures do nothing unless you `.await` or poll them"]
9pub struct Ready<T>(Option<T>);
10
11impl<T> Ready<T> {
12 #[inline]
14 pub fn into_inner(mut self) -> T {
15 self.0.take().unwrap()
16 }
17}
18
19impl<T> Unpin for Ready<T> {}
20
21impl<T> FusedFuture for Ready<T> {
22 fn is_terminated(&self) -> bool {
23 self.0.is_none()
24 }
25}
26
27impl<T> Future for Ready<T> {
28 type Output = T;
29
30 #[inline]
31 fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<T> {
32 Poll::Ready(self.0.take().expect("Ready polled after completion"))
33 }
34}
35
36pub fn ready<T>(t: T) -> Ready<T> {
49 assert_future::<T, _>(Ready(Some(t)))
50}
51
52pub fn ok<T, E>(t: T) -> Ready<Result<T, E>> {
65 Ready(Some(Ok(t)))
66}
67
68pub fn err<T, E>(err: E) -> Ready<Result<T, E>> {
81 Ready(Some(Err(err)))
82}