diff options
Diffstat (limited to 'crates/stdx')
-rw-r--r-- | crates/stdx/src/lib.rs | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 5332edb09..5aacdb16e 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs | |||
@@ -1,5 +1,5 @@ | |||
1 | //! Missing batteries for standard libraries. | 1 | //! Missing batteries for standard libraries. |
2 | use std::{ops, process, time::Instant}; | 2 | use std::{cmp::Ordering, ops, process, time::Instant}; |
3 | 3 | ||
4 | mod macros; | 4 | mod macros; |
5 | pub mod panic_context; | 5 | pub mod panic_context; |
@@ -117,7 +117,12 @@ impl<'a> Iterator for LinesWithEnds<'a> { | |||
117 | } | 117 | } |
118 | } | 118 | } |
119 | 119 | ||
120 | // https://github.com/rust-lang/rust/issues/73831 | 120 | /// Returns `idx` such that: |
121 | /// | ||
122 | /// ∀ x in slice[..idx]: pred(x) | ||
123 | /// && ∀ x in slice[idx..]: !pred(x) | ||
124 | /// | ||
125 | /// https://github.com/rust-lang/rust/issues/73831 | ||
121 | pub fn partition_point<T, P>(slice: &[T], mut pred: P) -> usize | 126 | pub fn partition_point<T, P>(slice: &[T], mut pred: P) -> usize |
122 | where | 127 | where |
123 | P: FnMut(&T) -> bool, | 128 | P: FnMut(&T) -> bool, |
@@ -147,6 +152,15 @@ where | |||
147 | left | 152 | left |
148 | } | 153 | } |
149 | 154 | ||
155 | pub fn equal_range_by<T, F>(slice: &[T], mut key: F) -> (usize, usize) | ||
156 | where | ||
157 | F: FnMut(&T) -> Ordering, | ||
158 | { | ||
159 | let start = partition_point(slice, |it| key(it) == Ordering::Less); | ||
160 | let len = partition_point(&slice[start..], |it| key(it) == Ordering::Equal); | ||
161 | (start, len) | ||
162 | } | ||
163 | |||
150 | pub struct JodChild(pub process::Child); | 164 | pub struct JodChild(pub process::Child); |
151 | 165 | ||
152 | impl ops::Deref for JodChild { | 166 | impl ops::Deref for JodChild { |