diff --git a/src/libcore/either.rs b/src/libcore/either.rs index 89d47b20746a0..60ad12d2e026a 100644 --- a/src/libcore/either.rs +++ b/src/libcore/either.rs @@ -78,6 +78,31 @@ fn partition(eithers: [t]) ret {lefts: lefts, rights: rights}; } +/* +Function: flip + +Flips between left and right of a given either +*/ +pure fn flip(eith: t) -> t { + alt eith { + right(r) { left(r) } + left(l) { right(l) } + } +} + +/* +Function: to_result + +Converts either::t to a result::t, making the "right" choice +an ok result, and the "left" choice a fail +*/ +pure fn to_result(eith: t) -> result::t { + alt eith { + right(r) { result::ok(r) } + left(l) { result::err(l) } + } +} + // // Local Variables: // mode: rust diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 550f53470bbe8..84fbc3b7f90e0 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -42,7 +42,7 @@ fn get(res: t) -> T { ok(t) { t } err(_) { // FIXME: Serialize the error value - // and include it in the fail message + // and include it in the fail message (maybe just note it) fail "get called on error result"; } } @@ -71,7 +71,7 @@ Function: success Returns true if the result is */ -fn success(res: t) -> bool { +pure fn success(res: t) -> bool { alt res { ok(_) { true } err(_) { false } @@ -83,10 +83,17 @@ Function: failure Returns true if the result is */ -fn failure(res: t) -> bool { +pure fn failure(res: t) -> bool { !success(res) } +pure fn to_either(res: t) -> either::t { + alt res { + ok(res) { either::right(res) } + err(fail_) { either::left(fail_) } + } +} + /* Function: chain