use futures_core::future::BoxFuture; use futures_core::ready; use pin_project::pin_project; use std::{ fmt::Debug, future::Future, pin::Pin, task::{Context, Poll}, }; pub fn wrap_future(f: T) -> AFFuture where T: Future + Send + Sync + 'static, { AFFuture { fut: Box::pin(f) } } #[pin_project] pub struct AFFuture { #[pin] pub fut: Pin + Sync + Send>>, } impl Future for AFFuture where T: Send + Sync, { 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))) } } #[pin_project] pub struct FutureResult { #[pin] pub fut: Pin> + Sync + Send>>, } impl FutureResult { pub fn new(f: F) -> Self where F: Future> + Send + Sync + 'static, { Self { fut: Box::pin(async { f.await }), } } } impl Future for FutureResult where T: Send + Sync, 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) } } pub type BoxResultFuture<'a, T, E> = BoxFuture<'a, Result>;