macro_rules! measure_time {
    (@unit [$unit:literal, $as_unit:ident]; $tag:expr, $expr:expr) => { ... };
    (@auto $tag:expr, $expr:expr) => { ... };
    ($tag:expr, $expr:expr) => { ... };
    (MILLI, $tag:expr, $expr:expr) => { ... };
    (MICRO, $tag:expr, $expr:expr) => { ... };
    (NANO,  $tag:expr, $expr:expr) => { ... };
    (SEC,   $tag:expr, $expr:expr) => { ... };
}
Expand description

Measure given expression usage time.

If expression is early return, Time will not be measured. It occur when we use ? in expression. Some kind of that expression can be measured by moving ? out of expression.

For example

// This is async method that return Result
async fn get() -> Result<(), ()> { ... }

async fn main() -> Result<(), ()> {
    const TAG: &'static str = "Get something";
    // don't do this, if error occur, measurement log is not emit.
    measure_time!(TAG, get().await?);
    // change to this instead.
    measure_time!(TAG, get().await)?;
}

NOTE:

  • Use ; as a unit separator cause rustfmt at call site not working properly.
  • $tag can be anything that implement std::fmt::Display.