1use core::future::Future;
4use core::marker::PhantomData;
5use core::ops::{Deref, DerefMut};
6use core::pin::Pin;
7use core::task::{Context, Poll};
8
9use super::{PubSubBehavior, PubSubChannel};
10use crate::blocking_mutex::raw::RawMutex;
11
12pub struct Pub<'a, PSB: PubSubBehavior<T> + ?Sized, T: Clone> {
14 channel: &'a PSB,
16 _phantom: PhantomData<T>,
17}
18
19impl<'a, PSB: PubSubBehavior<T> + ?Sized, T: Clone> Pub<'a, PSB, T> {
20 pub(super) fn new(channel: &'a PSB) -> Self {
21 Self {
22 channel,
23 _phantom: Default::default(),
24 }
25 }
26
27 pub fn publish_immediate(&self, message: T) {
30 self.channel.publish_immediate(message)
31 }
32
33 pub fn publish<'s>(&'s self, message: T) -> PublisherWaitFuture<'s, 'a, PSB, T> {
35 PublisherWaitFuture {
36 message: Some(message),
37 publisher: self,
38 }
39 }
40
41 pub fn try_publish(&self, message: T) -> Result<(), T> {
43 self.channel.publish_with_context(message, None)
44 }
45
46 pub fn capacity(&self) -> usize {
48 self.channel.capacity()
49 }
50
51 pub fn free_capacity(&self) -> usize {
55 self.channel.free_capacity()
56 }
57
58 pub fn clear(&self) {
60 self.channel.clear();
61 }
62
63 pub fn len(&self) -> usize {
65 self.channel.len()
66 }
67
68 pub fn is_empty(&self) -> bool {
70 self.channel.is_empty()
71 }
72
73 pub fn is_full(&self) -> bool {
75 self.channel.is_full()
76 }
77
78 #[inline]
80 pub const fn sink(&self) -> PubSink<'a, '_, PSB, T> {
81 PubSink { publ: self, fut: None }
82 }
83}
84
85impl<'a, PSB: PubSubBehavior<T> + ?Sized, T: Clone> Drop for Pub<'a, PSB, T> {
86 fn drop(&mut self) {
87 self.channel.unregister_publisher()
88 }
89}
90
91pub struct DynPublisher<'a, T: Clone>(pub(super) Pub<'a, dyn PubSubBehavior<T> + 'a, T>);
93
94impl<'a, T: Clone> Deref for DynPublisher<'a, T> {
95 type Target = Pub<'a, dyn PubSubBehavior<T> + 'a, T>;
96
97 fn deref(&self) -> &Self::Target {
98 &self.0
99 }
100}
101
102impl<'a, T: Clone> DerefMut for DynPublisher<'a, T> {
103 fn deref_mut(&mut self) -> &mut Self::Target {
104 &mut self.0
105 }
106}
107
108pub struct Publisher<'a, M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usize>(
110 pub(super) Pub<'a, PubSubChannel<M, T, CAP, SUBS, PUBS>, T>,
111);
112
113impl<'a, M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usize> Deref
114 for Publisher<'a, M, T, CAP, SUBS, PUBS>
115{
116 type Target = Pub<'a, PubSubChannel<M, T, CAP, SUBS, PUBS>, T>;
117
118 fn deref(&self) -> &Self::Target {
119 &self.0
120 }
121}
122
123impl<'a, M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usize> DerefMut
124 for Publisher<'a, M, T, CAP, SUBS, PUBS>
125{
126 fn deref_mut(&mut self) -> &mut Self::Target {
127 &mut self.0
128 }
129}
130
131pub struct ImmediatePub<'a, PSB: PubSubBehavior<T> + ?Sized, T: Clone> {
134 channel: &'a PSB,
136 _phantom: PhantomData<T>,
137}
138
139impl<'a, PSB: PubSubBehavior<T> + ?Sized, T: Clone> ImmediatePub<'a, PSB, T> {
140 pub(super) fn new(channel: &'a PSB) -> Self {
141 Self {
142 channel,
143 _phantom: Default::default(),
144 }
145 }
146 pub fn publish_immediate(&self, message: T) {
149 self.channel.publish_immediate(message)
150 }
151
152 pub fn try_publish(&self, message: T) -> Result<(), T> {
154 self.channel.publish_with_context(message, None)
155 }
156
157 pub fn capacity(&self) -> usize {
159 self.channel.capacity()
160 }
161
162 pub fn free_capacity(&self) -> usize {
166 self.channel.free_capacity()
167 }
168
169 pub fn clear(&self) {
171 self.channel.clear();
172 }
173
174 pub fn len(&self) -> usize {
176 self.channel.len()
177 }
178
179 pub fn is_empty(&self) -> bool {
181 self.channel.is_empty()
182 }
183
184 pub fn is_full(&self) -> bool {
186 self.channel.is_full()
187 }
188}
189
190pub struct DynImmediatePublisher<'a, T: Clone>(pub(super) ImmediatePub<'a, dyn PubSubBehavior<T> + 'a, T>);
192
193impl<'a, T: Clone> Deref for DynImmediatePublisher<'a, T> {
194 type Target = ImmediatePub<'a, dyn PubSubBehavior<T> + 'a, T>;
195
196 fn deref(&self) -> &Self::Target {
197 &self.0
198 }
199}
200
201impl<'a, T: Clone> DerefMut for DynImmediatePublisher<'a, T> {
202 fn deref_mut(&mut self) -> &mut Self::Target {
203 &mut self.0
204 }
205}
206
207pub struct ImmediatePublisher<'a, M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usize>(
209 pub(super) ImmediatePub<'a, PubSubChannel<M, T, CAP, SUBS, PUBS>, T>,
210);
211
212impl<'a, M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usize> Deref
213 for ImmediatePublisher<'a, M, T, CAP, SUBS, PUBS>
214{
215 type Target = ImmediatePub<'a, PubSubChannel<M, T, CAP, SUBS, PUBS>, T>;
216
217 fn deref(&self) -> &Self::Target {
218 &self.0
219 }
220}
221
222impl<'a, M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usize> DerefMut
223 for ImmediatePublisher<'a, M, T, CAP, SUBS, PUBS>
224{
225 fn deref_mut(&mut self) -> &mut Self::Target {
226 &mut self.0
227 }
228}
229
230#[must_use = "Sinks do nothing unless polled"]
231pub struct PubSink<'a, 'p, PSB, T>
233where
234 T: Clone,
235 PSB: PubSubBehavior<T> + ?Sized,
236{
237 publ: &'p Pub<'a, PSB, T>,
238 fut: Option<PublisherWaitFuture<'p, 'a, PSB, T>>,
239}
240
241impl<'a, 'p, PSB, T> PubSink<'a, 'p, PSB, T>
242where
243 PSB: PubSubBehavior<T> + ?Sized,
244 T: Clone,
245{
246 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
248 let Some(mut fut) = self.fut.take() else {
249 return Poll::Ready(());
250 };
251
252 if Pin::new(&mut fut).poll(cx).is_pending() {
253 self.fut = Some(fut);
254 return Poll::Pending;
255 }
256
257 Poll::Ready(())
258 }
259}
260
261impl<'a, 'p, PSB, T> futures_sink::Sink<T> for PubSink<'a, 'p, PSB, T>
262where
263 PSB: PubSubBehavior<T> + ?Sized,
264 T: Clone,
265{
266 type Error = core::convert::Infallible;
267
268 #[inline]
269 fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
270 self.poll(cx).map(Ok)
271 }
272
273 #[inline]
274 fn start_send(mut self: Pin<&mut Self>, item: T) -> Result<(), Self::Error> {
275 self.fut = Some(self.publ.publish(item));
276
277 Ok(())
278 }
279
280 #[inline]
281 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
282 self.poll(cx).map(Ok)
283 }
284
285 #[inline]
286 fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
287 self.poll(cx).map(Ok)
288 }
289}
290
291#[must_use = "futures do nothing unless you `.await` or poll them"]
293pub struct PublisherWaitFuture<'s, 'a, PSB: PubSubBehavior<T> + ?Sized, T: Clone> {
294 message: Option<T>,
296 publisher: &'s Pub<'a, PSB, T>,
297}
298
299impl<'s, 'a, PSB: PubSubBehavior<T> + ?Sized, T: Clone> Future for PublisherWaitFuture<'s, 'a, PSB, T> {
300 type Output = ();
301
302 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
303 let message = self.message.take().unwrap();
304 match self.publisher.channel.publish_with_context(message, Some(cx)) {
305 Ok(()) => Poll::Ready(()),
306 Err(message) => {
307 self.message = Some(message);
308 Poll::Pending
309 }
310 }
311 }
312}
313
314impl<'s, 'a, PSB: PubSubBehavior<T> + ?Sized, T: Clone> Unpin for PublisherWaitFuture<'s, 'a, PSB, T> {}