use core::{future::Future, pin::Pin, task::Context, task::Poll};
pub struct DynFuture<'a, T>(&'a mut dyn Future<Output = T>);
impl<T> core::fmt::Debug for DynFuture<'_, T> {
fn fmt(
&self,
f: &mut core::fmt::Formatter<'_>,
) -> Result<(), core::fmt::Error> {
write!(f, "DynFuture")
}
}
impl<T> Future for DynFuture<'_, T> {
type Output = T;
#[allow(unsafe_code)]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut fut = unsafe { Pin::new_unchecked(std::ptr::read(&self.0)) };
let ret = fut.as_mut().poll(cx);
std::mem::forget(fut);
ret
}
}
pub trait DynFut<'a, T> {
fn fut(&'a mut self) -> DynFuture<'a, T>;
}
impl<'a, T, F> DynFut<'a, T> for F
where
F: Future<Output = T>,
{
fn fut(&'a mut self) -> DynFuture<'a, T> {
DynFuture(self)
}
}