Skip to main content

bbqueue/traits/notifier/
maitake.rs

1//! A Maitake-Sync based notifications
2
3use const_init::ConstInit;
4use maitake_sync::WaitCell;
5
6use super::{AsyncNotifier, Notifier};
7
8/// A Maitake-Sync based SPSC notifier
9///
10/// Usable for async context. Should not be used with multiple consumers or multiple producers
11/// at the same time.
12pub struct MaiNotSpsc {
13    not_empty: WaitCell,
14    not_full: WaitCell,
15}
16
17impl MaiNotSpsc {
18    /// Create a new Maitake-Sync based notifier
19    pub const fn new() -> Self {
20        Self::INIT
21    }
22}
23
24impl Default for MaiNotSpsc {
25    fn default() -> Self {
26        Self::new()
27    }
28}
29
30impl ConstInit for MaiNotSpsc {
31    #[allow(clippy::declare_interior_mutable_const)]
32    const INIT: Self = Self {
33        not_empty: WaitCell::new(),
34        not_full: WaitCell::new(),
35    };
36}
37
38impl Notifier for MaiNotSpsc {
39    fn wake_one_consumer(&self) {
40        _ = self.not_empty.wake();
41    }
42
43    fn wake_one_producer(&self) {
44        _ = self.not_full.wake();
45    }
46}
47
48impl AsyncNotifier for MaiNotSpsc {
49    async fn wait_for_not_empty<T, F: FnMut() -> Option<T>>(&self, f: F) -> T {
50        self.not_empty.wait_for_value(f).await.unwrap()
51    }
52
53    async fn wait_for_not_full<T, F: FnMut() -> Option<T>>(&self, f: F) -> T {
54        self.not_full.wait_for_value(f).await.unwrap()
55    }
56}