]> git.lizzy.rs Git - rust.git/commitdiff
Add cmp::Ord implementation for semver::Version
authorZack Corr <zack@z0w0.me>
Sat, 19 Jan 2013 09:58:24 +0000 (19:58 +1000)
committerGraydon Hoare <graydon@mozilla.com>
Sat, 16 Feb 2013 02:04:10 +0000 (18:04 -0800)
src/libcore/semver.rs

index 5a739772d1e68c9c92192cd7736d9cf7c68072f3..fc5d951edc5db90a854be16e281961e3c949b463 100644 (file)
@@ -17,6 +17,7 @@
 use str;
 use to_str::ToStr;
 use char;
+use cmp;
 
 pub struct Version {
     major: uint,
@@ -37,6 +38,61 @@ impl Version: ToStr {
     }
 }
 
+impl Version: cmp::Ord {
+    #[inline(always)]
+    pure fn lt(&self, other: &Version) -> bool {
+        self.major < other.major ||
+        self.minor < other.minor ||
+        self.patch < other.patch ||
+        (match self.tag {
+            Some(stag) => match other.tag {
+                Some(otag) => stag < otag,
+                None => true
+            },
+            None => false
+        })
+    } 
+    #[inline(always)]
+    pure fn le(&self, other: &Version) -> bool {
+        self.major <= other.major ||
+        self.minor <= other.minor ||
+        self.patch <= other.patch ||
+        (match self.tag {
+            Some(stag) => match other.tag {
+                Some(otag) => stag <= otag,
+                None => true
+            },
+            None => false
+        })
+    }
+    #[inline(always)]
+    pure fn gt(&self, other: &Version) -> bool {
+        self.major > other.major ||
+        self.minor > other.minor ||
+        self.patch > other.patch ||
+        (match self.tag {
+            Some(stag) => match other.tag {
+                Some(otag) => stag > otag,
+                None => false
+            },
+            None => true
+        })
+    }
+    #[inline(always)]
+    pure fn ge(&self, other: &Version) -> bool {
+        self.major >= other.major ||
+        self.minor >= other.minor ||
+        self.patch >= other.patch ||
+        (match self.tag {
+            Some(stag) => match other.tag {
+                Some(otag) => stag >= otag,
+                None => false
+            },
+            None => true
+        })
+    }
+}
+
 fn read_whitespace(rdr: io::Reader, ch: char) -> char {
     let mut nch = ch;