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
use std::sync::{
    atomic::{AtomicBool, Ordering},
    Arc,
};
use tokio::sync::Notify;

/// A bool one can `await` the changes.
#[derive(Clone)]
pub struct FutureBool {
    notify_true: Arc<Notify>,
    notify_false: Arc<Notify>,
    inner: Arc<AtomicBool>,
}

impl FutureBool {
    /// Creates a new `FutureBool` with some initial value.
    pub fn new(val: bool) -> Self {
        Self {
            notify_true: Arc::new(Notify::new()),
            notify_false: Arc::new(Notify::new()),
            inner: Arc::new(AtomicBool::new(val)),
        }
    }

    /// Sets the `bool` value to `true`.
    pub fn set(&self) {
        self.inner.store(true, Ordering::Release);
        self.notify_true.notify_waiters();
    }

    /// Sets the `bool` value to `false`.
    pub fn unset(&self) {
        self.inner.store(false, Ordering::Release);
        self.notify_false.notify_waiters();
    }

    /// Returns the new value when it has changed.
    pub async fn wait_change(&self) -> bool {
        let val = self.inner.load(Ordering::Acquire);
        if val {
            self.notify_false.notified().await;
        } else {
            self.notify_true.notified().await;
        }
        !val
    }

    /// If the value is `true`, returns immidiately, otherwise waits until it's `true`.
    pub async fn wait_true(&self) {
        let val = self.inner.load(Ordering::Acquire);
        if !val {
            self.notify_true.notified().await;
        }
    }

    /// If the value is `false`, returns immidiately, otherwise waits until it's `false`.
    pub async fn wait_false(&self) {
        let val = self.inner.load(Ordering::Acquire);
        if val {
            self.notify_false.notified().await;
        }
    }
}