diff options
Diffstat (limited to 'crates/stdx')
-rw-r--r-- | crates/stdx/src/lib.rs | 39 |
1 files changed, 37 insertions, 2 deletions
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 374ed5910..13aab1451 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::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,36 @@ where | |||
147 | left | 152 | left |
148 | } | 153 | } |
149 | 154 | ||
155 | pub fn equal_range_by<T, F>(slice: &[T], mut key: F) -> ops::Range<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..start + len | ||
162 | } | ||
163 | |||
164 | pub struct JodChild(pub process::Child); | ||
165 | |||
166 | impl ops::Deref for JodChild { | ||
167 | type Target = process::Child; | ||
168 | fn deref(&self) -> &process::Child { | ||
169 | &self.0 | ||
170 | } | ||
171 | } | ||
172 | |||
173 | impl ops::DerefMut for JodChild { | ||
174 | fn deref_mut(&mut self) -> &mut process::Child { | ||
175 | &mut self.0 | ||
176 | } | ||
177 | } | ||
178 | |||
179 | impl Drop for JodChild { | ||
180 | fn drop(&mut self) { | ||
181 | let _ = self.0.kill(); | ||
182 | } | ||
183 | } | ||
184 | |||
150 | #[cfg(test)] | 185 | #[cfg(test)] |
151 | mod tests { | 186 | mod tests { |
152 | use super::*; | 187 | use super::*; |