use futures_core::ready; use pin_project::pin_project; use std::{ fmt::Debug, future::Future, pin::Pin, task::{Context, Poll}, }; #[pin_project] pub struct ClosureFuture { #[pin] pub fut: Pin + Sync + Send>>, } impl Future for ClosureFuture where T: Send + Sync, { type Output = T; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = self.as_mut().project(); loop { return Poll::Ready(ready!(this.fut.poll(cx))); } } } #[pin_project] pub struct ResultFuture { #[pin] pub fut: Pin> + Sync + Send>>, } impl ResultFuture { pub fn new(f: F) -> Self where F: Future> + Send + Sync + 'static, { Self { fut: Box::pin(async { f.await }), } } } impl Future for ResultFuture 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(); loop { let result = ready!(this.fut.poll(cx)); return Poll::Ready(result); } } }