use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use crate::shutdown_signal::ShutdownSignal;
#[must_use = "futures must be polled to make progress"]
pub struct WrapCancel<F> {
pub(crate) shutdown_signal: ShutdownSignal,
pub(crate) future: Option<F>,
}
impl<F: Future> Future for WrapCancel<F> {
type Output = Option<F::Output>;
#[inline]
fn poll(self: Pin<&mut Self>, context: &mut Context) -> Poll<Self::Output> {
let me = unsafe { self.get_unchecked_mut() };
match &mut me.future {
None => return Poll::Ready(None),
Some(future) => {
let future = unsafe { Pin::new_unchecked(future) };
if let Poll::Ready(value) = future.poll(context) {
return Poll::Ready(Some(value));
}
},
};
Pin::new(&mut me.shutdown_signal)
.poll(context)
.map(|()| None)
}
}