Skip to content

Commit a404510

Browse files
committed
Simplify with ManuallyDrop
1 parent 0ae20be commit a404510

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

src/metrics_utils.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::time::{Duration, Instant};
1+
use std::{
2+
mem::ManuallyDrop,
3+
time::{Duration, Instant},
4+
};
25

36
use metrics::{describe_counter, describe_gauge, describe_histogram, gauge, histogram};
47

@@ -59,33 +62,28 @@ impl Drop for GaugeGuard {
5962
}
6063

6164
pub(crate) struct DurationHistogramGuard {
62-
start: Option<Instant>,
65+
start: Instant,
6366
key: &'static str,
6467
}
6568

6669
impl DurationHistogramGuard {
6770
pub(crate) fn start(key: &'static str) -> Self {
6871
Self {
69-
start: Some(Instant::now()),
72+
start: Instant::now(),
7073
key,
7174
}
7275
}
7376

74-
pub(crate) fn into_elapsed(mut self) -> Duration {
75-
let start = self
76-
.start
77-
.take()
78-
.expect("start time was unset without consuming or dropping the guard");
79-
let elapsed = start.elapsed();
80-
histogram!(self.key).record(elapsed);
77+
pub(crate) fn into_elapsed(self) -> Duration {
78+
let this = ManuallyDrop::new(self);
79+
let elapsed = this.start.elapsed();
80+
histogram!(this.key).record(elapsed);
8181
elapsed
8282
}
8383
}
8484

8585
impl Drop for DurationHistogramGuard {
8686
fn drop(&mut self) {
87-
if let Some(start) = self.start.take() {
88-
histogram!(self.key).record(start.elapsed());
89-
}
87+
histogram!(self.key).record(self.start.elapsed());
9088
}
9189
}

0 commit comments

Comments
 (0)