diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-03-03 20:57:49 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-03-03 20:57:49 +0000 |
| commit | 20760ff4f7dcbca8b1654957462df4551883932d (patch) | |
| tree | c0b9fa4a6e9ca1ffcc14993694fe8153406f9172 | |
| parent | 0cd293acc2ef9c957c0c3dfb7ee9956cdbfc6610 (diff) | |
| parent | 69d37503c22be73c3d8283f39155cfa1559a37eb (diff) | |
Merge pull request #2648 from peterkrull/dyn-channel-constructor
Add constructor for dynamic channel
| -rw-r--r-- | embassy-sync/src/channel.rs | 22 |
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(); |
