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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//! This crate contains two objects: [`SwitchingSleep`] and [`ASwitchingSleep`].
//!
//! [`ASwitchingSleep`] is just a wrapper around
//! [`Arc`](struct@std::sync::Arc)<[`RwLock`](struct@tokio::sync::RwLock)<[`SwitchingSleep`]>>.
//!
//! They are a [`tokio::time::Sleep`](struct@tokio::time::Sleep) with a
//! switchable state. When you call the [`start`] method a [`Sleep`] is created,
//! when you call the [`stop`] one the current [`Sleep`] is dropped. So calling
//! [`start`] will reset the timer.
//!
//! The timer will complete after the `duration` time since
//! [`start`] method is called (or [`new_start`], [`new`] + [`start`]).
//!
//! [SwitchingSleep]: struct@SwitchingSleep
//! [Sleep]: struct@tokio::time::Sleep
//! [`start`]: SwitchingSleep::start()
//! [`stop`]: SwitchingSleep::stop()
//! [`new_start`]: SwitchingSleep::new_start()
//! [`new`]: SwitchingSleep::new()

use std::{
    fmt::Debug,
    future::Future,
    pin::Pin,
    sync::Arc,
    task::{Context, Poll},
    time::Duration,
};

use tokio::{
    sync::{broadcast, RwLock},
    time::{sleep, Sleep},
};

/// The [`!Sync`][trait@std::marker::Sync] one.
#[derive(Debug)]
pub struct SwitchingSleep {
    period: Duration,
    tx: broadcast::Sender<()>,
    rx: broadcast::Receiver<()>,
    sleeper: Option<Sleep>,
}

impl Unpin for SwitchingSleep {}

impl SwitchingSleep {
    /// Create a new [`SwitchingSleep`] and doesn't start the timer.
    pub fn new(period: Duration) -> Self {
        let (tx, rx) = broadcast::channel(10);

        Self {
            period,
            tx,
            rx,
            sleeper: None,
        }
    }

    /// Create a new [`SwitchingSleep`] and start the timer.
    pub fn new_start(period: Duration) -> Self {
        let mut me = Self::new(period);
        me.start();
        me
    }

    /// Start the timer. Reset if already started.
    pub fn start(&mut self) {
        if !self.is_elapsed() {
            self.stop();

            self.sleeper = Some(sleep(self.period));
            self.tx.send(()).unwrap();
        }
    }

    /// Stop the timer. It doesn nothing if already stopped.
    pub fn stop(&mut self) {
        if !self.is_elapsed() {
            match self.sleeper.take() {
                Some(_) => {
                    self.tx.send(()).unwrap();
                }
                None => (),
            }
        }
    }

    /// Check if the timer (if any) is elapsed.
    pub fn is_elapsed(&self) -> bool {
        self.sleeper.is_some() && (&self.sleeper).as_ref().unwrap().is_elapsed()
    }
}

unsafe impl Send for SwitchingSleep {}

impl Future for SwitchingSleep {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<<Self as Future>::Output> {
        unsafe {
            let me = Pin::get_unchecked_mut(self);

            if me.is_elapsed() {
                return Poll::Ready(());
            }

            let sleeper = match me.sleeper {
                Some(ref mut sleeper) => {
                    let sleeper = Pin::new_unchecked(sleeper);

                    Some(sleeper.poll(cx))
                }
                None => None,
            };
            let mut recv = me.rx.recv();
            let recv = Pin::new_unchecked(&mut recv);
            let _ = recv.poll(cx);

            if let Some(Poll::Ready(_)) = sleeper {
                Poll::Ready(())
            } else {
                Poll::Pending
            }
        }
    }
}

/// The [`Sync`][trait@std::marker::Sync] one.
#[derive(Debug)]
pub struct ASwitchingSleep(Arc<RwLock<SwitchingSleep>>);

impl ASwitchingSleep {
    /// Create a new [`ASwitchingSleep`] and doesn't start the timer.
    pub fn new(period: Duration) -> Self {
        Self(Arc::new(RwLock::new(SwitchingSleep::new(period))))
    }

    /// Create a new [`ASwitchingSleep`] and start the timer.
    pub async fn new_start(period: Duration) -> Self {
        let me = Self::new(period);
        me.start().await;
        me
    }

    /// Start the timer. Reset if already started.
    pub async fn start(&self) {
        let mut inner = self.0.write().await;
        inner.start()
    }

    /// Stop the timer. It doesn nothing if already stopped.
    pub async fn stop(&self) {
        let mut inner = self.0.write().await;
        inner.stop()
    }

    /// Check if the timer (if any) is elapsed.
    pub async fn is_elapsed(&self) -> bool {
        let inner = self.0.read().await;
        inner.is_elapsed()
    }
}

unsafe impl Send for ASwitchingSleep {}
unsafe impl Sync for ASwitchingSleep {}
impl Unpin for ASwitchingSleep {}

impl Clone for ASwitchingSleep {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl Future for ASwitchingSleep {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<<Self as Future>::Output> {
        unsafe {
            let me = Pin::get_unchecked_mut(self);

            let mut inner = me.0.write();
            let inner = Pin::new_unchecked(&mut inner);

            match inner.poll(cx) {
                Poll::Pending => Poll::Pending,
                Poll::Ready(mut inner) => Pin::new_unchecked(&mut *inner).poll(cx),
            }
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::time::Duration;
    use tokio::{
        select,
        time::{sleep, Instant},
    };

    #[tokio::test]
    async fn it_works() {
        let mut sleeper = ASwitchingSleep::new(Duration::from_secs(3));

        let start = Instant::now();

        let mut task = {
            let sleeper = sleeper.clone();
            tokio::task::spawn(async move {
                sleep(Duration::from_secs(5)).await;

                assert_eq!(sleeper.is_elapsed().await, false);

                sleeper.start().await;

                sleep(Duration::from_secs(2)).await;

                assert_eq!(sleeper.is_elapsed().await, false);

                sleeper.stop().await;

                sleep(Duration::from_secs(2)).await;

                assert_eq!(sleeper.is_elapsed().await, false);

                sleeper.start().await;

                sleep(Duration::from_secs(2)).await;

                assert_eq!(sleeper.is_elapsed().await, false);
            })
        };

        loop {
            select! {
                _ = &mut task => {
                    loop {
                        select! {
                            _ = &mut sleeper => {
                                break;
                            }
                        }
                    }
                    break;
                },
                _ = &mut sleeper => break,
            }
        }

        let stop = Instant::now();
        let diff = stop - start;

        assert_eq!(sleeper.is_elapsed().await, true);
        assert_eq!(diff.as_secs(), 12);
    }
}