implicit_await/
as_future.rs

1use core::future::Future;
2use core::pin::Pin;
3use core::task::{Context, Poll};
4
5use implicit_await_macro::as_future_internal;
6
7pub trait FutureAsFuture {
8    fn as_future(self) -> Self;
9}
10
11impl<T: Future> FutureAsFuture for T {
12    fn as_future(self) -> Self {
13        self
14    }
15}
16
17pub trait NonFutureAsFuture : Sized {
18    fn as_future(self) -> Ready<Self>;
19}
20
21// Clone of future-rs' Ready, to avoid needing to take a dependency on them.
22pub struct Ready<T>(Option<T>);
23
24impl<T> Unpin for Ready<T> {}
25
26impl<T> Future for Ready<T> {
27    type Output = T;
28
29    fn poll(mut self: Pin<&mut Self>, _context: &mut Context<'_>) -> Poll<Self::Output> {
30        Poll::Ready(self.0.take().unwrap())
31    }
32}
33
34pub fn ready<T>(t: T) -> Ready<T> {
35    Ready(Some(t))
36}
37
38#[cfg(feature = "std")]
39impl<T, E> NonFutureAsFuture for Result<T, E> {
40    fn as_future(self) -> Ready<Self> {
41        ready(self)
42    }
43}
44
45include!{"as_future_core.rs"}
46
47#[cfg(feature = "std")]
48include!{"as_future_std.rs"}