use std::pin::Pin;
#[inline]
pub fn map<F, M, O>(f: F, map: M) -> Map<F, M>
where
F: Future,
M: FnOnce(F::Output) -> O,
{
Map { f, map: Some(map) }
}
#[derive(Debug)]
pub struct Map<F, M> {
f: F,
map: Option<M>,
}
impl<F, M, O> Future for Map<F, M>
where
F: Future,
M: FnOnce(F::Output) -> O,
{
type Output = O;
fn poll(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
let (f, map) = unsafe {
let me = self.get_unchecked_mut();
(Pin::new_unchecked(&mut me.f), &mut me.map)
};
f.poll(cx).map(map.take().expect("poll after complete"))
}
}