From 86eb768cb55d9745feabd1d9f7e4d0a1a0be741d Mon Sep 17 00:00:00 2001 From: Beta Date: Sun, 3 Mar 2024 02:51:20 +0000 Subject: [PATCH 1/2] add ord and partialOrd to Timestamp --- prost-types/src/lib.rs | 25 +++++++++++++++++++++++++ prost-types/src/protobuf.rs | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/prost-types/src/lib.rs b/prost-types/src/lib.rs index 81fcd9670..2d8d6cd59 100644 --- a/prost-types/src/lib.rs +++ b/prost-types/src/lib.rs @@ -395,6 +395,15 @@ impl std::hash::Hash for Timestamp { } } +impl Ord for Timestamp { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + match self.seconds.cmp(&other.seconds) { + std::cmp::Ordering::Equal => self.nanos.cmp(&other.nanos), + ordering => ordering, + } + } +} + #[cfg(feature = "std")] impl From for Timestamp { fn from(system_time: std::time::SystemTime) -> Timestamp { @@ -907,4 +916,20 @@ mod tests { // Must contain at least one "/" character. assert_eq!(TypeUrl::new("google.protobuf.Duration"), None); } + + #[test] + fn check_timestamp_ord() { + let earlier_timestamp = Timestamp { seconds: 10, nanos: 500 }; + let later_timestamp = Timestamp { seconds: 10, nanos: 700 }; + let equal_timestamp = Timestamp { seconds: 10, nanos: 500 }; + + assert!(earlier_timestamp < later_timestamp); + assert!(earlier_timestamp <= later_timestamp); + assert!(later_timestamp > earlier_timestamp); + assert!(later_timestamp >= earlier_timestamp); + + assert_eq!(earlier_timestamp.cmp(&later_timestamp), std::cmp::Ordering::Less); + assert_eq!(later_timestamp.cmp(&earlier_timestamp), std::cmp::Ordering::Greater); + assert_eq!(equal_timestamp.cmp(&earlier_timestamp), std::cmp::Ordering::Equal); + } } diff --git a/prost-types/src/protobuf.rs b/prost-types/src/protobuf.rs index edc1361be..34caa44c9 100644 --- a/prost-types/src/protobuf.rs +++ b/prost-types/src/protobuf.rs @@ -2293,7 +2293,7 @@ impl NullValue { /// the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one can use /// the Joda Time's [`ISODateTimeFormat.dateTime()`]() to obtain a formatter capable of generating timestamps in this format. #[allow(clippy::derive_partial_eq_without_eq)] -#[derive(Clone, PartialEq, ::prost::Message)] +#[derive(Clone, PartialEq, PartialOrd,::prost::Message)] pub struct Timestamp { /// Represents seconds of UTC time since Unix epoch /// 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to From 6eb00b3f4cf10643ff34595c28e6b41900ba6ea9 Mon Sep 17 00:00:00 2001 From: Beta Date: Sun, 3 Mar 2024 02:53:51 +0000 Subject: [PATCH 2/2] FMT --- prost-types/src/lib.rs | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/prost-types/src/lib.rs b/prost-types/src/lib.rs index 2d8d6cd59..f41dac750 100644 --- a/prost-types/src/lib.rs +++ b/prost-types/src/lib.rs @@ -919,17 +919,35 @@ mod tests { #[test] fn check_timestamp_ord() { - let earlier_timestamp = Timestamp { seconds: 10, nanos: 500 }; - let later_timestamp = Timestamp { seconds: 10, nanos: 700 }; - let equal_timestamp = Timestamp { seconds: 10, nanos: 500 }; + let earlier_timestamp = Timestamp { + seconds: 10, + nanos: 500, + }; + let later_timestamp = Timestamp { + seconds: 10, + nanos: 700, + }; + let equal_timestamp = Timestamp { + seconds: 10, + nanos: 500, + }; assert!(earlier_timestamp < later_timestamp); assert!(earlier_timestamp <= later_timestamp); assert!(later_timestamp > earlier_timestamp); assert!(later_timestamp >= earlier_timestamp); - assert_eq!(earlier_timestamp.cmp(&later_timestamp), std::cmp::Ordering::Less); - assert_eq!(later_timestamp.cmp(&earlier_timestamp), std::cmp::Ordering::Greater); - assert_eq!(equal_timestamp.cmp(&earlier_timestamp), std::cmp::Ordering::Equal); + assert_eq!( + earlier_timestamp.cmp(&later_timestamp), + std::cmp::Ordering::Less + ); + assert_eq!( + later_timestamp.cmp(&earlier_timestamp), + std::cmp::Ordering::Greater + ); + assert_eq!( + equal_timestamp.cmp(&earlier_timestamp), + std::cmp::Ordering::Equal + ); } }