aboutsummaryrefslogtreecommitdiff
path: root/embassy-sync/src/pubsub/mod.rs
diff options
context:
space:
mode:
authorNathan Perry <[email protected]>2024-09-20 01:52:59 -0400
committerNathan Perry <[email protected]>2024-09-20 01:58:03 -0400
commit89bad07e817dec482d385f765da5be3b6d2d0e4c (patch)
tree7771cd82627973d7ac8787e426d96d0f1ff7a875 /embassy-sync/src/pubsub/mod.rs
parentd7780fcf83a8a35d7b09e5df7b62dd8218436715 (diff)
embassy_sync: `Sink` adapter for `pubsub::Pub`
Corresponding to the `Stream` impl for `pubsub::Sub`. Notable difference is that we need a separate adapter type to store the pending item, i.e. we can't `impl Sink for Pub` directly. Instead a method `Pub::sink(&self)` is exposed, which constructs a `PubSink`.
Diffstat (limited to 'embassy-sync/src/pubsub/mod.rs')
-rw-r--r--embassy-sync/src/pubsub/mod.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/embassy-sync/src/pubsub/mod.rs b/embassy-sync/src/pubsub/mod.rs
index 812302e2b..1c0f68bd0 100644
--- a/embassy-sync/src/pubsub/mod.rs
+++ b/embassy-sync/src/pubsub/mod.rs
@@ -755,4 +755,30 @@ mod tests {
755 assert_eq!(1, sub0.try_next_message_pure().unwrap().0); 755 assert_eq!(1, sub0.try_next_message_pure().unwrap().0);
756 assert_eq!(0, sub1.try_next_message_pure().unwrap().0); 756 assert_eq!(0, sub1.try_next_message_pure().unwrap().0);
757 } 757 }
758
759 #[futures_test::test]
760 async fn publisher_sink() {
761 use futures_util::{SinkExt, StreamExt};
762
763 let channel = PubSubChannel::<NoopRawMutex, u32, 4, 4, 4>::new();
764
765 let mut sub = channel.subscriber().unwrap();
766
767 let publ = channel.publisher().unwrap();
768 let mut sink = publ.sink();
769
770 sink.send(0).await.unwrap();
771 assert_eq!(0, sub.try_next_message_pure().unwrap());
772
773 sink.send(1).await.unwrap();
774 assert_eq!(1, sub.try_next_message_pure().unwrap());
775
776 sink.send_all(&mut futures_util::stream::iter(0..4).map(Ok))
777 .await
778 .unwrap();
779 assert_eq!(0, sub.try_next_message_pure().unwrap());
780 assert_eq!(1, sub.try_next_message_pure().unwrap());
781 assert_eq!(2, sub.try_next_message_pure().unwrap());
782 assert_eq!(3, sub.try_next_message_pure().unwrap());
783 }
758} 784}