diff options
Diffstat (limited to 'crates/stdx/src/lib.rs')
-rw-r--r-- | crates/stdx/src/lib.rs | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index b0a18d58d..1b6211044 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs | |||
@@ -1,7 +1,8 @@ | |||
1 | //! Missing batteries for standard libraries. | 1 | //! Missing batteries for standard libraries. |
2 | use std::{cmp::Ordering, ops, process, time::Instant}; | 2 | use std::{cmp::Ordering, ops, time::Instant}; |
3 | 3 | ||
4 | mod macros; | 4 | mod macros; |
5 | pub mod process; | ||
5 | pub mod panic_context; | 6 | pub mod panic_context; |
6 | 7 | ||
7 | pub use always_assert::{always, never}; | 8 | pub use always_assert::{always, never}; |
@@ -13,18 +14,8 @@ pub fn is_ci() -> bool { | |||
13 | 14 | ||
14 | #[must_use] | 15 | #[must_use] |
15 | pub fn timeit(label: &'static str) -> impl Drop { | 16 | pub fn timeit(label: &'static str) -> impl Drop { |
16 | struct Guard { | 17 | let start = Instant::now(); |
17 | label: &'static str, | 18 | defer(move || eprintln!("{}: {:.2?}", label, start.elapsed())) |
18 | start: Instant, | ||
19 | } | ||
20 | |||
21 | impl Drop for Guard { | ||
22 | fn drop(&mut self) { | ||
23 | eprintln!("{}: {:.2?}", self.label, self.start.elapsed()) | ||
24 | } | ||
25 | } | ||
26 | |||
27 | Guard { label, start: Instant::now() } | ||
28 | } | 19 | } |
29 | 20 | ||
30 | /// Prints backtrace to stderr, useful for debugging. | 21 | /// Prints backtrace to stderr, useful for debugging. |
@@ -178,18 +169,31 @@ where | |||
178 | start..start + len | 169 | start..start + len |
179 | } | 170 | } |
180 | 171 | ||
172 | #[must_use] | ||
173 | pub fn defer<F: FnOnce()>(f: F) -> impl Drop { | ||
174 | struct D<F: FnOnce()>(Option<F>); | ||
175 | impl<F: FnOnce()> Drop for D<F> { | ||
176 | fn drop(&mut self) { | ||
177 | if let Some(f) = self.0.take() { | ||
178 | f() | ||
179 | } | ||
180 | } | ||
181 | } | ||
182 | D(Some(f)) | ||
183 | } | ||
184 | |||
181 | #[repr(transparent)] | 185 | #[repr(transparent)] |
182 | pub struct JodChild(pub process::Child); | 186 | pub struct JodChild(pub std::process::Child); |
183 | 187 | ||
184 | impl ops::Deref for JodChild { | 188 | impl ops::Deref for JodChild { |
185 | type Target = process::Child; | 189 | type Target = std::process::Child; |
186 | fn deref(&self) -> &process::Child { | 190 | fn deref(&self) -> &std::process::Child { |
187 | &self.0 | 191 | &self.0 |
188 | } | 192 | } |
189 | } | 193 | } |
190 | 194 | ||
191 | impl ops::DerefMut for JodChild { | 195 | impl ops::DerefMut for JodChild { |
192 | fn deref_mut(&mut self) -> &mut process::Child { | 196 | fn deref_mut(&mut self) -> &mut std::process::Child { |
193 | &mut self.0 | 197 | &mut self.0 |
194 | } | 198 | } |
195 | } | 199 | } |
@@ -202,9 +206,9 @@ impl Drop for JodChild { | |||
202 | } | 206 | } |
203 | 207 | ||
204 | impl JodChild { | 208 | impl JodChild { |
205 | pub fn into_inner(self) -> process::Child { | 209 | pub fn into_inner(self) -> std::process::Child { |
206 | // SAFETY: repr transparent | 210 | // SAFETY: repr transparent |
207 | unsafe { std::mem::transmute::<JodChild, process::Child>(self) } | 211 | unsafe { std::mem::transmute::<JodChild, std::process::Child>(self) } |
208 | } | 212 | } |
209 | } | 213 | } |
210 | 214 | ||