aboutsummaryrefslogtreecommitdiff
path: root/embassy-sync/src
diff options
context:
space:
mode:
authorOliver Rockstedt <[email protected]>2024-10-05 13:39:27 +0200
committerOliver Rockstedt <[email protected]>2024-10-05 13:39:27 +0200
commit383ad72b63b11ed1fc50ad5803534ac69996aff6 (patch)
tree32ac409c6364fc1acf4979fcd83797fa5d7c6246 /embassy-sync/src
parent6e0b08291b63a0da8eba9284869d1d046bc5dabb (diff)
embassy-sync: add clear, len, is_empty and is_full functions to zerocopy_channel
Diffstat (limited to 'embassy-sync/src')
-rw-r--r--embassy-sync/src/zerocopy_channel.rs76
1 files changed, 76 insertions, 0 deletions
diff --git a/embassy-sync/src/zerocopy_channel.rs b/embassy-sync/src/zerocopy_channel.rs
index cfce9a571..a2c763294 100644
--- a/embassy-sync/src/zerocopy_channel.rs
+++ b/embassy-sync/src/zerocopy_channel.rs
@@ -70,6 +70,28 @@ impl<'a, M: RawMutex, T> Channel<'a, M, T> {
70 pub fn split(&mut self) -> (Sender<'_, M, T>, Receiver<'_, M, T>) { 70 pub fn split(&mut self) -> (Sender<'_, M, T>, Receiver<'_, M, T>) {
71 (Sender { channel: self }, Receiver { channel: self }) 71 (Sender { channel: self }, Receiver { channel: self })
72 } 72 }
73
74 /// Clears all elements in the channel.
75 pub fn clear(&mut self) {
76 self.state.lock(|s| {
77 s.borrow_mut().clear();
78 });
79 }
80
81 /// Returns the number of elements currently in the channel.
82 pub fn len(&self) -> usize {
83 self.state.lock(|s| s.borrow().len())
84 }
85
86 /// Returns whether the channel is empty.
87 pub fn is_empty(&self) -> bool {
88 self.state.lock(|s| s.borrow().is_empty())
89 }
90
91 /// Returns whether the channel is full.
92 pub fn is_full(&self) -> bool {
93 self.state.lock(|s| s.borrow().is_full())
94 }
73} 95}
74 96
75/// Send-only access to a [`Channel`]. 97/// Send-only access to a [`Channel`].
@@ -130,6 +152,28 @@ impl<'a, M: RawMutex, T> Sender<'a, M, T> {
130 pub fn send_done(&mut self) { 152 pub fn send_done(&mut self) {
131 self.channel.state.lock(|s| s.borrow_mut().push_done()) 153 self.channel.state.lock(|s| s.borrow_mut().push_done())
132 } 154 }
155
156 /// Clears all elements in the channel.
157 pub fn clear(&mut self) {
158 self.channel.state.lock(|s| {
159 s.borrow_mut().clear();
160 });
161 }
162
163 /// Returns the number of elements currently in the channel.
164 pub fn len(&self) -> usize {
165 self.channel.state.lock(|s| s.borrow().len())
166 }
167
168 /// Returns whether the channel is empty.
169 pub fn is_empty(&self) -> bool {
170 self.channel.state.lock(|s| s.borrow().is_empty())
171 }
172
173 /// Returns whether the channel is full.
174 pub fn is_full(&self) -> bool {
175 self.channel.state.lock(|s| s.borrow().is_full())
176 }
133} 177}
134 178
135/// Receive-only access to a [`Channel`]. 179/// Receive-only access to a [`Channel`].
@@ -190,6 +234,28 @@ impl<'a, M: RawMutex, T> Receiver<'a, M, T> {
190 pub fn receive_done(&mut self) { 234 pub fn receive_done(&mut self) {
191 self.channel.state.lock(|s| s.borrow_mut().pop_done()) 235 self.channel.state.lock(|s| s.borrow_mut().pop_done())
192 } 236 }
237
238 /// Clears all elements in the channel.
239 pub fn clear(&mut self) {
240 self.channel.state.lock(|s| {
241 s.borrow_mut().clear();
242 });
243 }
244
245 /// Returns the number of elements currently in the channel.
246 pub fn len(&self) -> usize {
247 self.channel.state.lock(|s| s.borrow().len())
248 }
249
250 /// Returns whether the channel is empty.
251 pub fn is_empty(&self) -> bool {
252 self.channel.state.lock(|s| s.borrow().is_empty())
253 }
254
255 /// Returns whether the channel is full.
256 pub fn is_full(&self) -> bool {
257 self.channel.state.lock(|s| s.borrow().is_full())
258 }
193} 259}
194 260
195struct State { 261struct State {
@@ -217,6 +283,16 @@ impl State {
217 } 283 }
218 } 284 }
219 285
286 fn clear(&mut self) {
287 self.front = 0;
288 self.back = 0;
289 self.full = false;
290 }
291
292 fn len(&self) -> usize {
293 self.len
294 }
295
220 fn is_full(&self) -> bool { 296 fn is_full(&self) -> bool {
221 self.full 297 self.full
222 } 298 }