pollable_map/futures/
optional.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use futures::future::FusedFuture;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll, Waker};

/// A reusable future that is the equivalent to an `Option`.
///
/// By default, this future will be empty, which would return  [`Poll::Pending`] when polled,
/// but if a [`Future`] is supplied either upon construction via [`OptionalFuture::new`] or
/// is set via [`OptionalFuture::replace`], the future would then be polled once [`OptionalFuture`]
/// is polled. Once the future is polled to completion, the results will be returned, with
/// [`OptionalFuture`] being empty.
pub struct OptionalFuture<F> {
    future: Option<F>,
    waker: Option<Waker>,
}

impl<F: Unpin> Unpin for OptionalFuture<F> {}

impl<F> Default for OptionalFuture<F> {
    fn default() -> Self {
        Self {
            future: None,
            waker: None,
        }
    }
}

impl<F> OptionalFuture<F>
where
    F: Future + Send + Unpin + 'static,
{
    /// Construct a new `OptionalFuture` with an existing `Future`.
    pub fn new(future: F) -> Self {
        Self {
            future: Some(future),
            waker: None,
        }
    }

    /// Takes the future out, leaving the OptionalFuture empty.
    pub fn take(&mut self) -> Option<F> {
        let fut = self.future.take();
        if let Some(waker) = self.waker.take() {
            waker.wake();
        }
        fut
    }

    /// Returns true if future still exist.
    pub fn is_some(&self) -> bool {
        self.future.is_some()
    }

    /// Returns false if future doesnt exist or has been completed.
    pub fn is_none(&self) -> bool {
        self.future.is_none()
    }

    /// Returns reference of the future.
    pub fn as_ref(&self) -> Option<&F> {
        self.future.as_ref()
    }

    /// Returns mutable reference of the future.
    pub fn as_mut(&mut self) -> Option<&mut F> {
        self.future.as_mut()
    }

    /// Replaces the current future with a new one, returning the previous future if present.
    pub fn replace(&mut self, future: F) -> Option<F> {
        let fut = self.future.replace(future);
        if let Some(waker) = self.waker.take() {
            waker.wake();
        }
        fut
    }
}

impl<F> Future for OptionalFuture<F>
where
    F: Future + Send + Unpin + 'static,
{
    type Output = F::Output;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        let Some(future) = self.future.as_mut() else {
            self.waker.replace(cx.waker().clone());
            return Poll::Pending;
        };

        match Pin::new(future).poll(cx) {
            Poll::Ready(output) => {
                self.future.take();
                Poll::Ready(output)
            }
            Poll::Pending => {
                self.waker.replace(cx.waker().clone());
                Poll::Pending
            }
        }
    }
}

impl<F: Future> FusedFuture for OptionalFuture<F>
where
    F: Future + Send + Unpin + 'static,
{
    fn is_terminated(&self) -> bool {
        self.future.is_none()
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_optional_future() {
        let mut future = OptionalFuture::new(futures::future::ready(0));
        assert!(future.is_some());
        let waker = futures::task::noop_waker_ref();

        let val = Pin::new(&mut future).poll(&mut Context::from_waker(waker));
        assert_eq!(val, Poll::Ready(0));
        assert!(future.is_none());
    }

    #[test]
    fn reusable_optional_future() {
        let mut future = OptionalFuture::new(futures::future::ready(0));
        assert!(future.is_some());
        let waker = futures::task::noop_waker_ref();

        let val = Pin::new(&mut future).poll(&mut Context::from_waker(waker));
        assert_eq!(val, Poll::Ready(0));
        assert!(future.is_none());

        future.replace(futures::future::ready(1));
        assert!(future.is_some());

        let val = Pin::new(&mut future).poll(&mut Context::from_waker(waker));
        assert_eq!(val, Poll::Ready(1));
        assert!(future.is_none());
    }
}