1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! Monotonic latency measurement for protocol implementations.
//!
//! Uses [`tokio::time::Instant`] instead of [`std::time::SystemTime`] because:
//!
//! - `Instant` is **monotonic** — it never goes backwards, even when the
//! system clock is adjusted by NTP or the user.
//! - `SystemTime` can go backwards on clock corrections, which would cause
//! `elapsed()` to return an error or a negative duration.
//! - `Instant` has no epoch — it cannot be serialised or compared across
//! process restarts, but that is fine here since we only measure short
//! durations within a single ping.
//!
//! # Usage
//!
//! ```rust,ignore
//! use rust_mc_status::core::time::{start_timer, elapsed_ms};
//!
//! let start = start_timer();
//! // … network operation …
//! let latency = elapsed_ms(start); // f64, always ≥ 0
//! ```
use Instant;
/// Record the start of a latency measurement.
///
/// Returns a [`tokio::time::Instant`] that should be passed to [`elapsed_ms`]
/// after the operation completes.
///
/// # Example
///
/// ```rust,ignore
/// let start = start_timer();
/// do_something().await;
/// let ms = elapsed_ms(start);
/// ```
/// Return elapsed milliseconds since `start` as `f64`.
///
/// Always returns a non-negative value — the underlying monotonic clock
/// guarantees that `Instant::elapsed` never goes backwards.
///
/// # Example
///
/// ```rust,ignore
/// let start = start_timer();
/// do_something().await;
/// println!("{:.2} ms", elapsed_ms(start));
/// ```