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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//! Extension trait to simplify optionally polling futures.

use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

/// Construct a fusing adapter that is capable of polling an interior value that
/// is being polled using a custom function.
///
/// The value of this container *will not* be cleared, since a common use case
/// is to optionally interact with stream-like things like [Interval] (see below
/// for example).
///
/// For simplicity's sake, this adapter also pins the value it's being
/// constructed with.
///
/// # Examples
///
/// ```rust
/// use std::time::Duration;
/// use tokio::time;
///
/// # #[tokio::main]
/// # async fn main() {
/// let mut interval = async_fuse::poll_fn(time::interval(Duration::from_millis(200)), time::Interval::poll_tick);
///
/// tokio::select! {
///     _ = &mut interval => {
///         interval.clear();
///     }
/// }
///
/// assert!(interval.is_empty());
/// # }
/// ```
///
/// [Interval]: https://docs.rs/tokio/1/tokio/time/struct.Interval.html
pub fn poll_fn<T, P, O>(value: T, poll: P) -> PollFn<T, P, O>
where
    T: Unpin,
    P: Unpin,
    P: FnMut(&mut T, &mut Context<'_>) -> Poll<O>,
{
    PollFn {
        value: Some(value),
        poll,
    }
}

/// Fusing adapter that is capable of polling an interior value that is
/// being fused using a custom polling function.
///
/// See [poll_fn] for details.
pub struct PollFn<T, P, O>
where
    T: Unpin,
    P: Unpin,
    P: FnMut(&mut T, &mut Context<'_>) -> Poll<O>,
{
    value: Option<T>,
    poll: P,
}

impl<T, P, O> Future for PollFn<T, P, O>
where
    T: Unpin,
    P: Unpin,
    P: FnMut(&mut T, &mut Context<'_>) -> Poll<O>,
{
    type Output = O;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = &mut *self.as_mut();

        let inner = match this.value.as_mut() {
            Some(inner) => inner,
            None => return Poll::Pending,
        };

        let value = match (this.poll)(inner, cx) {
            Poll::Ready(value) => value,
            Poll::Pending => return Poll::Pending,
        };

        Poll::Ready(value)
    }
}

impl<T, P, O> PollFn<T, P, O>
where
    T: Unpin,
    P: Unpin,
    P: FnMut(&mut T, &mut Context<'_>) -> Poll<O>,
{
    /// Set the fused value to be something else. The previous value will be
    /// dropped.
    ///
    /// The signature of this function is optimized towards being pinned.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tokio::time;
    /// use std::time::Duration;
    ///
    /// # #[tokio::main]
    /// # async fn main() {
    /// let mut interval = async_fuse::poll_fn(time::interval(Duration::from_millis(200)), time::Interval::poll_tick);
    ///
    /// interval.set(time::interval(Duration::from_secs(10)));
    /// # }
    /// ```
    pub fn set(&mut self, value: T) {
        self.value = Some(value);
    }

    /// Clear the fused value.
    ///
    /// This will cause the old value to be dropped if present.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tokio::time;
    /// use std::time::Duration;
    ///
    /// # #[tokio::main]
    /// # async fn main() {
    /// let mut interval = async_fuse::poll_fn(time::interval(Duration::from_millis(200)), time::Interval::poll_tick);
    ///
    /// assert!(!interval.is_empty());
    /// interval.clear();
    /// assert!(interval.is_empty());
    /// # }
    /// ```
    pub fn clear(&mut self) {
        self.value = None;
    }

    /// Test if the polled for value is empty.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tokio::time;
    /// use std::time::Duration;
    ///
    /// # #[tokio::main]
    /// # async fn main() {
    /// let mut interval = async_fuse::poll_fn(time::interval(Duration::from_millis(200)), time::Interval::poll_tick);
    ///
    /// assert!(!interval.is_empty());
    /// interval.clear();
    /// assert!(interval.is_empty());
    /// # }
    /// ```
    pub fn is_empty(&self) -> bool {
        self.value.is_none()
    }
}