aboutsummaryrefslogtreecommitdiff
path: root/crates/stdx
diff options
context:
space:
mode:
Diffstat (limited to 'crates/stdx')
-rw-r--r--crates/stdx/src/lib.rs39
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.
2use std::time::Instant; 2use std::{cmp::Ordering, ops, process, time::Instant};
3 3
4mod macros; 4mod macros;
5pub mod panic_context; 5pub 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
121pub fn partition_point<T, P>(slice: &[T], mut pred: P) -> usize 126pub fn partition_point<T, P>(slice: &[T], mut pred: P) -> usize
122where 127where
123 P: FnMut(&T) -> bool, 128 P: FnMut(&T) -> bool,
@@ -147,6 +152,36 @@ where
147 left 152 left
148} 153}
149 154
155pub fn equal_range_by<T, F>(slice: &[T], mut key: F) -> ops::Range<usize>
156where
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
164pub struct JodChild(pub process::Child);
165
166impl ops::Deref for JodChild {
167 type Target = process::Child;
168 fn deref(&self) -> &process::Child {
169 &self.0
170 }
171}
172
173impl ops::DerefMut for JodChild {
174 fn deref_mut(&mut self) -> &mut process::Child {
175 &mut self.0
176 }
177}
178
179impl Drop for JodChild {
180 fn drop(&mut self) {
181 let _ = self.0.kill();
182 }
183}
184
150#[cfg(test)] 185#[cfg(test)]
151mod tests { 186mod tests {
152 use super::*; 187 use super::*;