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
use std::{fmt::Debug, future::Future, sync::Arc, task::Poll};

use futures::task::AtomicWaker;

use crate::queues::spsc::{DequeueError, EnqueueError};

use super::{BoundedReceiver, BoundedSender};

/// An async variant of the [`BoundedSender`] that allows your to efficiently
/// use this Queue in async Contexts as well.
///
/// Created using the [`async_queue`] method
pub struct AsyncBoundedSender<T> {
    rx_waker: Arc<AtomicWaker>,
    tx_waker: Arc<AtomicWaker>,
    queue: BoundedSender<T>,
}

/// An async variant of the [`BoundedReceiver`] that allows your to efficiently
/// use this Queue in async Contexts as well.
///
/// Created using the [`async_queue`] method
pub struct AsyncBoundedReceiver<T> {
    rx_waker: Arc<AtomicWaker>,
    tx_waker: Arc<AtomicWaker>,
    queue: BoundedReceiver<T>,
}

/// The Future returned when enqueueing an Item
///
/// # Behaviour
/// This Future only resolves when it either successfully enqueued the Item
/// in the Queue (`Ok`) or when the Queue gets closed by the Consumer and
/// therefore no more Items can be enqueued into it (`Err`)
pub struct EnqueueFuture<'queue, T> {
    /// The Waker to notify a potential waiting Dequeue-Operation
    rx_waker: &'queue AtomicWaker,
    /// The Waker for this type of Future
    tx_waker: &'queue AtomicWaker,
    /// The actual underlying Queue
    queue: &'queue mut BoundedSender<T>,
    /// The Data that the User wants to enqueue
    data: Option<T>,
}

/// The Future returned when dequeue an Item
///
/// # Behaviour
/// This Future only resolves when it either successfully dequeued an Item
/// and then returns it (`Ok(item)`) or when the Queue was closed by the Producer
/// and there are no more Items left in it to be dequeued (`Err`)
pub struct DequeueFuture<'queue, T> {
    /// The Waker for this type of Future
    rx_waker: &'queue AtomicWaker,
    /// The Waker to notify a potential waiting Enqueue-Operation
    tx_waker: &'queue AtomicWaker,
    /// The actual underlying Queue
    queue: &'queue mut BoundedReceiver<T>,
}

impl<T> AsyncBoundedSender<T> {
    /// Checks if the Queue has been closed by the Consumer
    pub fn is_closed(&self) -> bool {
        self.queue.is_closed()
    }

    /// The async variant of the blocking [`enqueue`](BoundedSender::enqueue)
    /// operation on the Non-Async version of the Queue
    pub fn enqueue<'queue>(&'queue mut self, data: T) -> EnqueueFuture<'queue, T> {
        EnqueueFuture {
            rx_waker: &self.rx_waker,
            tx_waker: &self.tx_waker,
            queue: &mut self.queue,
            data: Some(data),
        }
    }

    /// Attempts to enqueue the given Data on the Queue.
    ///
    /// This behaves just like the [`try_enqueue`](BoundedSender::try_enqueue)
    /// operation on the normal sync-BoundedSender
    pub fn try_enqueue(&mut self, data: T) -> Result<(), (T, EnqueueError)> {
        match self.queue.try_enqueue(data) {
            Ok(_) => {
                self.rx_waker.wake();
                Ok(())
            }
            Err(e) => Err(e),
        }
    }

    /// Checks if the Queue is currently Full
    pub fn is_full(&self) -> bool {
        self.queue.is_full()
    }
}

impl<T> Debug for AsyncBoundedSender<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Async-Bounded-Sender ()")
    }
}

impl<T> AsyncBoundedReceiver<T> {
    /// Checks if the Queue has been closed by the Producer
    ///
    /// # Note
    /// Even when this indicates that the Queue is closed, there might still be
    /// Items left in the Queue that the Consumer should dequeue first to make
    /// sure that no data is lost
    pub fn is_closed(&self) -> bool {
        self.queue.is_closed()
    }

