cute_async/adaptors.rs
1//!Future adaptors to transform some builtin types into `Future`
2
3use core::task;
4use core::pin::Pin;
5use core::future::Future;
6
7///Wrapper over Option that implements `Future`
8///
9///If there is `Some(T)` then it is polled.
10///Otherwise it returns `None` immediately.
11///
12///## Usage:
13///
14///```rust,no_run
15///use cute_async::IntoFuture;
16///
17///use core::task;
18///use core::pin::Pin;
19///use core::future::Future;
20///
21///pub struct MyFuture;
22///
23///impl Future for MyFuture {
24/// type Output = ();
25///
26/// fn poll(self: Pin<&mut Self>, ctx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
27/// task::Poll::Ready(())
28/// }
29///}
30///
31///let fut_maybe = if true {
32/// Some(MyFuture)
33///} else {
34/// None
35///};
36///
37///let mut future = fut_maybe.into_future();
38///```
39pub struct OptionFut<F> {
40 inner: Option<F>,
41}
42
43impl<F> From<Option<F>> for OptionFut<F> {
44 fn from(inner: Option<F>) -> Self {
45 Self {
46 inner
47 }
48 }
49}
50
51impl<F: Unpin> Unpin for OptionFut<F> {}
52
53impl<F: Future> Future for OptionFut<F> {
54 type Output = Option<F::Output>;
55
56 fn poll(self: Pin<&mut Self>, ctx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
57 let this = unsafe {
58 self.get_unchecked_mut()
59 };
60
61 match this.inner.as_mut() {
62 Some(fut) => Future::poll(unsafe { Pin::new_unchecked(fut) }, ctx).map(|res| Some(res)),
63 None => task::Poll::Ready(None)
64 }
65 }
66}
67
68impl<F: Future> crate::fut::IntoFuture for Option<F> {
69 type Output = OptionFut<F>;
70
71 fn into_future(self) -> Self::Output {
72 self.into()
73 }
74}
75
76///Wrapper over Result that implements `Future`
77///
78///Depending on variant, it polls underlying future and returns `Output` in corresponding variant.
79///
80///## Usage:
81///
82///```rust,no_run
83///use cute_async::IntoFuture;
84///
85///use core::task;
86///use core::pin::Pin;
87///use core::future::Future;
88///
89///pub struct MyFuture;
90///
91///impl Future for MyFuture {
92/// type Output = ();
93///
94/// fn poll(self: Pin<&mut Self>, ctx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
95/// task::Poll::Ready(())
96/// }
97///}
98///
99///let result = if true {
100/// Ok(MyFuture)
101///} else {
102/// Err(MyFuture)
103///};
104///
105///let mut future = result.into_future();
106///```
107pub struct ResultFut<T, E> {
108 inner: Result<T, E>
109}
110
111impl<T, E> From<Result<T, E>> for ResultFut<T, E> {
112 fn from(inner: Result<T, E>) -> Self {
113 Self {
114 inner
115 }
116 }
117}
118
119impl<T: Unpin, E: Unpin> Unpin for ResultFut<T, E> {}
120
121impl<T: Future, E: Future> Future for ResultFut<T, E> {
122 type Output = Result<T::Output, E::Output>;
123
124 fn poll(self: Pin<&mut Self>, ctx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
125 let this = unsafe {
126 self.get_unchecked_mut()
127 };
128
129 match this.inner.as_mut() {
130 Ok(ok) => Future::poll(unsafe { Pin::new_unchecked(ok) }, ctx).map(|res| Ok(res)),
131 Err(err) => Future::poll(unsafe { Pin::new_unchecked(err) }, ctx).map(|res| Err(res)),
132 }
133 }
134}
135
136impl<T: Future, E: Future> crate::fut::IntoFuture for Result<T, E> {
137 type Output = ResultFut<T, E>;
138
139 fn into_future(self) -> Self::Output {
140 self.into()
141 }
142}