@@ -197,17 +197,23 @@ macro_rules! tuple_impls {
197
197
}
198
198
199
199
#[ cfg( not( test) ) ]
200
- impl <$( $T: Ord ) ,+> Ord for ( $( $T, ) +) {
200
+ impl <$( $T: Ord + Eq ) ,+> Ord for ( $( $T, ) +) {
201
201
#[ inline]
202
202
fn lt( & self , other: & ( $( $T, ) +) ) -> bool {
203
- lexical_lt! ( $( self . $get_ref_fn( ) , other. $get_ref_fn( ) ) ,+)
203
+ lexical_ord! ( lt , $( self . $get_ref_fn( ) , other. $get_ref_fn( ) ) ,+)
204
204
}
205
205
#[ inline]
206
- fn le( & self , other: & ( $( $T, ) +) ) -> bool { !( * other) . lt( & ( * self ) ) }
206
+ fn le( & self , other: & ( $( $T, ) +) ) -> bool {
207
+ lexical_ord!( le, $( self . $get_ref_fn( ) , other. $get_ref_fn( ) ) ,+)
208
+ }
207
209
#[ inline]
208
- fn ge( & self , other: & ( $( $T, ) +) ) -> bool { !( * self ) . lt( other) }
210
+ fn ge( & self , other: & ( $( $T, ) +) ) -> bool {
211
+ lexical_ord!( ge, $( self . $get_ref_fn( ) , other. $get_ref_fn( ) ) ,+)
212
+ }
209
213
#[ inline]
210
- fn gt( & self , other: & ( $( $T, ) +) ) -> bool { ( * other) . lt( & ( * self ) ) }
214
+ fn gt( & self , other: & ( $( $T, ) +) ) -> bool {
215
+ lexical_ord!( gt, $( self . $get_ref_fn( ) , other. $get_ref_fn( ) ) ,+)
216
+ }
211
217
}
212
218
213
219
#[ cfg( not( test) ) ]
@@ -234,17 +240,16 @@ macro_rules! tuple_impls {
234
240
}
235
241
}
236
242
237
- // Constructs an expression that performs a lexical less-than
238
- // ordering. The values are interleaved, so the macro invocation for
239
- // `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_lt!( a1, b1, a2, b2,
243
+ // Constructs an expression that performs a lexical ordering using method $rel.
244
+ // The values are interleaved, so the macro invocation for
245
+ // `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
240
246
// a3, b3)` (and similarly for `lexical_cmp`)
241
- macro_rules! lexical_lt {
242
- ( $a: expr, $b: expr, $( $rest_a: expr, $rest_b: expr) ,+) => {
243
- if * $a < * $b { true }
244
- else if !( * $b < * $a) { lexical_lt!( $( $rest_a, $rest_b) ,+) }
245
- else { false }
247
+ macro_rules! lexical_ord {
248
+ ( $rel: ident, $a: expr, $b: expr, $( $rest_a: expr, $rest_b: expr) ,+) => {
249
+ if * $a != * $b { lexical_ord!( $rel, $a, $b) }
250
+ else { lexical_ord!( $rel, $( $rest_a, $rest_b) ,+) }
246
251
} ;
247
- ( $a: expr, $b: expr) => { * $a < * $b } ;
252
+ ( $rel : ident , $ a: expr, $b: expr) => { ( * $a) . $rel ( $b ) } ;
248
253
}
249
254
250
255
macro_rules! lexical_cmp {
@@ -436,6 +441,8 @@ mod tests {
436
441
fn test_tuple_cmp ( ) {
437
442
let ( small, big) = ( ( 1 u, 2 u, 3 u) , ( 3 u, 2 u, 1 u) ) ;
438
443
444
+ let nan = 0.0 /0.0 ;
445
+
439
446
// Eq
440
447
assert_eq ! ( small, small) ;
441
448
assert_eq ! ( big, big) ;
@@ -456,6 +463,13 @@ mod tests {
456
463
assert ! ( big >= small) ;
457
464
assert ! ( big >= big) ;
458
465
466
+ assert ! ( !( ( 1.0 , 2.0 ) < ( nan, 3.0 ) ) ) ;
467
+ assert ! ( !( ( 1.0 , 2.0 ) <= ( nan, 3.0 ) ) ) ;
468
+ assert ! ( !( ( 1.0 , 2.0 ) > ( nan, 3.0 ) ) ) ;
469
+ assert ! ( !( ( 1.0 , 2.0 ) >= ( nan, 3.0 ) ) ) ;
470
+ assert ! ( ( ( 1.0 , 2.0 ) < ( 2.0 , nan) ) ) ;
471
+ assert ! ( !( ( 2.0 , 2.0 ) < ( 2.0 , nan) ) ) ;
472
+
459
473
// TotalEq
460
474
assert ! ( small. equals( & small) ) ;
461
475
assert ! ( big. equals( & big) ) ;
0 commit comments