hrpc/common/future.rs
1use std::future::Future;
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5/// Creates a future that is immediately ready with a value.
6///
7/// This `struct` is created by [`ready()`]. See its
8/// documentation for more.
9#[derive(Debug, Clone)]
10#[must_use = "futures do nothing unless you `.await` or poll them"]
11pub struct Ready<T>(Option<T>);
12
13impl<T> Ready<T> {
14 /// Extract the inner value of this [`Ready`].
15 pub fn into_inner(self) -> Option<T> {
16 self.0
17 }
18}
19
20impl<T> Unpin for Ready<T> {}
21
22impl<T> Future for Ready<T> {
23 type Output = T;
24
25 #[inline]
26 fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<T> {
27 Poll::Ready(self.0.take().expect("`Ready` polled after completion"))
28 }
29}
30
31/// Creates a future that is immediately ready with a value.
32///
33/// Futures created through this function are functionally similar to those
34/// created through `async {}`. The main difference is that futures created
35/// through this function are named and implement `Unpin`.
36///
37/// # Examples
38///
39/// ```
40/// use std::future;
41///
42/// # async fn run() {
43/// let a = future::ready(1);
44/// assert_eq!(a.await, 1);
45/// # }
46/// ```
47pub fn ready<T>(t: T) -> Ready<T> {
48 Ready(Some(t))
49}