[fractal] Make model.Message order by date, then id



commit e551658a630c729c11a531a03e1d77ed6c62feba
Author: Kai A. Hiller <V02460 gmail com>
Date:   Thu Aug 13 12:56:45 2020 +0200

    Make model.Message order by date, then id

 fractal-gtk/src/model/message.rs | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
---
diff --git a/fractal-gtk/src/model/message.rs b/fractal-gtk/src/model/message.rs
index 8ac6dfcd..1bfa753b 100644
--- a/fractal-gtk/src/model/message.rs
+++ b/fractal-gtk/src/model/message.rs
@@ -49,19 +49,32 @@ pub struct Message {
     pub extra_content: Option<JsonValue>,
 }
 
+impl Eq for Message {}
+
 impl PartialEq for Message {
+    /// Compares equal if ids match.
     fn eq(&self, other: &Self) -> bool {
-        self.id == other.id
+        // - Panics if ids are None.
+        // - Assumes the date is a function of the id. That means, two Messages
+        //   with the same id will always have the same date.
+        self.id.as_ref().unwrap() == other.id.as_ref().unwrap()
+    }
+}
+
+impl Ord for Message {
+    /// Orders based on date, then id.
+    fn cmp(&self, other: &Self) -> Ordering {
+        match self.date.cmp(&other.date) {
+            // Panics if ids are None
+            Ordering::Equal => self.id.as_ref().unwrap().cmp(&other.id.as_ref().unwrap()),
+            date_order => date_order,
+        }
     }
 }
 
 impl PartialOrd for Message {
     fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
-        if self == other {
-            Some(Ordering::Equal)
-        } else {
-            self.date.partial_cmp(&other.date)
-        }
+        Some(self.cmp(other))
     }
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]