Skip to content

Commit b57d272

Browse files
committed
auto merge of #15564 : alexcrichton/rust/moar-hash, r=huonw
- semver::Version is now Eq, Ord, and Hash - Path is now PartialOrd and Ord
2 parents 0e80dbe + 8aa8ca7 commit b57d272

File tree

3 files changed

+56
-30
lines changed

3 files changed

+56
-30
lines changed

src/libsemver/lib.rs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,23 @@
3636
#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
3737
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
3838
html_root_url = "http://doc.rust-lang.org/0.11.0/")]
39+
#![feature(default_type_params)]
3940

4041
use std::char;
4142
use std::cmp;
42-
use std::fmt;
4343
use std::fmt::Show;
44-
use std::option::{Option, Some, None};
45-
use std::string::String;
44+
use std::fmt;
45+
use std::hash;
4646

4747
/// An identifier in the pre-release or build metadata. If the identifier can
4848
/// be parsed as a decimal value, it will be represented with `Numeric`.
49-
#[deriving(Clone, PartialEq)]
49+
#[deriving(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
5050
#[allow(missing_doc)]
5151
pub enum Identifier {
5252
Numeric(uint),
5353
AlphaNumeric(String)
5454
}
5555

56-
impl cmp::PartialOrd for Identifier {
57-
#[inline]
58-
fn partial_cmp(&self, other: &Identifier) -> Option<Ordering> {
59-
match (self, other) {
60-
(&Numeric(a), &Numeric(ref b)) => a.partial_cmp(b),
61-
(&Numeric(_), _) => Some(Less),
62-
(&AlphaNumeric(ref a), &AlphaNumeric(ref b)) => a.partial_cmp(b),
63-
(&AlphaNumeric(_), _) => Some(Greater)
64-
}
65-
}
66-
}
67-
6856
impl fmt::Show for Identifier {
6957
#[inline]
7058
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -77,7 +65,7 @@ impl fmt::Show for Identifier {
7765

7866

7967
/// Represents a version number conforming to the semantic versioning scheme.
80-
#[deriving(Clone)]
68+
#[deriving(Clone, Eq)]
8169
pub struct Version {
8270
/// The major version, to be incremented on incompatible changes.
8371
pub major: uint,
@@ -129,35 +117,49 @@ impl cmp::PartialEq for Version {
129117
}
130118

131119
impl cmp::PartialOrd for Version {
132-
#[inline]
133120
fn partial_cmp(&self, other: &Version) -> Option<Ordering> {
134-
match self.major.partial_cmp(&other.major) {
135-
Some(Equal) => {}
121+
Some(self.cmp(other))
122+
}
123+
}
124+
125+
impl cmp::Ord for Version {
126+
fn cmp(&self, other: &Version) -> Ordering {
127+
match self.major.cmp(&other.major) {
128+
Equal => {}
136129
r => return r,
137130
}
138131

139-
match self.minor.partial_cmp(&other.minor) {
140-
Some(Equal) => {}
132+
match self.minor.cmp(&other.minor) {
133+
Equal => {}
141134
r => return r,
142135
}
143136

144-
match self.patch.partial_cmp(&other.patch) {
145-
Some(Equal) => {}
137+
match self.patch.cmp(&other.patch) {
138+
Equal => {}
146139
r => return r,
147140
}
148141

149142
// NB: semver spec says 0.0.0-pre < 0.0.0
150143
// but the version of ord defined for vec
151144
// says that [] < [pre] so we alter it here
152145
match (self.pre.len(), other.pre.len()) {
153-
(0, 0) => Some(Equal),
154-
(0, _) => Some(Greater),
155-
(_, 0) => Some(Less),
156-
(_, _) => self.pre.partial_cmp(&other.pre)
146+
(0, 0) => Equal,
147+
(0, _) => Greater,
148+
(_, 0) => Less,
149+
(_, _) => self.pre.cmp(&other.pre)
157150
}
158151
}
159152
}
160153

154+
impl<S: hash::Writer> hash::Hash<S> for Version {
155+
fn hash(&self, into: &mut S) {
156+
self.major.hash(into);
157+
self.minor.hash(into);
158+
self.patch.hash(into);
159+
self.pre.hash(into);
160+
}
161+
}
162+
161163
fn take_nonempty_prefix<T:Iterator<char>>(rdr: &mut T, pred: |char| -> bool)
162164
-> (String, Option<char>) {
163165
let mut buf = String::new();

src/libstd/path/posix.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
use c_str::{CString, ToCStr};
1414
use clone::Clone;
15-
use cmp::{PartialEq, Eq};
15+
use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
1616
use collections::Collection;
1717
use from_str::FromStr;
1818
use hash;
@@ -68,6 +68,18 @@ impl PartialEq for Path {
6868

6969
impl Eq for Path {}
7070

71+
impl PartialOrd for Path {
72+
fn partial_cmp(&self, other: &Path) -> Option<Ordering> {
73+
Some(self.cmp(other))
74+
}
75+
}
76+
77+
impl Ord for Path {
78+
fn cmp(&self, other: &Path) -> Ordering {
79+
self.repr.cmp(&other.repr)
80+
}
81+
}
82+
7183
impl FromStr for Path {
7284
fn from_str(s: &str) -> Option<Path> {
7385
Path::new_opt(s)

src/libstd/path/windows.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use ascii::AsciiCast;
1414
use c_str::{CString, ToCStr};
1515
use clone::Clone;
16-
use cmp::{PartialEq, Eq};
16+
use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
1717
use collections::Collection;
1818
use from_str::FromStr;
1919
use hash;
@@ -90,6 +90,18 @@ impl PartialEq for Path {
9090

9191
impl Eq for Path {}
9292

93+
impl PartialOrd for Path {
94+
fn partial_cmp(&self, other: &Path) -> Option<Ordering> {
95+
Some(self.cmp(other))
96+
}
97+
}
98+
99+
impl Ord for Path {
100+
fn cmp(&self, other: &Path) -> Ordering {
101+
self.repr.cmp(&other.repr)
102+
}
103+
}
104+
93105
impl FromStr for Path {
94106
fn from_str(s: &str) -> Option<Path> {
95107
Path::new_opt(s)

0 commit comments

Comments
 (0)