auto_future/
auto_future.rs1use ::std::fmt::Debug;
2use ::std::fmt::Formatter;
3use ::std::fmt::Result as FmtResult;
4use ::std::future::Future;
5use ::std::pin::Pin;
6use ::std::task::Context;
7use ::std::task::Poll;
8
9pub struct AutoFuture<T> {
10 inner: Pin<Box<dyn Future<Output = T>>>,
11 is_done: bool,
12}
13
14impl<T> AutoFuture<T> {
15 pub fn new<F>(raw_future: F) -> Self
16 where
17 F: Future<Output = T> + 'static,
18 {
19 Self {
20 inner: Box::pin(raw_future),
21 is_done: false,
22 }
23 }
24}
25
26impl<T> Future for AutoFuture<T> {
27 type Output = T;
28
29 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
30 if self.is_done {
31 panic!("Polling future when this is already completed");
32 }
33
34 let inner_poll = self.inner.as_mut().poll(cx);
35 match inner_poll {
36 Poll::Pending => Poll::Pending,
37 Poll::Ready(inner_result) => {
38 self.is_done = true;
39 Poll::Ready(inner_result)
40 }
41 }
42 }
43}
44
45impl<T> Debug for AutoFuture<T> {
46 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
47 write!(
48 f,
49 "AutoFuture {{ inner: Pin<Box<dyn Future<Output = T>>>, is_dome: {:?} }}",
50 self.is_done
51 )
52 }
53}