Skip to content

Commit 02c15c9

Browse files
committed
move: DisplayMs to common::time
It seems we're using this in more than `lexe-api`, so might as well make it more broadly available.
1 parent cac06d3 commit 02c15c9

File tree

5 files changed

+18
-16
lines changed

5 files changed

+18
-16
lines changed

common/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub mod root_seed;
6161
pub mod task;
6262
/// `TestEvent`.
6363
pub mod test_event;
64-
/// `TimestampMs`
64+
/// `TimestampMs` and `DisplayMs`.
6565
pub mod time;
6666

6767
/// Feature-gated test utilities that can be shared across crate boundaries.

common/src/time.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ use std::{
66

77
use serde::{de, Serialize};
88

9+
/// [`Display`]s a [`Duration`] in ms with 3 decimal places, e.g. "123.456ms".
10+
///
11+
/// Useful to log elapsed times in a consistent unit, as [`Duration`]'s default
12+
/// implementation will go with seconds, ms, nanos etc depending on the value.
13+
pub struct DisplayMs(pub Duration);
14+
15+
impl Display for DisplayMs {
16+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17+
let ms = self.0.as_secs_f64() * 1000.0;
18+
write!(f, "{ms:.3}ms")
19+
}
20+
}
21+
922
/// The number of milliseconds since the [`UNIX_EPOCH`].
1023
///
1124
/// - Internally represented by a non-negative [`i64`] to ease interoperability

lexe-api/src/rest.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use common::{
66
ApiError, CommonApiError, CommonErrorKind, ErrorCode, ErrorResponse,
77
},
88
backoff, ed25519,
9+
time::DisplayMs,
910
};
1011
use http::{
1112
header::{HeaderValue, CONTENT_TYPE},
@@ -16,8 +17,7 @@ use reqwest::IntoUrl;
1617
use serde::{de::DeserializeOwned, Serialize};
1718
use tracing::{debug, info, warn, Instrument};
1819

19-
use super::trace::TraceId;
20-
use crate::trace::{self, DisplayMs};
20+
use crate::{trace, trace::TraceId};
2121

2222
/// The CONTENT-TYPE header for signed BCS-serialized structs.
2323
pub static CONTENT_TYPE_ED25519_BCS: HeaderValue =

lexe-api/src/trace.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88
};
99

1010
use anyhow::{bail, ensure, Context};
11-
use common::rng::ThreadFastRng;
11+
use common::{rng::ThreadFastRng, time::DisplayMs};
1212
use http::{HeaderName, HeaderValue};
1313
use rand_core::RngCore;
1414
use tracing::{span, warn, Dispatch};
@@ -352,17 +352,6 @@ macro_rules! define_trace_id_fns {
352352
};
353353
}
354354

355-
/// [`Display`]s a [`Duration`] in ms with 3 decimal places, e.g. "123.456ms".
356-
/// Used to log request / response times in a consistent unit.
357-
pub struct DisplayMs(pub Duration);
358-
359-
impl Display for DisplayMs {
360-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
361-
let ms = self.0.as_secs_f64() * 1000.0;
362-
write!(f, "{ms:.3}ms")
363-
}
364-
}
365-
366355
/// Client tracing utilities.
367356
pub(crate) mod client {
368357
use tracing::info_span;

node/src/event_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ async fn do_handle_event(
565565
mod anonymize {
566566
use std::collections::HashSet;
567567

568-
use lexe_api::trace::DisplayMs;
568+
use common::time::DisplayMs;
569569
use lightning::{
570570
events::PathFailure,
571571
ln::{channelmanager::PaymentId, PaymentHash},

0 commit comments

Comments
 (0)