From 910c177d2e7a68406f48dfb0635a82319892b50b Mon Sep 17 00:00:00 2001 From: Lyndon Gingerich Date: Fri, 23 Jun 2023 11:10:56 -0500 Subject: [PATCH 1/3] Hand-write CustomComparison and CustomEquality on Timestamp --- Protobuf.FSharp/BuiltinTypes/Timestamp.fs | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Protobuf.FSharp/BuiltinTypes/Timestamp.fs b/Protobuf.FSharp/BuiltinTypes/Timestamp.fs index 86cd0cb..6f71209 100644 --- a/Protobuf.FSharp/BuiltinTypes/Timestamp.fs +++ b/Protobuf.FSharp/BuiltinTypes/Timestamp.fs @@ -32,6 +32,9 @@ module TimestampReflection = true ) let Descriptor(): global.Google.Protobuf.Reflection.FileDescriptor = descriptorBackingField.Value +// Start of hand-written code +[] +// End of hand-written code type Timestamp = { mutable _UnknownFields: global.Google.Protobuf.UnknownFieldSet mutable Seconds: int64 @@ -56,6 +59,29 @@ type Timestamp = { member me.ToDateTimeOffset() : ValueOption = let ticks = int64 me.Nanos / 100L + me.Seconds * System.TimeSpan.TicksPerSecond ValueSome(System.DateTimeOffset(Timestamp.unixEpoch.AddTicks(ticks))) + + override this.Equals other = + match other with + | :? Timestamp as otherTimestamp -> + otherTimestamp.Seconds.Equals this.Seconds && otherTimestamp.Nanos.Equals this.Nanos + | _ -> false + + override this.GetHashCode() = + ((System.Numerics.BigInteger(this.Seconds) <<< 32) + System.Numerics.BigInteger(this.Nanos)).GetHashCode() + + interface System.IComparable with + member this.CompareTo(other: Timestamp) = + if this.Seconds > other.Seconds then 1 + else if this.Seconds < other.Seconds then -1 + else if this.Nanos > other.Nanos then 1 + else if this.Nanos < other.Nanos then -1 + else 0 + + interface System.IComparable with + member this.CompareTo other = + match other with + | :? Timestamp as otherTimestamp -> (this :> System.IComparable<_>).CompareTo otherTimestamp + | _ -> -1 // End of hand-written code [] member me.Clone() : Timestamp = { From 32b913ac69308e9cb6f625f030a69ad362bc14d3 Mon Sep 17 00:00:00 2001 From: Lyndon Gingerich Date: Fri, 23 Jun 2023 11:44:18 -0500 Subject: [PATCH 2/3] Implement System.IEquatable The absence of this interface was causing an error I didn't notice at first. --- Protobuf.FSharp/BuiltinTypes/Timestamp.fs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Protobuf.FSharp/BuiltinTypes/Timestamp.fs b/Protobuf.FSharp/BuiltinTypes/Timestamp.fs index 6f71209..656dff1 100644 --- a/Protobuf.FSharp/BuiltinTypes/Timestamp.fs +++ b/Protobuf.FSharp/BuiltinTypes/Timestamp.fs @@ -82,6 +82,9 @@ type Timestamp = { match other with | :? Timestamp as otherTimestamp -> (this :> System.IComparable<_>).CompareTo otherTimestamp | _ -> -1 + + interface System.IEquatable with + member this.Equals other = this.Equals other // End of hand-written code [] member me.Clone() : Timestamp = { From 36ba5f1fcb0fe146e1fa939d4c0e9f631bf08447 Mon Sep 17 00:00:00 2001 From: Lyndon Gingerich Date: Fri, 23 Jun 2023 11:47:34 -0500 Subject: [PATCH 3/3] Call IEquatable<_>.Equals from Object.Equals --- Protobuf.FSharp/BuiltinTypes/Timestamp.fs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Protobuf.FSharp/BuiltinTypes/Timestamp.fs b/Protobuf.FSharp/BuiltinTypes/Timestamp.fs index 656dff1..50b2a87 100644 --- a/Protobuf.FSharp/BuiltinTypes/Timestamp.fs +++ b/Protobuf.FSharp/BuiltinTypes/Timestamp.fs @@ -59,11 +59,13 @@ type Timestamp = { member me.ToDateTimeOffset() : ValueOption = let ticks = int64 me.Nanos / 100L + me.Seconds * System.TimeSpan.TicksPerSecond ValueSome(System.DateTimeOffset(Timestamp.unixEpoch.AddTicks(ticks))) + + interface System.IEquatable with + member this.Equals other = other.Seconds.Equals this.Seconds && other.Nanos.Equals this.Nanos override this.Equals other = match other with - | :? Timestamp as otherTimestamp -> - otherTimestamp.Seconds.Equals this.Seconds && otherTimestamp.Nanos.Equals this.Nanos + | :? Timestamp as otherTimestamp -> (this :> System.IEquatable<_>).Equals otherTimestamp | _ -> false override this.GetHashCode() = @@ -82,9 +84,6 @@ type Timestamp = { match other with | :? Timestamp as otherTimestamp -> (this :> System.IComparable<_>).CompareTo otherTimestamp | _ -> -1 - - interface System.IEquatable with - member this.Equals other = this.Equals other // End of hand-written code [] member me.Clone() : Timestamp = {