aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-sync/src/channel.rs106
1 files changed, 106 insertions, 0 deletions
diff --git a/embassy-sync/src/channel.rs b/embassy-sync/src/channel.rs
index a229c52e7..f97cb2b8e 100644
--- a/embassy-sync/src/channel.rs
+++ b/embassy-sync/src/channel.rs
@@ -164,6 +164,57 @@ impl<'ch, T> DynamicSender<'ch, T> {
164 } 164 }
165} 165}
166 166
167/// Send-only access to a [`Channel`] without knowing channel size.
168/// This version can be sent between threads but can only be created if the underlying mutex is Sync.
169pub struct SendDynamicSender<'ch, T> {
170 pub(crate) channel: &'ch dyn DynamicChannel<T>,
171}
172
173impl<'ch, T> Clone for SendDynamicSender<'ch, T> {
174 fn clone(&self) -> Self {
175 *self
176 }
177}
178
179impl<'ch, T> Copy for SendDynamicSender<'ch, T> {}
180unsafe impl<'ch, T: Send> Send for SendDynamicSender<'ch, T> {}
181unsafe impl<'ch, T: Send> Sync for SendDynamicSender<'ch, T> {}
182
183impl<'ch, M, T, const N: usize> From<Sender<'ch, M, T, N>> for SendDynamicSender<'ch, T>
184where
185 M: RawMutex + Sync + Send,
186{
187 fn from(s: Sender<'ch, M, T, N>) -> Self {
188 Self { channel: s.channel }
189 }
190}
191
192impl<'ch, T> SendDynamicSender<'ch, T> {
193 /// Sends a value.
194 ///
195 /// See [`Channel::send()`]
196 pub fn send(&self, message: T) -> DynamicSendFuture<'ch, T> {
197 DynamicSendFuture {
198 channel: self.channel,
199 message: Some(message),
200 }
201 }
202
203 /// Attempt to immediately send a message.
204 ///
205 /// See [`Channel::send()`]
206 pub fn try_send(&self, message: T) -> Result<(), TrySendError<T>> {
207 self.channel.try_send_with_context(message, None)
208 }
209
210 /// Allows a poll_fn to poll until the channel is ready to send
211 ///
212 /// See [`Channel::poll_ready_to_send()`]
213 pub fn poll_ready_to_send(&self, cx: &mut Context<'_>) -> Poll<()> {
214 self.channel.poll_ready_to_send(cx)
215 }
216}
217
167/// Receive-only access to a [`Channel`]. 218/// Receive-only access to a [`Channel`].
168pub struct Receiver<'ch, M, T, const N: usize> 219pub struct Receiver<'ch, M, T, const N: usize>
169where 220where
@@ -337,6 +388,61 @@ where
337 } 388 }
338} 389}
339 390
391/// Receive-only access to a [`Channel`] without knowing channel size.
392/// This version can be sent between threads but can only be created if the underlying mutex is Sync.
393pub struct SendableDynamicReceiver<'ch, T> {
394 pub(crate) channel: &'ch dyn DynamicChannel<T>,
395}
396
397impl<'ch, T> Clone for SendableDynamicReceiver<'ch, T> {
398 fn clone(&self) -> Self {
399 *self
400 }
401}
402
403impl<'ch, T> Copy for SendableDynamicReceiver<'ch, T> {}
404unsafe impl<'ch, T: Send> Send for SendableDynamicReceiver<'ch, T> {}
405unsafe impl<'ch, T: Send> Sync for SendableDynamicReceiver<'ch, T> {}
406
407impl<'ch, T> SendableDynamicReceiver<'ch, T> {
408 /// Receive the next value.
409 ///
410 /// See [`Channel::receive()`].
411 pub fn receive(&self) -> DynamicReceiveFuture<'_, T> {
412 DynamicReceiveFuture { channel: self.channel }
413 }
414
415 /// Attempt to immediately receive the next value.
416 ///
417 /// See [`Channel::try_receive()`]
418 pub fn try_receive(&self) -> Result<T, TryReceiveError> {
419 self.channel.try_receive_with_context(None)
420 }
421
422 /// Allows a poll_fn to poll until the channel is ready to receive
423 ///
424 /// See [`Channel::poll_ready_to_receive()`]
425 pub fn poll_ready_to_receive(&self, cx: &mut Context<'_>) -> Poll<()> {
426 self.channel.poll_ready_to_receive(cx)
427 }
428
429 /// Poll the channel for the next item
430 ///
431 /// See [`Channel::poll_receive()`]
432 pub fn poll_receive(&self, cx: &mut Context<'_>) -> Poll<T> {
433 self.channel.poll_receive(cx)
434 }
435}
436
437impl<'ch, M, T, const N: usize> From<Receiver<'ch, M, T, N>> for SendableDynamicReceiver<'ch, T>
438where
439 M: RawMutex + Sync + Send,
440{
441 fn from(s: Receiver<'ch, M, T, N>) -> Self {
442 Self { channel: s.channel }
443 }
444}
445
340impl<'ch, M, T, const N: usize> futures_util::Stream for Receiver<'ch, M, T, N> 446impl<'ch, M, T, const N: usize> futures_util::Stream for Receiver<'ch, M, T, N>
341where 447where
342 M: RawMutex, 448 M: RawMutex,