Skip to content

Commit 9f451c7

Browse files
Gibbzlulf
authored andcommitted
added remove_if to priority channel
1 parent c390767 commit 9f451c7

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

embassy-sync/src/priority_channel.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ where
7272
self.channel.poll_ready_to_send(cx)
7373
}
7474

75+
/// Removes the elements from the channel that satisfy the predicate.
76+
///
77+
/// See [`PriorityChannel::remove_if()`]
78+
pub fn remove_if<F>(&self, predicate: F)
79+
where
80+
F: Fn(&T) -> bool,
81+
T: Clone,
82+
{
83+
self.channel.remove_if(predicate)
84+
}
85+
7586
/// Returns the maximum number of elements the channel can hold.
7687
///
7788
/// See [`PriorityChannel::capacity()`]
@@ -189,6 +200,17 @@ where
189200
self.channel.poll_receive(cx)
190201
}
191202

203+
/// Removes the elements from the channel that satisfy the predicate.
204+
///
205+
/// See [`PriorityChannel::remove_if()`]
206+
pub fn remove_if<F>(&self, predicate: F)
207+
where
208+
F: Fn(&T) -> bool,
209+
T: Clone,
210+
{
211+
self.channel.remove_if(predicate)
212+
}
213+
192214
/// Returns the maximum number of elements the channel can hold.
193215
///
194216
/// See [`PriorityChannel::capacity()`]
@@ -534,6 +556,26 @@ where
534556
self.lock(|c| c.try_receive())
535557
}
536558

559+
/// Removes elements from the channel based on the given predicate.
560+
pub fn remove_if<F>(&self, predicate: F)
561+
where
562+
F: Fn(&T) -> bool,
563+
T: Clone,
564+
{
565+
self.lock(|c| {
566+
let mut new_heap = BinaryHeap::<T, K, N>::new();
567+
for item in c.queue.iter() {
568+
if !predicate(item) {
569+
match new_heap.push(item.clone()) {
570+
Ok(_) => (),
571+
Err(_) => panic!("Error pushing item to heap"),
572+
}
573+
}
574+
}
575+
c.queue = new_heap;
576+
});
577+
}
578+
537579
/// Returns the maximum number of elements the channel can hold.
538580
pub const fn capacity(&self) -> usize {
539581
N

0 commit comments

Comments
 (0)