From 9206584aa9af14a7fee8af4d07be096f8c7facce Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Thu, 7 Apr 2022 15:15:44 +0200 Subject: [PATCH] Add back support for cloning sender/receiver * Remove level of import indirection for Channel and Signal. --- embassy/src/channel/channel.rs | 36 ++++++++++++++++++++++++++++++++-- embassy/src/channel/mod.rs | 3 +++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/embassy/src/channel/channel.rs b/embassy/src/channel/channel.rs index 9084cd57b..d749e5971 100644 --- a/embassy/src/channel/channel.rs +++ b/embassy/src/channel/channel.rs @@ -28,7 +28,7 @@ use crate::blocking_mutex::Mutex; use crate::waitqueue::WakerRegistration; /// Send-only access to a [`Channel`]. -#[derive(Copy, Clone)] +#[derive(Copy)] pub struct Sender<'ch, M, T, const N: usize> where M: RawMutex, @@ -36,6 +36,17 @@ where channel: &'ch Channel, } +impl<'ch, M, T, const N: usize> Clone for Sender<'ch, M, T, N> +where + M: RawMutex, +{ + fn clone(&self) -> Self { + Sender { + channel: self.channel, + } + } +} + impl<'ch, M, T, const N: usize> Sender<'ch, M, T, N> where M: RawMutex, @@ -56,7 +67,7 @@ where } /// Receive-only access to a [`Channel`]. -#[derive(Copy, Clone)] +#[derive(Copy)] pub struct Receiver<'ch, M, T, const N: usize> where M: RawMutex, @@ -64,6 +75,17 @@ where channel: &'ch Channel, } +impl<'ch, M, T, const N: usize> Clone for Receiver<'ch, M, T, N> +where + M: RawMutex, +{ + fn clone(&self) -> Self { + Receiver { + channel: self.channel, + } + } +} + impl<'ch, M, T, const N: usize> Receiver<'ch, M, T, N> where M: RawMutex, @@ -379,6 +401,16 @@ mod tests { assert_eq!(c.try_recv().unwrap(), 1); } + #[test] + fn cloning() { + let c = Channel::::new(); + let r1 = c.receiver(); + let s1 = c.sender(); + + let _ = r1.clone(); + let _ = s1.clone(); + } + #[futures_test::test] async fn receiver_receives_given_try_send_async() { let executor = ThreadPool::new().unwrap(); diff --git a/embassy/src/channel/mod.rs b/embassy/src/channel/mod.rs index e51a442df..4d37fe859 100644 --- a/embassy/src/channel/mod.rs +++ b/embassy/src/channel/mod.rs @@ -1,4 +1,7 @@ //! Async channels pub mod channel; +pub use channel::*; + pub mod signal; +pub use signal::*;