15
15
use clone:: Clone ;
16
16
#[ cfg( not( test) ) ] use cmp:: * ;
17
17
#[ cfg( not( test) ) ] use default:: Default ;
18
+ use fmt;
19
+ use result:: { Ok , Err } ;
18
20
19
21
/// Method extensions to pairs where both types satisfy the `Clone` bound
20
22
pub trait CloneableTuple < T , U > {
@@ -176,6 +178,12 @@ macro_rules! tuple_impls {
176
178
( $( { let x: $T = Default :: default ( ) ; x} , ) +)
177
179
}
178
180
}
181
+
182
+ impl <$( $T: fmt:: Show ) ,+> fmt:: Show for ( $( $T, ) +) {
183
+ fn fmt( & self , f: & mut fmt:: Formatter ) -> fmt:: Result {
184
+ write_tuple!( f. buf, $( self . $get_ref_fn( ) ) ,+)
185
+ }
186
+ }
179
187
) +
180
188
}
181
189
}
@@ -202,6 +210,17 @@ macro_rules! lexical_cmp {
202
210
( $a: expr, $b: expr) => { ( $a) . cmp( $b) } ;
203
211
}
204
212
213
+ macro_rules! write_tuple {
214
+ ( $buf: expr, $x: expr) => (
215
+ write!( $buf, "({},)" , * $x)
216
+ ) ;
217
+ ( $buf: expr, $hd: expr, $( $tl: expr) ,+) => ( {
218
+ if_ok!( write!( $buf, "(" ) ) ;
219
+ if_ok!( write!( $buf, "{}" , * $hd) ) ;
220
+ $( if_ok!( write!( $buf, ", {}" , * $tl) ) ; ) +
221
+ write!( $buf, ")" )
222
+ } ) ;
223
+ }
205
224
206
225
tuple_impls ! {
207
226
( Tuple1 , ImmutableTuple1 ) {
@@ -422,4 +441,11 @@ mod tests {
422
441
assert_eq!(small.cmp(&big), Less);
423
442
assert_eq!(big.cmp(&small), Greater);
424
443
}
444
+
445
+ #[test]
446
+ fn test_show() {
447
+ assert_eq!(format!(" { } ", (1,)), ~" ( 1 , ) ");
448
+ assert_eq!(format!(" { } ", (1, true)), ~" ( 1 , true ) ");
449
+ assert_eq!(format!(" { } ", (1, ~" hi", true)), ~" ( 1 , hi, true ) " ) ;
450
+ }
425
451
}
0 commit comments