diff --git a/src/lib/result.rs b/src/lib/result.rs index ba5ead5d4db..8b07138d8d0 100644 --- a/src/lib/result.rs +++ b/src/lib/result.rs @@ -19,11 +19,11 @@ tag t { */ ok(T); /* - Variant: error + Variant: err Contains the error value */ - error(U); + err(U); } /* Section: Operations */ @@ -40,14 +40,14 @@ If the result is an error fn get(res: t) -> T { alt res { ok(t) { t } - error(_) { + err(_) { fail "get called on error result"; } } } /* -Function: get +Function: get_err Get the value out of an error result @@ -55,9 +55,9 @@ Failure: If the result is not an error */ -fn get_error(res: t) -> U { +fn get_err(res: t) -> U { alt res { - error(u) { u } + err(u) { u } ok(_) { fail "get_error called on ok result"; } @@ -72,7 +72,7 @@ Returns true if the result is fn success(res: t) -> bool { alt res { ok(_) { true } - error(_) { false } + err(_) { false } } } @@ -83,4 +83,27 @@ Returns true if the result is */ fn failure(res: t) -> bool { !success(res) -} \ No newline at end of file +} + +/* +Function: chain + +Call a function based on a previous result + +If `res` is then the value is extracted and passed to `op` whereupon +`op`s result is returned. if `res` is then it is immediately returned. +This function can be used to compose the results of two functions. + +Example: + +> let res = chain(read_file(file), { |buf] +> ok(parse_buf(buf)) +> }) + +*/ +fn chain(res: t, op: block(T) -> t) -> t { + alt res { + ok(t) { op(t) } + err(e) { err(e) } + } +} diff --git a/src/test/stdtest/result.rs b/src/test/stdtest/result.rs new file mode 100644 index 00000000000..9a7b390a523 --- /dev/null +++ b/src/test/stdtest/result.rs @@ -0,0 +1,17 @@ +import std::result; + +fn op1() -> result::t { result::ok(666) } + +fn op2(&&i: int) -> result::t { result::ok(i as uint + 1u) } + +fn op3() -> result::t { result::err("sadface") } + +#[test] +fn chain_success() { + assert result::get(result::chain(op1(), op2)) == 667u; +} + +#[test] +fn chain_failure() { + assert result::get_err(result::chain(op3(), op2)) == "sadface"; +} \ No newline at end of file diff --git a/src/test/stdtest/stdtest.rc b/src/test/stdtest/stdtest.rc index b2b7522c2e1..c47b85c65f8 100644 --- a/src/test/stdtest/stdtest.rc +++ b/src/test/stdtest/stdtest.rc @@ -31,6 +31,7 @@ mod test; mod uint; mod float; mod math; +mod result; // Local Variables: // mode: rust