1 //! Definition of the `Option` (optional step) combinator 2 3 use core::pin::Pin; 4 use futures_core::future::{Future, FusedFuture}; 5 use futures_core::task::{Context, Poll}; 6 use pin_project_lite::pin_project; 7 8 pin_project! { 9 /// A future representing a value which may or may not be present. 10 /// 11 /// Created by the [`From`] implementation for [`Option`](std::option::Option). 12 /// 13 /// # Examples 14 /// 15 /// ``` 16 /// # futures::executor::block_on(async { 17 /// use futures::future::OptionFuture; 18 /// 19 /// let mut a: OptionFuture<_> = Some(async { 123 }).into(); 20 /// assert_eq!(a.await, Some(123)); 21 /// 22 /// a = None.into(); 23 /// assert_eq!(a.await, None); 24 /// # }); 25 /// ``` 26 #[derive(Debug, Clone)] 27 #[must_use = "futures do nothing unless you `.await` or poll them"] 28 pub struct OptionFuture<F> { 29 #[pin] 30 inner: Option<F>, 31 } 32 } 33 34 impl<F: Future> Future for OptionFuture<F> { 35 type Output = Option<F::Output>; 36 poll( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Self::Output>37 fn poll( 38 self: Pin<&mut Self>, 39 cx: &mut Context<'_>, 40 ) -> Poll<Self::Output> { 41 match self.project().inner.as_pin_mut() { 42 Some(x) => x.poll(cx).map(Some), 43 None => Poll::Ready(None), 44 } 45 } 46 } 47 48 impl<F: FusedFuture> FusedFuture for OptionFuture<F> { is_terminated(&self) -> bool49 fn is_terminated(&self) -> bool { 50 match &self.inner { 51 Some(x) => x.is_terminated(), 52 None => true, 53 } 54 } 55 } 56 57 impl<T> From<Option<T>> for OptionFuture<T> { from(option: Option<T>) -> Self58 fn from(option: Option<T>) -> Self { 59 Self { inner: option } 60 } 61 } 62