use pin_project::pin_project; use std::{ future::Future, ops::Deref, pin::Pin, task::{Context, Poll}, }; #[pin_project] #[derive(Debug, Clone)] #[must_use = "futures do nothing unless you `.await` or poll them"] pub struct OptionFuture(#[pin] Option); impl Future for OptionFuture { type Output = Option; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { match self.project().0.as_pin_mut() { Some(x) => x.poll(cx).map(Some), None => Poll::Ready(None), } } } impl Deref for OptionFuture { type Target = Option; fn deref(&self) -> &Self::Target { &self.0 } } impl From> for OptionFuture { fn from(option: Option) -> Self { OptionFuture(option) } }