/rust/registry/src/index.crates.io-6f17d22bba15001f/futures-util-0.3.31/src/future/ready.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use super::assert_future; |
2 | | use core::pin::Pin; |
3 | | use futures_core::future::{FusedFuture, Future}; |
4 | | use 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"] |
9 | | pub struct Ready<T>(Option<T>); |
10 | | |
11 | | impl<T> Ready<T> { |
12 | | /// Unwraps the value from this immediately ready future. |
13 | | #[inline] |
14 | 0 | pub fn into_inner(mut self) -> T { |
15 | 0 | self.0.take().unwrap() |
16 | 0 | } |
17 | | } |
18 | | |
19 | | impl<T> Unpin for Ready<T> {} |
20 | | |
21 | | impl<T> FusedFuture for Ready<T> { |
22 | 0 | fn is_terminated(&self) -> bool { |
23 | 0 | self.0.is_none() |
24 | 0 | } |
25 | | } |
26 | | |
27 | | impl<T> Future for Ready<T> { |
28 | | type Output = T; |
29 | | |
30 | | #[inline] |
31 | 0 | fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<T> { |
32 | 0 | Poll::Ready(self.0.take().expect("Ready polled after completion")) |
33 | 0 | } |
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 | | /// ``` |
48 | 0 | pub fn ready<T>(t: T) -> Ready<T> { |
49 | 0 | assert_future::<T, _>(Ready(Some(t))) |
50 | 0 | } |
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 | | /// ``` |
64 | 0 | pub fn ok<T, E>(t: T) -> Ready<Result<T, E>> { |
65 | 0 | Ready(Some(Ok(t))) |
66 | 0 | } |
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 | | /// ``` |
80 | 0 | pub fn err<T, E>(err: E) -> Ready<Result<T, E>> { |
81 | 0 | Ready(Some(Err(err))) |
82 | 0 | } |