@@ -22,6 +22,7 @@ use vec;
22
22
use vec:: OwnedVector ;
23
23
use to_str:: ToStr ;
24
24
use str:: StrSlice ;
25
+ use fmt;
25
26
26
27
/// `Result` is a type that represents either success (`Ok`) or failure (`Err`).
27
28
///
@@ -290,7 +291,7 @@ pub trait AsResult<T, E> {
290
291
291
292
impl < T : Clone , E > option:: ToOption < T > for Result < T , E > {
292
293
#[ inline]
293
- fn to_option ( & self ) -> Option < T > {
294
+ fn to_option ( & self ) -> Option < T > {
294
295
match * self {
295
296
Ok ( ref t) => Some ( t. clone ( ) ) ,
296
297
Err ( _) => None ,
@@ -300,7 +301,7 @@ impl<T: Clone, E> option::ToOption<T> for Result<T, E> {
300
301
301
302
impl < T , E > option:: IntoOption < T > for Result < T , E > {
302
303
#[ inline]
303
- fn into_option ( self ) -> Option < T > {
304
+ fn into_option ( self ) -> Option < T > {
304
305
match self {
305
306
Ok ( t) => Some ( t) ,
306
307
Err ( _) => None ,
@@ -310,7 +311,7 @@ impl<T, E> option::IntoOption<T> for Result<T, E> {
310
311
311
312
impl < T , E > option:: AsOption < T > for Result < T , E > {
312
313
#[ inline]
313
- fn as_option < ' a > ( & ' a self ) -> Option < & ' a T > {
314
+ fn as_option < ' a > ( & ' a self ) -> Option < & ' a T > {
314
315
match * self {
315
316
Ok ( ref t) => Some ( t) ,
316
317
Err ( _) => None ,
@@ -340,7 +341,7 @@ impl<T, E> AsResult<T, E> for Result<T, E> {
340
341
341
342
impl < T : Clone , E : Clone > either:: ToEither < E , T > for Result < T , E > {
342
343
#[ inline]
343
- fn to_either ( & self ) -> either:: Either < E , T > {
344
+ fn to_either ( & self ) -> either:: Either < E , T > {
344
345
match * self {
345
346
Ok ( ref t) => either:: Right ( t. clone ( ) ) ,
346
347
Err ( ref e) => either:: Left ( e. clone ( ) ) ,
@@ -350,7 +351,7 @@ impl<T: Clone, E: Clone> either::ToEither<E, T> for Result<T, E> {
350
351
351
352
impl < T , E > either:: IntoEither < E , T > for Result < T , E > {
352
353
#[ inline]
353
- fn into_either ( self ) -> either:: Either < E , T > {
354
+ fn into_either ( self ) -> either:: Either < E , T > {
354
355
match self {
355
356
Ok ( t) => either:: Right ( t) ,
356
357
Err ( e) => either:: Left ( e) ,
@@ -360,14 +361,34 @@ impl<T, E> either::IntoEither<E, T> for Result<T, E> {
360
361
361
362
impl < T , E > either:: AsEither < E , T > for Result < T , E > {
362
363
#[ inline]
363
- fn as_either < ' a > ( & ' a self ) -> either:: Either < & ' a E , & ' a T > {
364
+ fn as_either < ' a > ( & ' a self ) -> either:: Either < & ' a E , & ' a T > {
364
365
match * self {
365
366
Ok ( ref t) => either:: Right ( t) ,
366
367
Err ( ref e) => either:: Left ( e) ,
367
368
}
368
369
}
369
370
}
370
371
372
+ impl < T : ToStr , E : ToStr > ToStr for Result < T , E > {
373
+ #[ inline]
374
+ fn to_str ( & self ) -> ~str {
375
+ match * self {
376
+ Ok ( ref t) => format ! ( "Ok({:s})" , t. to_str( ) ) ,
377
+ Err ( ref e) => format ! ( "Err({:s})" , e. to_str( ) )
378
+ }
379
+ }
380
+ }
381
+
382
+ impl < T : fmt:: Default , E : fmt:: Default > fmt:: Default for Result < T , E > {
383
+ #[ inline]
384
+ fn fmt ( s : & Result < T , E > , f : & mut fmt:: Formatter ) {
385
+ match * s {
386
+ Ok ( ref t) => write ! ( f. buf, "Ok({})" , * t) ,
387
+ Err ( ref e) => write ! ( f. buf, "Err({})" , * e)
388
+ }
389
+ }
390
+ }
391
+
371
392
/// Takes each element in the iterator: if it is an error, no further
372
393
/// elements are taken, and the error is returned.
373
394
/// Should no error occur, a vector containing the values of each Result
@@ -441,6 +462,8 @@ mod tests {
441
462
use option;
442
463
use str:: OwnedStr ;
443
464
use vec:: ImmutableVector ;
465
+ use to_str:: ToStr ;
466
+ use fmt:: Default ;
444
467
445
468
pub fn op1 ( ) -> Result < int , ~str > { Ok ( 666 ) }
446
469
pub fn op2 ( ) -> Result < int , ~str > { Err ( ~"sadface") }
@@ -659,4 +682,22 @@ mod tests {
659
682
assert_eq!(ok.as_either().unwrap_right(), &100);
660
683
assert_eq!(err.as_either().unwrap_left(), &404);
661
684
}
685
+
686
+ #[test]
687
+ pub fn test_to_str() {
688
+ let ok: Result<int, ~str> = Ok(100);
689
+ let err: Result<int, ~str> = Err(~" Err ");
690
+
691
+ assert_eq!(ok.to_str(), ~" Ok ( 100 ) ");
692
+ assert_eq!(err.to_str(), ~" Err ( Err ) ");
693
+ }
694
+
695
+ #[test]
696
+ pub fn test_fmt_default() {
697
+ let ok: Result<int, ~str> = Ok(100);
698
+ let err: Result<int, ~str> = Err(~" Err ");
699
+
700
+ assert_eq!(format!(" { } ", ok), ~" Ok ( 100 ) ");
701
+ assert_eq!(format!(" { } ", err), ~" Err ( Err ) " ) ;
702
+ }
662
703
}
0 commit comments