From 1249dda583cd93be472b2be76797dacb33601adc Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 2 Feb 2023 12:10:29 +0100 Subject: [PATCH] common: Implement Debug manually for [Milli]SecondsSinceUnixEpoch --- crates/ruma-common/src/time.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/crates/ruma-common/src/time.rs b/crates/ruma-common/src/time.rs index bb818bc4..707b931d 100644 --- a/crates/ruma-common/src/time.rs +++ b/crates/ruma-common/src/time.rs @@ -1,10 +1,13 @@ -use std::time::{Duration, SystemTime, UNIX_EPOCH}; +use std::{ + fmt, + time::{Duration, SystemTime, UNIX_EPOCH}, +}; use js_int::{uint, UInt}; use serde::{Deserialize, Serialize}; /// A timestamp represented as the number of milliseconds since the unix epoch. -#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)] +#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)] #[allow(clippy::exhaustive_structs)] #[serde(transparent)] pub struct MilliSecondsSinceUnixEpoch(pub UInt); @@ -43,8 +46,16 @@ impl MilliSecondsSinceUnixEpoch { } } +impl fmt::Debug for MilliSecondsSinceUnixEpoch { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // The default Debug impl would put the inner value on its own line if the formatter's + // alternate mode is enabled, which bloats debug strings unnecessarily + write!(f, "MilliSecondsSinceUnixEpoch({})", self.0) + } +} + /// A timestamp represented as the number of seconds since the unix epoch. -#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)] +#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)] #[allow(clippy::exhaustive_structs)] #[serde(transparent)] pub struct SecondsSinceUnixEpoch(pub UInt); @@ -78,6 +89,14 @@ impl SecondsSinceUnixEpoch { } } +impl fmt::Debug for SecondsSinceUnixEpoch { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // The default Debug impl would put the inner value on its own line if the formatter's + // alternate mode is enabled, which bloats debug strings unnecessarily + write!(f, "SecondsSinceUnixEpoch({})", self.0) + } +} + #[cfg(all(target_arch = "wasm32", target_os = "unknown", feature = "js"))] fn f64_to_uint(val: f64) -> UInt { // UInt::MAX milliseconds is ~285 616 years, we do not account for that