]> git.lizzy.rs Git - rust.git/commitdiff
std: Add some implementation of common traits
authorAlex Crichton <alex@alexcrichton.com>
Thu, 10 Jul 2014 01:16:16 +0000 (18:16 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Thu, 10 Jul 2014 14:50:58 +0000 (07:50 -0700)
- semver::Version is now Eq, Ord, and Hash
- Path is now PartialOrd and Ord

src/libsemver/lib.rs
src/libstd/path/posix.rs
src/libstd/path/windows.rs

index 22664ba26281e362c6333fda8fd52769f956bdfa..1832e7c0dff52e7b1f89a23e04bf890a83cca441 100644 (file)
 #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
        html_favicon_url = "http://www.rust-lang.org/favicon.ico",
        html_root_url = "http://doc.rust-lang.org/0.11.0/")]
+#![feature(default_type_params)]
 
 use std::char;
 use std::cmp;
-use std::fmt;
 use std::fmt::Show;
-use std::option::{Option, Some, None};
-use std::string::String;
+use std::fmt;
+use std::hash;
 
 /// An identifier in the pre-release or build metadata. If the identifier can
 /// be parsed as a decimal value, it will be represented with `Numeric`.
-#[deriving(Clone, PartialEq)]
+#[deriving(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
 #[allow(missing_doc)]
 pub enum Identifier {
     Numeric(uint),
     AlphaNumeric(String)
 }
 
-impl cmp::PartialOrd for Identifier {
-    #[inline]
-    fn partial_cmp(&self, other: &Identifier) -> Option<Ordering> {
-        match (self, other) {
-            (&Numeric(a), &Numeric(ref b)) => a.partial_cmp(b),
-            (&Numeric(_), _) => Some(Less),
-            (&AlphaNumeric(ref a), &AlphaNumeric(ref b)) => a.partial_cmp(b),
-            (&AlphaNumeric(_), _) => Some(Greater)
-        }
-    }
-}
-
 impl fmt::Show for Identifier {
     #[inline]
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -77,7 +65,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 
 
 /// Represents a version number conforming to the semantic versioning scheme.
-#[deriving(Clone)]
+#[deriving(Clone, Eq)]
 pub struct Version {
     /// The major version, to be incremented on incompatible changes.
     pub major: uint,
@@ -129,20 +117,25 @@ fn eq(&self, other: &Version) -> bool {
 }
 
 impl cmp::PartialOrd for Version {
-    #[inline]
     fn partial_cmp(&self, other: &Version) -> Option<Ordering> {
-        match self.major.partial_cmp(&other.major) {
-            Some(Equal) => {}
+        Some(self.cmp(other))
+    }
+}
+
+impl cmp::Ord for Version {
+    fn cmp(&self, other: &Version) -> Ordering {
+        match self.major.cmp(&other.major) {
+            Equal => {}
             r => return r,
         }
 
-        match self.minor.partial_cmp(&other.minor) {
-            Some(Equal) => {}
+        match self.minor.cmp(&other.minor) {
+            Equal => {}
             r => return r,
         }
 
-        match self.patch.partial_cmp(&other.patch) {
-            Some(Equal) => {}
+        match self.patch.cmp(&other.patch) {
+            Equal => {}
             r => return r,
         }
 
@@ -150,14 +143,23 @@ fn partial_cmp(&self, other: &Version) -> Option<Ordering> {
         // but the version of ord defined for vec
         // says that [] < [pre] so we alter it here
         match (self.pre.len(), other.pre.len()) {
-            (0, 0) => Some(Equal),
-            (0, _) => Some(Greater),
-            (_, 0) => Some(Less),
-            (_, _) => self.pre.partial_cmp(&other.pre)
+            (0, 0) => Equal,
+            (0, _) => Greater,
+            (_, 0) => Less,
+            (_, _) => self.pre.cmp(&other.pre)
         }
     }
 }
 
+impl<S: hash::Writer> hash::Hash<S> for Version {
+    fn hash(&self, into: &mut S) {
+        self.major.hash(into);
+        self.minor.hash(into);
+        self.patch.hash(into);
+        self.pre.hash(into);
+    }
+}
+
 fn take_nonempty_prefix<T:Iterator<char>>(rdr: &mut T, pred: |char| -> bool)
                         -> (String, Option<char>) {
     let mut buf = String::new();
index 5c6e140bd29b30aa2827273f5882a9f95719bea6..007686aa05cd5865cf03a4f4d52344653ff462df 100644 (file)
@@ -12,7 +12,7 @@
 
 use c_str::{CString, ToCStr};
 use clone::Clone;
-use cmp::{PartialEq, Eq};
+use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
 use collections::Collection;
 use from_str::FromStr;
 use hash;
@@ -68,6 +68,18 @@ fn eq(&self, other: &Path) -> bool {
 
 impl Eq for Path {}
 
+impl PartialOrd for Path {
+    fn partial_cmp(&self, other: &Path) -> Option<Ordering> {
+        Some(self.cmp(other))
+    }
+}
+
+impl Ord for Path {
+    fn cmp(&self, other: &Path) -> Ordering {
+        self.repr.cmp(&other.repr)
+    }
+}
+
 impl FromStr for Path {
     fn from_str(s: &str) -> Option<Path> {
         Path::new_opt(s)
index 5e42d740e0333a0890e02ef8a796e80901fa0931..88ae0d4837e569febda914a30ad4ab82d9a31800 100644 (file)
@@ -13,7 +13,7 @@
 use ascii::AsciiCast;
 use c_str::{CString, ToCStr};
 use clone::Clone;
-use cmp::{PartialEq, Eq};
+use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
 use collections::Collection;
 use from_str::FromStr;
 use hash;
@@ -90,6 +90,18 @@ fn eq(&self, other: &Path) -> bool {
 
 impl Eq for Path {}
 
+impl PartialOrd for Path {
+    fn partial_cmp(&self, other: &Path) -> Option<Ordering> {
+        Some(self.cmp(other))
+    }
+}
+
+impl Ord for Path {
+    fn cmp(&self, other: &Path) -> Ordering {
+        self.repr.cmp(&other.repr)
+    }
+}
+
 impl FromStr for Path {
     fn from_str(s: &str) -> Option<Path> {
         Path::new_opt(s)