    /// The async variant of the blocking [`dequeue`](BoundedReceiver::dequeue)
    /// operation on the Non-Async version of the Queue
    pub fn dequeue<'queue>(&'queue mut self) -> DequeueFuture<'queue, T> {
        DequeueFuture {
            rx_waker: &self.rx_waker,
            tx_waker: &self.tx_waker,
            queue: &mut self.queue,
        }
    }

    /// Attempts to dequeue a single Item from the Queue.
    ///
    /// This behaves just like the non-async
    /// [`try_dequeue`](BoundedReceiver::try_dequeue) operation
    pub fn try_dequeue(&mut self) -> Result<T, DequeueError> {
        match self.queue.try_dequeue() {
            Ok(d) => {
                self.tx_waker.wake();
                Ok(d)
            }
            Err(e) => Err(e),
        }
    }

    /// Checks if the Queue is currently Empty
    pub fn is_empty(&self) -> bool {
        self.queue.is_empty()
    }
}

impl<T> Debug for AsyncBoundedReceiver<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Async-Bounded-Receiver ()")
    }
}

impl<'queue, T> Unpin for EnqueueFuture<'queue, T> {}

impl<'queue, T> Future for EnqueueFuture<'queue, T> {
    type Output = Result<(), (T, EnqueueError)>;

    fn poll(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Self::Output> {
        let data = match self.data.take() {
            Some(d) => d,
            None => return Poll::Ready(Ok(())),
        };

        match self.queue.try_enqueue(data) {
            Ok(_) => {
                self.rx_waker.wake();
                Poll::Ready(Ok(()))
            }
            Err((d, e)) => match e {
                EnqueueError::WouldBlock => {
                    self.data.replace(d);
                    self.tx_waker.register(cx.waker());

                    Poll::Pending
                }
                EnqueueError::Closed => Poll::Ready(Err((d, e))),
            },
        }
    }
}

impl<'queue, T> Debug for EnqueueFuture<'queue, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Enqueue-Future ()")
    }
}

impl<'queue, T> Unpin for DequeueFuture<'queue, T> {}

impl<'queue, T> Future for DequeueFuture<'queue, T> {
    type Output = Result<T, DequeueError>;

    fn poll(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> Poll<Self::Output> {
        match self.queue.try_dequeue() {
            Ok(d) => {
                self.tx_waker.wake();
                Poll::Ready(Ok(d))
            }
            Err(e) => match e {
                DequeueError::WouldBlock => {
                    self.rx_waker.register(cx.waker());
                    Poll::Pending
                }
                DequeueError::Closed => Poll::Ready(Err(DequeueError::Closed)),
            },
        }
    }
}

impl<'queue, T> Debug for DequeueFuture<'queue, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Dequeue-Future ()")
    }
}

/// Creates an async BoundedQueue and returns its respecitive
/// ([`AsyncBoundedReceiver`], [`AsyncBoundedSender`])
pub fn async_queue<T>(size: usize) -> (AsyncBoundedReceiver<T>, AsyncBoundedSender<T>) {
    let (u_rx, u_tx) = super::queue(size);

    let rx_waker = Arc::new(AtomicWaker::new());
    let tx_waker = Arc::new(AtomicWaker::new());

    (
        AsyncBoundedReceiver {
            rx_waker: rx_waker.clone(),
            tx_waker: tx_waker.clone(),
            queue: u_rx,
        },
        AsyncBoundedSender {
            rx_waker,
            tx_waker,
            queue: u_tx,
        },
    )
}

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

    #[tokio::test]
    async fn enqueue_dequeue() {
        let (mut rx, mut tx) = async_queue::<usize>(10);

        tx.enqueue(13).await.unwrap();
        assert_eq!(Ok(13), rx.dequeue().await);
    }
}