use futures_core::future::LocalBoxFuture; use futures_core::ready; use pin_project::pin_project; use std::{ fmt::Debug, future::Future, pin::Pin, task::{Context, Poll}, }; #[allow(dead_code)] pub fn to_fut(f: T) -> Fut where T: Future + 'static, { Fut { fut: Box::pin(f) } } #[pin_project] pub struct Fut { #[pin] pub fut: Pin>>, } impl Future for Fut { type Output = T; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = self.as_mut().project(); Poll::Ready(ready!(this.fut.poll(cx))) } } #[allow(dead_code)] #[pin_project] pub struct FutureResult { #[pin] pub fut: Pin>>>, } impl FutureResult { #[allow(dead_code)] pub fn new(f: F) -> Self where F: Future> + 'static, { Self { fut: Box::pin(f) } } } impl Future for FutureResult where E: Debug, { type Output = Result; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = self.as_mut().project(); let result = ready!(this.fut.poll(cx)); Poll::Ready(result) } } #[allow(dead_code)] pub type BoxResultFuture<'a, T, E> = LocalBoxFuture<'a, Result>;