#![cfg(all(feature = "std", feature = "metrics"))]
use std::fmt;
use std::sync::Arc;
use std::time::Instant;
use crate::watch::Watch;
#[must_use]
pub struct Timer {
watch: Watch,
name: Arc<str>,
start: Option<Instant>, }
impl fmt::Debug for Timer {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Timer")
.field("name", &self.name)
.field("active", &self.start.is_some())
.finish_non_exhaustive()
}
}
impl Timer {
#[inline]
pub fn new(watch: Watch, name: impl Into<Arc<str>>) -> Self {
Self {
watch,
name: name.into(),
start: Some(Instant::now()),
}
}
#[inline]
pub fn stop(mut self) -> u64 {
if let Some(start) = self.start.take() {
return self.watch.record_instant(&self.name, start);
}
0
}
}
impl Drop for Timer {
#[inline]
fn drop(&mut self) {
if let Some(start) = self.start.take() {
let _ = self.watch.record_instant(&self.name, start);
}
}
}