From 3ac01e362ee5785a920c9bd918c54fa3ef2a94ad Mon Sep 17 00:00:00 2001 From: Awpteamoose Date: Fri, 6 Sep 2024 11:41:32 +0100 Subject: [PATCH] inline mod --- src/time_delta.rs | 87 ++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 39 deletions(-) diff --git a/src/time_delta.rs b/src/time_delta.rs index d0925a840..15c72b039 100644 --- a/src/time_delta.rs +++ b/src/time_delta.rs @@ -62,28 +62,6 @@ pub struct TimeDelta { nanos: i32, // Always 0 <= nanos < NANOS_PER_SEC } -#[cfg(feature = "serde")] -impl serde::Serialize for TimeDelta { - fn serialize(&self, serializer: S) -> Result { - <(i64, i32) as serde::Serialize>::serialize(&(self.secs, self.nanos), serializer) - } -} - -#[cfg(feature = "serde")] -impl<'de> serde::Deserialize<'de> for TimeDelta { - fn deserialize>(deserializer: D) -> Result { - let (secs, nanos) = <(i64, i32) as serde::Deserialize>::deserialize(deserializer)?; - if !(MIN.secs..=MAX.secs).contains(&secs) - || nanos >= 1_000_000_000 - || (secs == MAX.secs && nanos > MAX.nanos) - || (secs == MIN.secs && nanos < MIN.nanos) - { - return Err(serde::de::Error::custom("TimeDelta out of bounds")); - } - Ok(TimeDelta { secs, nanos }) - } -} - /// The minimum possible `TimeDelta`: `-i64::MAX` milliseconds. pub(crate) const MIN: TimeDelta = TimeDelta { secs: -i64::MAX / MILLIS_PER_SEC - 1, @@ -660,6 +638,54 @@ impl arbitrary::Arbitrary<'_> for TimeDelta { } } +#[cfg(feature = "serde")] +mod serde { + use super::{TimeDelta, MAX, MIN}; + use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer}; + + impl Serialize for TimeDelta { + fn serialize(&self, serializer: S) -> Result { + <(i64, i32) as Serialize>::serialize(&(self.secs, self.nanos), serializer) + } + } + + impl<'de> Deserialize<'de> for TimeDelta { + fn deserialize>(deserializer: D) -> Result { + let (secs, nanos) = <(i64, i32) as Deserialize>::deserialize(deserializer)?; + if !(MIN.secs..=MAX.secs).contains(&secs) + || nanos >= 1_000_000_000 + || (secs == MAX.secs && nanos > MAX.nanos) + || (secs == MIN.secs && nanos < MIN.nanos) + { + return Err(Error::custom("TimeDelta out of bounds")); + } + Ok(TimeDelta { secs, nanos }) + } + } + + #[cfg(test)] + mod tests { + use super::{TimeDelta, MAX}; + + #[test] + fn test_serde() { + let duration = TimeDelta::new(123, 456).unwrap(); + assert_eq!( + serde_json::from_value::(serde_json::to_value(duration).unwrap()) + .unwrap(), + duration + ); + } + + #[test] + #[should_panic(expected = "TimeDelta out of bounds")] + fn test_serde_oob_panic() { + let _ = + serde_json::from_value::(serde_json::json!([MAX.secs + 1, 0])).unwrap(); + } + } +} + #[cfg(test)] mod tests { use super::OutOfRangeError; @@ -1327,21 +1353,4 @@ mod tests { let bytes = rkyv::to_bytes::<_, 16>(&duration).unwrap(); assert_eq!(rkyv::from_bytes::(&bytes).unwrap(), duration); } - - #[test] - #[cfg(feature = "serde")] - fn test_serde() { - let duration = TimeDelta::new(123, 456).unwrap(); - assert_eq!( - serde_json::from_value::(serde_json::to_value(duration).unwrap()).unwrap(), - duration - ); - } - - #[test] - #[cfg(feature = "serde")] - #[should_panic(expected = "TimeDelta out of bounds")] - fn test_serde_oob_panic() { - let _ = serde_json::from_value::(serde_json::json!([MAX.secs + 1, 0])).unwrap(); - } }