File tree 2 files changed +23
-0
lines changed
2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change
1
+ ADDED: Add extension trait providing BinaryHeap::retain on Stable (as retain_ext)
Original file line number Diff line number Diff line change 36
36
#![ allow( clippy:: significant_drop_in_scrutinee) ] // arti/-/merge_requests/588/#note_2812945
37
37
//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
38
38
39
+ use std:: collections:: BinaryHeap ;
39
40
use std:: fmt;
41
+ use std:: mem;
40
42
41
43
pub mod futures;
42
44
pub mod iter;
@@ -101,6 +103,26 @@ impl IoErrorExt for std::io::Error {
101
103
102
104
// ----------------------------------------------------------------------
103
105
106
+ /// Implementation of `BinaryHeap::retain` that doesn't require Nightly
107
+ pub trait BinaryHeapExt < T > {
108
+ /// Remove all elements for which `f` returns `false`
109
+ ///
110
+ /// Performance is not great right now - the algorithm is `O(n*log(n))`
111
+ /// where `n` is the number of elements in the heap (not the number removed).
112
+ ///
113
+ /// The name is `retain_ext` to avoid a name collision with the unstable function,
114
+ /// which would require the use of UFCS and make this unergonomic.
115
+ fn retain_ext < F : FnMut ( & T ) -> bool > ( & mut self , f : F ) ;
116
+ }
117
+ impl < T : Ord > BinaryHeapExt < T > for BinaryHeap < T > {
118
+ fn retain_ext < F : FnMut ( & T ) -> bool > ( & mut self , f : F ) {
119
+ let items = mem:: take ( self ) . into_iter ( ) ;
120
+ * self = items. filter ( f) . collect ( ) ;
121
+ }
122
+ }
123
+
124
+ // ----------------------------------------------------------------------
125
+
104
126
/// Define an "accessor trait", which describes structs that have fields of certain types
105
127
///
106
128
/// This can be useful if a large struct, living high up in the dependency graph,
You can’t perform that action at this time.
0 commit comments