1use super::assert_future;
2use core::pin::Pin;
3use futures_core::future::{FusedFuture, Future};
4use futures_core::task::{Context, Poll};
5
6/// Future for the [`ready`](ready()) function.
7#[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 /// Unwraps the value from this immediately ready future.
13 #[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(msg:"Ready polled after completion"))
33 }
34}
35
36/// Creates a future that is immediately ready with a value.
37///
38/// # Examples
39///
40/// ```
41/// # futures::executor::block_on(async {
42/// use futures::future;
43///
44/// let a = future::ready(1);
45/// assert_eq!(a.await, 1);
46/// # });
47/// ```
48pub fn ready<T>(t: T) -> Ready<T> {
49 assert_future::<T, _>(Ready(Some(t)))
50}
51
52/// Create a future that is immediately ready with a success value.
53///
54/// # Examples
55///
56/// ```
57/// # futures::executor::block_on(async {
58/// use futures::future;
59///
60/// let a = future::ok::<i32, i32>(1);
61/// assert_eq!(a.await, Ok(1));
62/// # });
63/// ```
64pub fn ok<T, E>(t: T) -> Ready<Result<T, E>> {
65 Ready(Some(Ok(t)))
66}
67
68/// Create a future that is immediately ready with an error value.
69///
70/// # Examples
71///
72/// ```
73/// # futures::executor::block_on(async {
74/// use futures::future;
75///
76/// let a = future::err::<i32, i32>(1);
77/// assert_eq!(a.await, Err(1));
78/// # });
79/// ```
80pub fn err<T, E>(err: E) -> Ready<Result<T, E>> {
81 Ready(Some(Err(err)))
82}
83