Coverage Report

Created: 2025-07-11 06:53

/rust/registry/src/index.crates.io-6f17d22bba15001f/futures-util-0.3.31/src/future/option.rs
Line
Count
Source (jump to first uncovered line)
1
//! Definition of the `Option` (optional step) combinator
2
3
use core::pin::Pin;
4
use futures_core::future::{FusedFuture, Future};
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> Default for OptionFuture<F> {
35
0
    fn default() -> Self {
36
0
        Self { inner: None }
37
0
    }
38
}
39
40
impl<F: Future> Future for OptionFuture<F> {
41
    type Output = Option<F::Output>;
42
43
0
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
44
0
        match self.project().inner.as_pin_mut() {
45
0
            Some(x) => x.poll(cx).map(Some),
46
0
            None => Poll::Ready(None),
47
        }
48
0
    }
49
}
50
51
impl<F: FusedFuture> FusedFuture for OptionFuture<F> {
52
0
    fn is_terminated(&self) -> bool {
53
0
        match &self.inner {
54
0
            Some(x) => x.is_terminated(),
55
0
            None => true,
56
        }
57
0
    }
58
}
59
60
impl<T> From<Option<T>> for OptionFuture<T> {
61
0
    fn from(option: Option<T>) -> Self {
62
0
        Self { inner: option }
63
0
    }
64
}