Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added remove_if to priority channel #3523

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions embassy-sync/src/priority_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ where
self.channel.poll_ready_to_send(cx)
}

/// Removes the elements from the channel that satisfy the predicate.
///
/// See [`PriorityChannel::remove_if()`]
pub fn remove_if<F>(&self, predicate: F)
where
F: Fn(&T) -> bool,
T: Clone,
{
self.channel.remove_if(predicate)
}

/// Returns the maximum number of elements the channel can hold.
///
/// See [`PriorityChannel::capacity()`]
Expand Down Expand Up @@ -189,6 +200,17 @@ where
self.channel.poll_receive(cx)
}

/// Removes the elements from the channel that satisfy the predicate.
///
/// See [`PriorityChannel::remove_if()`]
pub fn remove_if<F>(&self, predicate: F)
where
F: Fn(&T) -> bool,
T: Clone,
{
self.channel.remove_if(predicate)
}

/// Returns the maximum number of elements the channel can hold.
///
/// See [`PriorityChannel::capacity()`]
Expand Down Expand Up @@ -534,6 +556,26 @@ where
self.lock(|c| c.try_receive())
}

/// Removes elements from the channel based on the given predicate.
pub fn remove_if<F>(&self, predicate: F)
where
F: Fn(&T) -> bool,
T: Clone,
{
self.lock(|c| {
let mut new_heap = BinaryHeap::<T, K, N>::new();
for item in c.queue.iter() {
if !predicate(item) {
match new_heap.push(item.clone()) {
Ok(_) => (),
Err(_) => panic!("Error pushing item to heap"),
}
}
}
c.queue = new_heap;
});
}

/// Returns the maximum number of elements the channel can hold.
pub const fn capacity(&self) -> usize {
N
Expand Down