Skip to content

Commit 1fe4bd0

Browse files
committed
std: added either::flip, to_result and result::to_either
1 parent fd1dd76 commit 1fe4bd0

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/libcore/either.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,31 @@ fn partition<copy T, copy U>(eithers: [t<T, U>])
7878
ret {lefts: lefts, rights: rights};
7979
}
8080

81+
/*
82+
Function: flip
83+
84+
Flips between left and right of a given either
85+
*/
86+
pure fn flip<copy T, copy U>(eith: t<T, U>) -> t<U, T> {
87+
alt eith {
88+
right(r) { left(r) }
89+
left(l) { right(l) }
90+
}
91+
}
92+
93+
/*
94+
Function: to_result
95+
96+
Converts either::t to a result::t, making the "right" choice
97+
an ok result, and the "left" choice a fail
98+
*/
99+
pure fn to_result<copy T, copy U>(eith: t<T, U>) -> result::t<U, T> {
100+
alt eith {
101+
right(r) { result::ok(r) }
102+
left(l) { result::err(l) }
103+
}
104+
}
105+
81106
//
82107
// Local Variables:
83108
// mode: rust

src/libcore/result.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn get<T, U>(res: t<T, U>) -> T {
4242
ok(t) { t }
4343
err(_) {
4444
// FIXME: Serialize the error value
45-
// and include it in the fail message
45+
// and include it in the fail message (maybe just note it)
4646
fail "get called on error result";
4747
}
4848
}
@@ -71,7 +71,7 @@ Function: success
7171
7272
Returns true if the result is <ok>
7373
*/
74-
fn success<T, U>(res: t<T, U>) -> bool {
74+
pure fn success<T, U>(res: t<T, U>) -> bool {
7575
alt res {
7676
ok(_) { true }
7777
err(_) { false }
@@ -83,10 +83,17 @@ Function: failure
8383
8484
Returns true if the result is <error>
8585
*/
86-
fn failure<T, U>(res: t<T, U>) -> bool {
86+
pure fn failure<T, U>(res: t<T, U>) -> bool {
8787
!success(res)
8888
}
8989

90+
pure fn to_either<copy T, copy U>(res: t<U, T>) -> either::t<T, U> {
91+
alt res {
92+
ok(res) { either::right(res) }
93+
err(fail_) { either::left(fail_) }
94+
}
95+
}
96+
9097
/*
9198
Function: chain
9299

0 commit comments

Comments
 (0)