Skip to content

Commit 85b82c7

Browse files
committed
libcore: combine cmp::Ordering instances in lexical order.
1 parent 3698ea7 commit 85b82c7

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/libcore/cmp.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,19 @@ totalord_impl!(i64)
116116
totalord_impl!(int)
117117
totalord_impl!(uint)
118118

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+
119132
/**
120133
* Trait for values that can be compared for a sort-order.
121134
*
@@ -184,6 +197,8 @@ pub fn max<T:Ord>(v1: T, v2: T) -> T {
184197

185198
#[cfg(test)]
186199
mod test {
200+
use super::lexical_ordering;
201+
187202
#[test]
188203
fn test_int_totalord() {
189204
assert_eq!(5.cmp(&10), Less);
@@ -204,4 +219,16 @@ mod test {
204219
assert!(Less < Equal);
205220
assert_eq!(Greater.cmp(&Less), Greater);
206221
}
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+
}
207234
}

0 commit comments

Comments
 (0)