|
| 1 | +use core::{ |
| 2 | + future::Future, |
| 3 | + task::{Poll, Waker}, |
| 4 | +}; |
| 5 | + |
| 6 | +use crate::{ |
| 7 | + async_impl::ssq::{WakerConsumer, WakerProducer}, |
| 8 | + spsc::Producer as HProducer, |
| 9 | +}; |
| 10 | + |
| 11 | +/// An async producer |
| 12 | +pub struct Producer<'queue, T, const N: usize> |
| 13 | +where |
| 14 | + T: Unpin, |
| 15 | +{ |
| 16 | + inner: HProducer<'queue, T, N>, |
| 17 | + producer_waker: WakerProducer<'queue>, |
| 18 | + consumer_waker: WakerConsumer<'queue>, |
| 19 | +} |
| 20 | + |
| 21 | +impl<'queue, T, const N: usize> Producer<'queue, T, N> |
| 22 | +where |
| 23 | + T: Unpin, |
| 24 | +{ |
| 25 | + pub(crate) fn new( |
| 26 | + producer: HProducer<'queue, T, N>, |
| 27 | + producer_waker: WakerProducer<'queue>, |
| 28 | + consumer_waker: WakerConsumer<'queue>, |
| 29 | + ) -> Self { |
| 30 | + Self { |
| 31 | + inner: producer, |
| 32 | + producer_waker, |
| 33 | + consumer_waker, |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /// Check if an item can be enqueued. |
| 38 | + /// |
| 39 | + /// If this returns true, at least the first subsequent [`Self::enqueue`] will succeed |
| 40 | + /// immediately. |
| 41 | + pub fn ready(&self) -> bool { |
| 42 | + self.inner.ready() |
| 43 | + } |
| 44 | + |
| 45 | + /// Returns the maximum number of elements the queue can hold. |
| 46 | + pub fn capacity(&self) -> usize { |
| 47 | + self.inner.capacity() |
| 48 | + } |
| 49 | + |
| 50 | + /// Returns the amount of elements currently in the queue. |
| 51 | + pub fn len(&self) -> usize { |
| 52 | + self.inner.len() |
| 53 | + } |
| 54 | + |
| 55 | + /// Enqueue `value` into the backing queue. |
| 56 | + /// |
| 57 | + /// The returned Future only resolves once the value was |
| 58 | + /// succesfully enqueued. |
| 59 | + pub fn enqueue<'me>(&'me mut self, value: T) -> ProducerFuture<'me, 'queue, T, N> { |
| 60 | + let value = self.inner.enqueue(value).err(); |
| 61 | + ProducerFuture { |
| 62 | + producer: self, |
| 63 | + value_to_enqueue: value, |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /// Try to enqueue `value` into the backing queue. |
| 68 | + pub fn try_enqueue(&mut self, value: T) -> Result<(), T> { |
| 69 | + self.inner.enqueue(value) |
| 70 | + } |
| 71 | + |
| 72 | + /// Try to wake the [`Consumer`](super::Consumer) associated with the backing queue if |
| 73 | + /// it is waiting to be awoken. |
| 74 | + fn wake_consumer(&mut self) { |
| 75 | + self.consumer_waker.dequeue().map(|v| v.wake()); |
| 76 | + } |
| 77 | + |
| 78 | + /// Register `waker` as the waker for this [`Producer`] |
| 79 | + fn register_waker<'v>(&mut self, waker: Waker) { |
| 80 | + // We can safely overwrite the old waker, as we can only ever have 1 instance |
| 81 | + // of `self` waiting to be awoken. |
| 82 | + self.producer_waker.enqueue(waker); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +pub struct ProducerFuture<'producer, 'queue, T, const N: usize> |
| 87 | +where |
| 88 | + T: Unpin, |
| 89 | +{ |
| 90 | + producer: &'producer mut Producer<'queue, T, N>, |
| 91 | + value_to_enqueue: Option<T>, |
| 92 | +} |
| 93 | + |
| 94 | +impl<T, const N: usize> Future for ProducerFuture<'_, '_, T, N> |
| 95 | +where |
| 96 | + T: Unpin, |
| 97 | +{ |
| 98 | + type Output = (); |
| 99 | + |
| 100 | + fn poll( |
| 101 | + self: core::pin::Pin<&mut Self>, |
| 102 | + cx: &mut core::task::Context<'_>, |
| 103 | + ) -> Poll<Self::Output> { |
| 104 | + let try_wake_consumer = |me: &mut Self| { |
| 105 | + me.producer.wake_consumer(); |
| 106 | + Poll::Ready(()) |
| 107 | + }; |
| 108 | + |
| 109 | + let me = self.get_mut(); |
| 110 | + let prod = &mut me.producer; |
| 111 | + let val_to_enqueue = &mut me.value_to_enqueue; |
| 112 | + |
| 113 | + let value = if let Some(value) = val_to_enqueue.take() { |
| 114 | + value |
| 115 | + } else { |
| 116 | + // Try to wake the consumer because we've enqueued our value |
| 117 | + return try_wake_consumer(me); |
| 118 | + }; |
| 119 | + |
| 120 | + let failed_enqueue_value = if let Some(value) = prod.inner.enqueue(value).err() { |
| 121 | + value |
| 122 | + } else { |
| 123 | + // Try to wake the consumer because we've enqueued our value |
| 124 | + return try_wake_consumer(me); |
| 125 | + }; |
| 126 | + |
| 127 | + me.value_to_enqueue = Some(failed_enqueue_value); |
| 128 | + |
| 129 | + me.producer.register_waker(cx.waker().clone()); |
| 130 | + |
| 131 | + Poll::Pending |
| 132 | + } |
| 133 | +} |
0 commit comments