AppFlowy/shared-lib/lib-infra/src/future.rs

68 lines
1.4 KiB
Rust
Raw Normal View History

2022-02-08 13:22:11 +00:00
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<T, O>(f: T) -> Fut<O>
where
T: Future<Output = O> + Send + Sync + 'static,
{
Fut { fut: Box::pin(f) }
}
#[pin_project]
pub struct Fut<T> {
#[pin]
pub fut: Pin<Box<dyn Future<Output = T> + Sync + Send>>,
}
impl<T> Future for Fut<T>
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)))
}
}
#[pin_project]
2021-12-13 05:55:44 +00:00
pub struct FutureResult<T, E> {
#[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> {
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>
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-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>>;