futures_util/future/
option.rs
1use core::pin::Pin;
4use futures_core::future::{FusedFuture, Future};
5use futures_core::task::{Context, Poll};
6use pin_project_lite::pin_project;
7
8pin_project! {
9 #[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
34impl<F> Default for OptionFuture<F> {
35 fn default() -> Self {
36 Self { inner: None }
37 }
38}
39
40impl<F: Future> Future for OptionFuture<F> {
41 type Output = Option<F::Output>;
42
43 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
44 match self.project().inner.as_pin_mut() {
45 Some(x) => x.poll(cx).map(Some),
46 None => Poll::Ready(None),
47 }
48 }
49}
50
51impl<F: FusedFuture> FusedFuture for OptionFuture<F> {
52 fn is_terminated(&self) -> bool {
53 match &self.inner {
54 Some(x) => x.is_terminated(),
55 None => true,
56 }
57 }
58}
59
60impl<T> From<Option<T>> for OptionFuture<T> {
61 fn from(option: Option<T>) -> Self {
62 Self { inner: option }
63 }
64}