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 to_future(f: T) -> Fut where T: Future + Send + Sync + 'static, { Fut { fut: Box::pin(f) } } #[pin_project] pub struct Fut { #[pin] pub fut: Pin + Sync + Send>>, } impl Future for Fut 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>;