File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -116,6 +116,19 @@ totalord_impl!(i64)
116
116
totalord_impl ! ( int)
117
117
totalord_impl ! ( uint)
118
118
119
+ /**
120
+ Return `o1` if it is not `Equal`, otherwise `o2`. Simulates the
121
+ lexical ordering on a type `(int, int)`.
122
+ */
123
+ // used in deriving code in libsyntax
124
+ #[ inline( always) ]
125
+ pub fn lexical_ordering ( o1 : Ordering , o2 : Ordering ) -> Ordering {
126
+ match o1 {
127
+ Equal => o2,
128
+ _ => o1
129
+ }
130
+ }
131
+
119
132
/**
120
133
* Trait for values that can be compared for a sort-order.
121
134
*
@@ -184,6 +197,8 @@ pub fn max<T:Ord>(v1: T, v2: T) -> T {
184
197
185
198
#[ cfg( test) ]
186
199
mod test {
200
+ use super :: lexical_ordering;
201
+
187
202
#[ test]
188
203
fn test_int_totalord ( ) {
189
204
assert_eq ! ( 5 . cmp( & 10 ) , Less ) ;
@@ -204,4 +219,16 @@ mod test {
204
219
assert ! ( Less < Equal ) ;
205
220
assert_eq ! ( Greater . cmp( & Less ) , Greater ) ;
206
221
}
222
+
223
+ #[ test]
224
+ fn test_lexical_ordering ( ) {
225
+ fn t ( o1 : Ordering , o2 : Ordering , e : Ordering ) {
226
+ assert_eq ! ( lexical_ordering( o1, o2) , e) ;
227
+ }
228
+ for [ Less , Equal , Greater ] . each |& o| {
229
+ t( Less , o, Less ) ;
230
+ t ( Equal , o, o) ;
231
+ t ( Greater , o, Greater ) ;
232
+ }
233
+ }
207
234
}
You can’t perform that action at this time.
0 commit comments