Skip to content

Commit fa03c94

Browse files
committed
auto merge of #9892 : Kimundi/rust/ResultToStr, r=alexcrichton
2 parents 40180cd + abecd61 commit fa03c94

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

src/libstd/result.rs

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use vec;
2222
use vec::OwnedVector;
2323
use to_str::ToStr;
2424
use str::StrSlice;
25+
use fmt;
2526

2627
/// `Result` is a type that represents either success (`Ok`) or failure (`Err`).
2728
///
@@ -290,7 +291,7 @@ pub trait AsResult<T, E> {
290291

291292
impl<T: Clone, E> option::ToOption<T> for Result<T, E> {
292293
#[inline]
293-
fn to_option(&self)-> Option<T> {
294+
fn to_option(&self) -> Option<T> {
294295
match *self {
295296
Ok(ref t) => Some(t.clone()),
296297
Err(_) => None,
@@ -300,7 +301,7 @@ impl<T: Clone, E> option::ToOption<T> for Result<T, E> {
300301

301302
impl<T, E> option::IntoOption<T> for Result<T, E> {
302303
#[inline]
303-
fn into_option(self)-> Option<T> {
304+
fn into_option(self) -> Option<T> {
304305
match self {
305306
Ok(t) => Some(t),
306307
Err(_) => None,
@@ -310,7 +311,7 @@ impl<T, E> option::IntoOption<T> for Result<T, E> {
310311

311312
impl<T, E> option::AsOption<T> for Result<T, E> {
312313
#[inline]
313-
fn as_option<'a>(&'a self)-> Option<&'a T> {
314+
fn as_option<'a>(&'a self) -> Option<&'a T> {
314315
match *self {
315316
Ok(ref t) => Some(t),
316317
Err(_) => None,
@@ -340,7 +341,7 @@ impl<T, E> AsResult<T, E> for Result<T, E> {
340341

341342
impl<T: Clone, E: Clone> either::ToEither<E, T> for Result<T, E> {
342343
#[inline]
343-
fn to_either(&self)-> either::Either<E, T> {
344+
fn to_either(&self) -> either::Either<E, T> {
344345
match *self {
345346
Ok(ref t) => either::Right(t.clone()),
346347
Err(ref e) => either::Left(e.clone()),
@@ -350,7 +351,7 @@ impl<T: Clone, E: Clone> either::ToEither<E, T> for Result<T, E> {
350351

351352
impl<T, E> either::IntoEither<E, T> for Result<T, E> {
352353
#[inline]
353-
fn into_either(self)-> either::Either<E, T> {
354+
fn into_either(self) -> either::Either<E, T> {
354355
match self {
355356
Ok(t) => either::Right(t),
356357
Err(e) => either::Left(e),
@@ -360,14 +361,34 @@ impl<T, E> either::IntoEither<E, T> for Result<T, E> {
360361

361362
impl<T, E> either::AsEither<E, T> for Result<T, E> {
362363
#[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> {
364365
match *self {
365366
Ok(ref t) => either::Right(t),
366367
Err(ref e) => either::Left(e),
367368
}
368369
}
369370
}
370371

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+
371392
/// Takes each element in the iterator: if it is an error, no further
372393
/// elements are taken, and the error is returned.
373394
/// Should no error occur, a vector containing the values of each Result
@@ -441,6 +462,8 @@ mod tests {
441462
use option;
442463
use str::OwnedStr;
443464
use vec::ImmutableVector;
465+
use to_str::ToStr;
466+
use fmt::Default;
444467

445468
pub fn op1() -> Result<int, ~str> { Ok(666) }
446469
pub fn op2() -> Result<int, ~str> { Err(~"sadface") }
@@ -659,4 +682,22 @@ mod tests {
659682
assert_eq!(ok.as_either().unwrap_right(), &100);
660683
assert_eq!(err.as_either().unwrap_left(), &404);
661684
}
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+
}
662703
}

0 commit comments

Comments
 (0)