@@ -274,17 +274,12 @@ impl<T: Clone + Integer + PartialOrd>
274
274
Num for Ratio < T > { }
275
275
276
276
/* String conversions */
277
- impl < T : fmt:: Show + Eq + One > fmt:: Show for Ratio < T > {
278
- /// Renders as `numer/denom`. If denom=1, renders as numer.
277
+ impl < T : fmt:: Show > fmt:: Show for Ratio < T > {
278
+ /// Renders as `numer/denom`.
279
279
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
280
- if self . denom == One :: one ( ) {
281
- write ! ( f, "{}" , self . numer)
282
- } else {
283
- write ! ( f, "{}/{}" , self . numer, self . denom)
284
- }
280
+ write ! ( f, "{}/{}" , self . numer, self . denom)
285
281
}
286
282
}
287
-
288
283
impl < T : ToStrRadix > ToStrRadix for Ratio < T > {
289
284
/// Renders as `numer/denom` where the numbers are in base `radix`.
290
285
fn to_str_radix ( & self , radix : uint ) -> String {
@@ -296,20 +291,21 @@ impl<T: ToStrRadix> ToStrRadix for Ratio<T> {
296
291
297
292
impl < T : FromStr + Clone + Integer + PartialOrd >
298
293
FromStr for Ratio < T > {
299
- /// Parses `numer/denom` or just `numer`
294
+ /// Parses `numer/denom`.
300
295
fn from_str ( s : & str ) -> Option < Ratio < T > > {
301
- let mut split = s. splitn ( '/' , 1 ) ;
302
-
303
- let num = split. next ( ) . and_then ( |n| FromStr :: from_str ( n) ) ;
304
- let den = split. next ( ) . or ( Some ( "1" ) ) . and_then ( |d| FromStr :: from_str ( d) ) ;
305
-
306
- match ( num, den) {
307
- ( Some ( n) , Some ( d) ) => Some ( Ratio :: new ( n, d) ) ,
308
- _ => None
296
+ let split: Vec < & str > = s. splitn ( '/' , 1 ) . collect ( ) ;
297
+ if split. len ( ) < 2 {
298
+ return None
309
299
}
300
+ let a_option: Option < T > = FromStr :: from_str ( * split. get ( 0 ) ) ;
301
+ a_option. and_then ( |a| {
302
+ let b_option: Option < T > = FromStr :: from_str ( * split. get ( 1 ) ) ;
303
+ b_option. and_then ( |b| {
304
+ Some ( Ratio :: new ( a. clone ( ) , b. clone ( ) ) )
305
+ } )
306
+ } )
310
307
}
311
308
}
312
-
313
309
impl < T : FromStrRadix + Clone + Integer + PartialOrd >
314
310
FromStrRadix for Ratio < T > {
315
311
/// Parses `numer/denom` where the numbers are in base `radix`.
@@ -433,13 +429,6 @@ mod test {
433
429
assert ! ( !_neg1_2. is_integer( ) ) ;
434
430
}
435
431
436
- #[ test]
437
- fn test_show ( ) {
438
- assert_eq ! ( format!( "{}" , _2) , "2" . to_string( ) ) ;
439
- assert_eq ! ( format!( "{}" , _1_2) , "1/2" . to_string( ) ) ;
440
- assert_eq ! ( format!( "{}" , _0) , "0" . to_string( ) ) ;
441
- assert_eq ! ( format!( "{}" , Ratio :: from_integer( -2 i) ) , "-2" . to_string( ) ) ;
442
- }
443
432
444
433
mod arith {
445
434
use super :: { _0, _1, _2, _1_2, _3_2, _neg1_2, to_big} ;
@@ -573,11 +562,11 @@ mod test {
573
562
assert_eq ! ( FromStr :: from_str( s. as_slice( ) ) , Some ( r) ) ;
574
563
assert_eq ! ( r. to_str( ) , s) ;
575
564
}
576
- test ( _1, "1" . to_string ( ) ) ;
577
- test ( _0, "0" . to_string ( ) ) ;
565
+ test ( _1, "1/1 " . to_string ( ) ) ;
566
+ test ( _0, "0/1 " . to_string ( ) ) ;
578
567
test ( _1_2, "1/2" . to_string ( ) ) ;
579
568
test ( _3_2, "3/2" . to_string ( ) ) ;
580
- test ( _2, "2" . to_string ( ) ) ;
569
+ test ( _2, "2/1 " . to_string ( ) ) ;
581
570
test ( _neg1_2, "-1/2" . to_string ( ) ) ;
582
571
}
583
572
#[ test]
0 commit comments