diff options
| author | Ulf Lilleengen <[email protected]> | 2022-04-07 15:15:44 +0200 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2022-04-07 15:15:44 +0200 |
| commit | 9206584aa9af14a7fee8af4d07be096f8c7facce (patch) | |
| tree | cfb5b09b9e3e42d99fbec78bdc251ec8d1513c8f | |
| parent | fee0aef076718adcb2c654920712aa20786c94be (diff) | |
Add back support for cloning sender/receiver
* Remove level of import indirection for Channel and Signal.
| -rw-r--r-- | embassy/src/channel/channel.rs | 36 | ||||
| -rw-r--r-- | 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; | |||
| 28 | use crate::waitqueue::WakerRegistration; | 28 | use crate::waitqueue::WakerRegistration; |
| 29 | 29 | ||
| 30 | /// Send-only access to a [`Channel`]. | 30 | /// Send-only access to a [`Channel`]. |
| 31 | #[derive(Copy, Clone)] | 31 | #[derive(Copy)] |
| 32 | pub struct Sender<'ch, M, T, const N: usize> | 32 | pub struct Sender<'ch, M, T, const N: usize> |
| 33 | where | 33 | where |
| 34 | M: RawMutex, | 34 | M: RawMutex, |
| @@ -36,6 +36,17 @@ where | |||
| 36 | channel: &'ch Channel<M, T, N>, | 36 | channel: &'ch Channel<M, T, N>, |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | impl<'ch, M, T, const N: usize> Clone for Sender<'ch, M, T, N> | ||
| 40 | where | ||
| 41 | M: RawMutex, | ||
| 42 | { | ||
| 43 | fn clone(&self) -> Self { | ||
| 44 | Sender { | ||
| 45 | channel: self.channel, | ||
| 46 | } | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 39 | impl<'ch, M, T, const N: usize> Sender<'ch, M, T, N> | 50 | impl<'ch, M, T, const N: usize> Sender<'ch, M, T, N> |
| 40 | where | 51 | where |
| 41 | M: RawMutex, | 52 | M: RawMutex, |
| @@ -56,7 +67,7 @@ where | |||
| 56 | } | 67 | } |
| 57 | 68 | ||
| 58 | /// Receive-only access to a [`Channel`]. | 69 | /// Receive-only access to a [`Channel`]. |
| 59 | #[derive(Copy, Clone)] | 70 | #[derive(Copy)] |
| 60 | pub struct Receiver<'ch, M, T, const N: usize> | 71 | pub struct Receiver<'ch, M, T, const N: usize> |
| 61 | where | 72 | where |
| 62 | M: RawMutex, | 73 | M: RawMutex, |
| @@ -64,6 +75,17 @@ where | |||
| 64 | channel: &'ch Channel<M, T, N>, | 75 | channel: &'ch Channel<M, T, N>, |
| 65 | } | 76 | } |
| 66 | 77 | ||
| 78 | impl<'ch, M, T, const N: usize> Clone for Receiver<'ch, M, T, N> | ||
| 79 | where | ||
| 80 | M: RawMutex, | ||
| 81 | { | ||
| 82 | fn clone(&self) -> Self { | ||
| 83 | Receiver { | ||
| 84 | channel: self.channel, | ||
| 85 | } | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 67 | impl<'ch, M, T, const N: usize> Receiver<'ch, M, T, N> | 89 | impl<'ch, M, T, const N: usize> Receiver<'ch, M, T, N> |
| 68 | where | 90 | where |
| 69 | M: RawMutex, | 91 | M: RawMutex, |
| @@ -379,6 +401,16 @@ mod tests { | |||
| 379 | assert_eq!(c.try_recv().unwrap(), 1); | 401 | assert_eq!(c.try_recv().unwrap(), 1); |
| 380 | } | 402 | } |
| 381 | 403 | ||
| 404 | #[test] | ||
| 405 | fn cloning() { | ||
| 406 | let c = Channel::<NoopRawMutex, u32, 3>::new(); | ||
| 407 | let r1 = c.receiver(); | ||
| 408 | let s1 = c.sender(); | ||
| 409 | |||
| 410 | let _ = r1.clone(); | ||
| 411 | let _ = s1.clone(); | ||
| 412 | } | ||
| 413 | |||
| 382 | #[futures_test::test] | 414 | #[futures_test::test] |
| 383 | async fn receiver_receives_given_try_send_async() { | 415 | async fn receiver_receives_given_try_send_async() { |
| 384 | let executor = ThreadPool::new().unwrap(); | 416 | 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 @@ | |||
| 1 | //! Async channels | 1 | //! Async channels |
| 2 | 2 | ||
| 3 | pub mod channel; | 3 | pub mod channel; |
| 4 | pub use channel::*; | ||
| 5 | |||
| 4 | pub mod signal; | 6 | pub mod signal; |
| 7 | pub use signal::*; | ||
