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) -> FnFuture where T: Future + Send + Sync + 'static, { FnFuture { fut: Box::pin(f) } } #[pin_project] pub struct FnFuture { #[pin] pub fut: Pin + Sync + Send>>, } impl Future for FnFuture 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) } } #[pin_project] pub struct FutureResultSend { #[pin] pub fut: Pin> + Send>>, } impl FutureResultSend { pub fn new(f: F) -> Self where F: Future> + Send + 'static, { Self { fut: Box::pin(async { f.await }), } } } impl Future for FutureResultSend where T: Send, 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) } }