2021-08-20 06:38:03 +00:00
|
|
|
use futures_core::ready;
|
|
|
|
use pin_project::pin_project;
|
|
|
|
use std::{
|
|
|
|
fmt::Debug,
|
|
|
|
future::Future,
|
|
|
|
pin::Pin,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
|
|
|
|
2021-09-25 13:47:02 +00:00
|
|
|
pub fn wrap_future<T, O>(f: T) -> FnFuture<O>
|
|
|
|
where
|
|
|
|
T: Future<Output = O> + Send + Sync + 'static,
|
|
|
|
{
|
|
|
|
FnFuture { fut: Box::pin(f) }
|
|
|
|
}
|
|
|
|
|
2021-08-20 06:38:03 +00:00
|
|
|
#[pin_project]
|
2021-09-25 13:47:02 +00:00
|
|
|
pub struct FnFuture<T> {
|
2021-08-20 06:38:03 +00:00
|
|
|
#[pin]
|
|
|
|
pub fut: Pin<Box<dyn Future<Output = T> + Sync + Send>>,
|
|
|
|
}
|
|
|
|
|
2021-09-25 13:47:02 +00:00
|
|
|
impl<T> Future for FnFuture<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
|
|
|
|
|
|
|
#[pin_project]
|
|
|
|
pub struct FutureResultSend<T, E> {
|
|
|
|
#[pin]
|
|
|
|
pub fut: Pin<Box<dyn Future<Output = Result<T, E>> + Send>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, E> FutureResultSend<T, E> {
|
|
|
|
pub fn new<F>(f: F) -> Self
|
|
|
|
where
|
|
|
|
F: Future<Output = Result<T, E>> + Send + 'static,
|
|
|
|
{
|
|
|
|
Self {
|
|
|
|
fut: Box::pin(async { f.await }),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, E> Future for FutureResultSend<T, E>
|
|
|
|
where
|
|
|
|
T: Send,
|
|
|
|
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();
|
|
|
|
let result = ready!(this.fut.poll(cx));
|
|
|
|
Poll::Ready(result)
|
|
|
|
}
|
|
|
|
}
|