From f6bb1a9572f17bf46e3d4a57952343f9ee93637b Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 3 Jun 2021 15:33:43 -0700 Subject: [PATCH] Implement Ord manually for time::Lamport --- zed/src/time.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/zed/src/time.rs b/zed/src/time.rs index c4cff793c93ef5a0a225a9d310e9102e95e749a4..00f4e54c186206462e25b288e349010ceb0600e3 100644 --- a/zed/src/time.rs +++ b/zed/src/time.rs @@ -15,7 +15,7 @@ pub struct Local { pub value: Seq, } -#[derive(Clone, Copy, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[derive(Clone, Copy, Default, Eq, Hash, PartialEq)] pub struct Lamport { pub replica_id: ReplicaId, pub value: Seq, @@ -139,6 +139,21 @@ impl PartialOrd for Global { } } +impl Ord for Lamport { + fn cmp(&self, other: &Self) -> Ordering { + // Use the replica id to break ties between concurrent events. + self.value + .cmp(&other.value) + .then_with(|| self.replica_id.cmp(&other.replica_id)) + } +} + +impl PartialOrd for Lamport { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + impl Lamport { pub fn new(replica_id: ReplicaId) -> Self { Self {