aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Mabin <[email protected]>2023-11-18 14:56:29 +0000
committerScott Mabin <[email protected]>2023-11-18 14:56:29 +0000
commit7589b5e13e0e01922804e603918fea0aa4dac30a (patch)
tree0031b04d52882d7bb3634435596318e4959eb486
parent2efa73f4313aeaad9b65b634425b689520096b9a (diff)
reduce duplication further by sharing Dynamic sender/receiver
-rw-r--r--embassy-sync/src/channel.rs4
-rw-r--r--embassy-sync/src/priority_channel.rs84
2 files changed, 3 insertions, 85 deletions
diff --git a/embassy-sync/src/channel.rs b/embassy-sync/src/channel.rs
index aa267471c..ff7129303 100644
--- a/embassy-sync/src/channel.rs
+++ b/embassy-sync/src/channel.rs
@@ -76,7 +76,7 @@ where
76 76
77/// Send-only access to a [`Channel`] without knowing channel size. 77/// Send-only access to a [`Channel`] without knowing channel size.
78pub struct DynamicSender<'ch, T> { 78pub struct DynamicSender<'ch, T> {
79 channel: &'ch dyn DynamicChannel<T>, 79 pub(crate) channel: &'ch dyn DynamicChannel<T>,
80} 80}
81 81
82impl<'ch, T> Clone for DynamicSender<'ch, T> { 82impl<'ch, T> Clone for DynamicSender<'ch, T> {
@@ -176,7 +176,7 @@ where
176 176
177/// Receive-only access to a [`Channel`] without knowing channel size. 177/// Receive-only access to a [`Channel`] without knowing channel size.
178pub struct DynamicReceiver<'ch, T> { 178pub struct DynamicReceiver<'ch, T> {
179 channel: &'ch dyn DynamicChannel<T>, 179 pub(crate) channel: &'ch dyn DynamicChannel<T>,
180} 180}
181 181
182impl<'ch, T> Clone for DynamicReceiver<'ch, T> { 182impl<'ch, T> Clone for DynamicReceiver<'ch, T> {
diff --git a/embassy-sync/src/priority_channel.rs b/embassy-sync/src/priority_channel.rs
index 6f419aa6e..143662814 100644
--- a/embassy-sync/src/priority_channel.rs
+++ b/embassy-sync/src/priority_channel.rs
@@ -13,7 +13,7 @@ use heapless::BinaryHeap;
13 13
14use crate::blocking_mutex::raw::RawMutex; 14use crate::blocking_mutex::raw::RawMutex;
15use crate::blocking_mutex::Mutex; 15use crate::blocking_mutex::Mutex;
16use crate::channel::{DynamicChannel, TryReceiveError, TrySendError}; 16use crate::channel::{DynamicChannel, DynamicReceiver, DynamicSender, TryReceiveError, TrySendError};
17use crate::waitqueue::WakerRegistration; 17use crate::waitqueue::WakerRegistration;
18 18
19/// Send-only access to a [`PriorityChannel`]. 19/// Send-only access to a [`PriorityChannel`].
@@ -73,19 +73,6 @@ where
73 } 73 }
74} 74}
75 75
76/// Send-only access to a [`PriorityChannel`] without knowing channel size.
77pub struct DynamicSender<'ch, T> {
78 channel: &'ch dyn DynamicChannel<T>,
79}
80
81impl<'ch, T> Clone for DynamicSender<'ch, T> {
82 fn clone(&self) -> Self {
83 DynamicSender { channel: self.channel }
84 }
85}
86
87impl<'ch, T> Copy for DynamicSender<'ch, T> {}
88
89impl<'ch, M, T, K, const N: usize> From<Sender<'ch, M, T, K, N>> for DynamicSender<'ch, T> 76impl<'ch, M, T, K, const N: usize> From<Sender<'ch, M, T, K, N>> for DynamicSender<'ch, T>
90where 77where
91 T: Ord, 78 T: Ord,
@@ -97,32 +84,6 @@ where
97 } 84 }
98} 85}
99 86
100impl<'ch, T> DynamicSender<'ch, T> {
101 /// Sends a value.
102 ///
103 /// See [`PriorityChannel::send()`]
104 pub fn send(&self, message: T) -> DynamicSendFuture<'ch, T> {
105 DynamicSendFuture {
106 channel: self.channel,
107 message: Some(message),
108 }
109 }
110
111 /// Attempt to immediately send a message.
112 ///
113 /// See [`PriorityChannel::send()`]
114 pub fn try_send(&self, message: T) -> Result<(), TrySendError<T>> {
115 self.channel.try_send_with_context(message, None)
116 }
117
118 /// Allows a poll_fn to poll until the channel is ready to send
119 ///
120 /// See [`PriorityChannel::poll_ready_to_send()`]
121 pub fn poll_ready_to_send(&self, cx: &mut Context<'_>) -> Poll<()> {
122 self.channel.poll_ready_to_send(cx)
123 }
124}
125
126/// Receive-only access to a [`PriorityChannel`]. 87/// Receive-only access to a [`PriorityChannel`].
127pub struct Receiver<'ch, M, T, K, const N: usize> 88pub struct Receiver<'ch, M, T, K, const N: usize>
128where 89where
@@ -187,49 +148,6 @@ where
187 } 148 }
188} 149}
189 150
190/// Receive-only access to a [`PriorityChannel`] without knowing channel size.
191pub struct DynamicReceiver<'ch, T> {
192 channel: &'ch dyn DynamicChannel<T>,
193}
194
195impl<'ch, T> Clone for DynamicReceiver<'ch, T> {
196 fn clone(&self) -> Self {
197 DynamicReceiver { channel: self.channel }
198 }
199}
200
201impl<'ch, T> Copy for DynamicReceiver<'ch, T> {}
202
203impl<'ch, T> DynamicReceiver<'ch, T> {
204 /// Receive the next value.
205 ///
206 /// See [`PriorityChannel::receive()`].
207 pub fn receive(&self) -> DynamicReceiveFuture<'_, T> {
208 DynamicReceiveFuture { channel: self.channel }
209 }
210
211 /// Attempt to immediately receive the next value.
212 ///
213 /// See [`PriorityChannel::try_receive()`]
214 pub fn try_receive(&self) -> Result<T, TryReceiveError> {
215 self.channel.try_receive_with_context(None)
216 }
217
218 /// Allows a poll_fn to poll until the channel is ready to receive
219 ///
220 /// See [`PriorityChannel::poll_ready_to_receive()`]
221 pub fn poll_ready_to_receive(&self, cx: &mut Context<'_>) -> Poll<()> {
222 self.channel.poll_ready_to_receive(cx)
223 }
224
225 /// Poll the channel for the next item
226 ///
227 /// See [`PriorityChannel::poll_receive()`]
228 pub fn poll_receive(&self, cx: &mut Context<'_>) -> Poll<T> {
229 self.channel.poll_receive(cx)
230 }
231}
232
233impl<'ch, M, T, K, const N: usize> From<Receiver<'ch, M, T, K, N>> for DynamicReceiver<'ch, T> 151impl<'ch, M, T, K, const N: usize> From<Receiver<'ch, M, T, K, N>> for DynamicReceiver<'ch, T>
234where 152where
235 T: Ord, 153 T: Ord,