2022-02-08 13:22:11 +00:00
|
|
|
use futures_core::future::BoxFuture;
|
|
|
|
use futures_core::ready;
|
2021-08-20 06:38:03 +00:00
|
|
|
use pin_project::pin_project;
|
|
|
|
use std::{
|
|
|
|
fmt::Debug,
|
|
|
|
future::Future,
|
|
|
|
pin::Pin,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
|
|
|
|
2022-08-15 12:07:01 +00:00
|
|
|
pub fn wrap_future<T, O>(f: T) -> AFFuture<O>
|
2021-09-25 13:47:02 +00:00
|
|
|
where
|
|
|
|
T: Future<Output = O> + Send + Sync + 'static,
|
|
|
|
{
|
2022-08-15 12:07:01 +00:00
|
|
|
AFFuture { fut: Box::pin(f) }
|
2021-09-25 13:47:02 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 06:38:03 +00:00
|
|
|
#[pin_project]
|
2022-08-15 12:07:01 +00:00
|
|
|
pub struct AFFuture<T> {
|
2021-08-20 06:38:03 +00:00
|
|
|
#[pin]
|
|
|
|
pub fut: Pin<Box<dyn Future<Output = T> + Sync + Send>>,
|
|
|
|
}
|
|
|
|
|
2022-08-15 12:07:01 +00:00
|
|
|
impl<T> Future for AFFuture<T>
|
2021-08-20 06:38:03 +00:00
|
|
|
where
|
|
|
|
T: Send + Sync,
|
|
|
|
{
|
|
|
|
type Output = T;
|
|
|
|
|
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
let this = self.as_mut().project();
|
2021-11-27 11:19:41 +00:00
|
|
|
Poll::Ready(ready!(this.fut.poll(cx)))
|
2021-08-20 06:38:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pin_project]
|
2021-12-13 05:55:44 +00:00
|
|
|
pub struct FutureResult<T, E> {
|
2021-08-20 06:38:03 +00:00
|
|
|
#[pin]
|
|
|
|
pub fut: Pin<Box<dyn Future<Output = Result<T, E>> + Sync + Send>>,
|
|
|
|
}
|
|
|
|
|
2021-12-13 05:55:44 +00:00
|
|
|
impl<T, E> FutureResult<T, E> {
|
2021-08-20 06:38:03 +00:00
|
|
|
pub fn new<F>(f: F) -> Self
|
|
|
|
where
|
|
|
|
F: Future<Output = Result<T, E>> + Send + Sync + 'static,
|
|
|
|
{
|
|
|
|
Self {
|
|
|
|
fut: Box::pin(async { f.await }),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-13 05:55:44 +00:00
|
|
|
impl<T, E> Future for FutureResult<T, E>
|
2021-08-20 06:38:03 +00:00
|
|
|
where
|
|
|
|
T: Send + Sync,
|
|
|
|
E: Debug,
|
|
|
|
{
|
|
|
|
type Output = Result<T, E>;
|
|
|
|
|
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
let this = self.as_mut().project();
|
2021-11-27 11:19:41 +00:00
|
|
|
let result = ready!(this.fut.poll(cx));
|
|
|
|
Poll::Ready(result)
|
2021-08-20 06:38:03 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-13 05:55:44 +00:00
|
|
|
|
2021-12-27 03:15:15 +00:00
|
|
|
pub type BoxResultFuture<'a, T, E> = BoxFuture<'a, Result<T, E>>;
|