|
72 | 72 | self.channel.poll_ready_to_send(cx) |
73 | 73 | } |
74 | 74 |
|
| 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 | + |
75 | 86 | /// Returns the maximum number of elements the channel can hold. |
76 | 87 | /// |
77 | 88 | /// See [`PriorityChannel::capacity()`] |
@@ -189,6 +200,17 @@ where |
189 | 200 | self.channel.poll_receive(cx) |
190 | 201 | } |
191 | 202 |
|
| 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 | + |
192 | 214 | /// Returns the maximum number of elements the channel can hold. |
193 | 215 | /// |
194 | 216 | /// See [`PriorityChannel::capacity()`] |
@@ -534,6 +556,26 @@ where |
534 | 556 | self.lock(|c| c.try_receive()) |
535 | 557 | } |
536 | 558 |
|
| 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 | + |
537 | 579 | /// Returns the maximum number of elements the channel can hold. |
538 | 580 | pub const fn capacity(&self) -> usize { |
539 | 581 | N |
|
0 commit comments