aboutsummaryrefslogtreecommitdiff
path: root/embassy-sync/src
diff options
context:
space:
mode:
authorPeter Krull <[email protected]>2024-03-02 13:13:26 +0100
committerPeter Krull <[email protected]>2024-03-02 13:13:26 +0100
commit69d37503c22be73c3d8283f39155cfa1559a37eb (patch)
treed997a721b3e596bac83747211f6cdb15b3450346 /embassy-sync/src
parent64890498ca6d6193ca0ac30952c24a657b8b88f3 (diff)
Add constructor for dynamic channel
Diffstat (limited to 'embassy-sync/src')
-rw-r--r--embassy-sync/src/channel.rs22
1 files changed, 21 insertions, 1 deletions
diff --git a/embassy-sync/src/channel.rs b/embassy-sync/src/channel.rs
index ff7129303..01db0d09a 100644
--- a/embassy-sync/src/channel.rs
+++ b/embassy-sync/src/channel.rs
@@ -507,6 +507,16 @@ where
507 Receiver { channel: self } 507 Receiver { channel: self }
508 } 508 }
509 509
510 /// Get a sender for this channel using dynamic dispatch.
511 pub fn dyn_sender(&self) -> DynamicSender<'_, T> {
512 DynamicSender { channel: self }
513 }
514
515 /// Get a receiver for this channel using dynamic dispatch.
516 pub fn dyn_receiver(&self) -> DynamicReceiver<'_, T> {
517 DynamicReceiver { channel: self }
518 }
519
510 /// Send a value, waiting until there is capacity. 520 /// Send a value, waiting until there is capacity.
511 /// 521 ///
512 /// Sending completes when the value has been pushed to the channel's queue. 522 /// Sending completes when the value has been pushed to the channel's queue.
@@ -648,7 +658,7 @@ mod tests {
648 } 658 }
649 659
650 #[test] 660 #[test]
651 fn dynamic_dispatch() { 661 fn dynamic_dispatch_into() {
652 let c = Channel::<NoopRawMutex, u32, 3>::new(); 662 let c = Channel::<NoopRawMutex, u32, 3>::new();
653 let s: DynamicSender<'_, u32> = c.sender().into(); 663 let s: DynamicSender<'_, u32> = c.sender().into();
654 let r: DynamicReceiver<'_, u32> = c.receiver().into(); 664 let r: DynamicReceiver<'_, u32> = c.receiver().into();
@@ -657,6 +667,16 @@ mod tests {
657 assert_eq!(r.try_receive().unwrap(), 1); 667 assert_eq!(r.try_receive().unwrap(), 1);
658 } 668 }
659 669
670 #[test]
671 fn dynamic_dispatch_constructor() {
672 let c = Channel::<NoopRawMutex, u32, 3>::new();
673 let s = c.dyn_sender();
674 let r = c.dyn_receiver();
675
676 assert!(s.try_send(1).is_ok());
677 assert_eq!(r.try_receive().unwrap(), 1);
678 }
679
660 #[futures_test::test] 680 #[futures_test::test]
661 async fn receiver_receives_given_try_send_async() { 681 async fn receiver_receives_given_try_send_async() {
662 let executor = ThreadPool::new().unwrap(); 682 let executor = ThreadPool::new().unwrap();