diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 857567a853a..1b6211044b4 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs @@ -14,18 +14,8 @@ pub fn is_ci() -> bool { #[must_use] pub fn timeit(label: &'static str) -> impl Drop { - struct Guard { - label: &'static str, - start: Instant, - } - - impl Drop for Guard { - fn drop(&mut self) { - eprintln!("{}: {:.2?}", self.label, self.start.elapsed()) - } - } - - Guard { label, start: Instant::now() } + let start = Instant::now(); + defer(move || eprintln!("{}: {:.2?}", label, start.elapsed())) } /// Prints backtrace to stderr, useful for debugging. @@ -179,6 +169,7 @@ pub fn equal_range_by(slice: &[T], mut key: F) -> ops::Range start..start + len } +#[must_use] pub fn defer(f: F) -> impl Drop { struct D(Option); impl Drop for D {