36
36
#![ doc( html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png" ,
37
37
html_favicon_url = "http://www.rust-lang.org/favicon.ico" ,
38
38
html_root_url = "http://doc.rust-lang.org/0.11.0/" ) ]
39
+ #![ feature( default_type_params) ]
39
40
40
41
use std:: char;
41
42
use std:: cmp;
42
- use std:: fmt;
43
43
use std:: fmt:: Show ;
44
- use std:: option :: { Option , Some , None } ;
45
- use std:: string :: String ;
44
+ use std:: fmt ;
45
+ use std:: hash ;
46
46
47
47
/// An identifier in the pre-release or build metadata. If the identifier can
48
48
/// be parsed as a decimal value, it will be represented with `Numeric`.
49
- #[ deriving( Clone , PartialEq ) ]
49
+ #[ deriving( Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
50
50
#[ allow( missing_doc) ]
51
51
pub enum Identifier {
52
52
Numeric ( uint ) ,
53
53
AlphaNumeric ( String )
54
54
}
55
55
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
-
68
56
impl fmt:: Show for Identifier {
69
57
#[ inline]
70
58
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
@@ -77,7 +65,7 @@ impl fmt::Show for Identifier {
77
65
78
66
79
67
/// Represents a version number conforming to the semantic versioning scheme.
80
- #[ deriving( Clone ) ]
68
+ #[ deriving( Clone , Eq ) ]
81
69
pub struct Version {
82
70
/// The major version, to be incremented on incompatible changes.
83
71
pub major : uint ,
@@ -129,35 +117,49 @@ impl cmp::PartialEq for Version {
129
117
}
130
118
131
119
impl cmp:: PartialOrd for Version {
132
- #[ inline]
133
120
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 => { }
136
129
r => return r,
137
130
}
138
131
139
- match self . minor . partial_cmp ( & other. minor ) {
140
- Some ( Equal ) => { }
132
+ match self . minor . cmp ( & other. minor ) {
133
+ Equal => { }
141
134
r => return r,
142
135
}
143
136
144
- match self . patch . partial_cmp ( & other. patch ) {
145
- Some ( Equal ) => { }
137
+ match self . patch . cmp ( & other. patch ) {
138
+ Equal => { }
146
139
r => return r,
147
140
}
148
141
149
142
// NB: semver spec says 0.0.0-pre < 0.0.0
150
143
// but the version of ord defined for vec
151
144
// says that [] < [pre] so we alter it here
152
145
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 )
157
150
}
158
151
}
159
152
}
160
153
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
+
161
163
fn take_nonempty_prefix < T : Iterator < char > > ( rdr : & mut T , pred: |char| -> bool)
162
164
-> ( String , Option < char > ) {
163
165
let mut buf = String :: new ( ) ;
0 commit comments