diff --git a/src/lib/result.rs b/src/lib/result.rs new file mode 100644 index 000000000000..ba5ead5d4dbe --- /dev/null +++ b/src/lib/result.rs @@ -0,0 +1,86 @@ +/* +Module: result + +A type representing either success or failure +*/ + +/* Section: Types */ + +/* +Tag: t + +The result type +*/ +tag t { + /* + Variant: ok + + Contains the result value + */ + ok(T); + /* + Variant: error + + Contains the error value + */ + error(U); +} + +/* Section: Operations */ + +/* +Function: get + +Get the value out of a successful result + +Failure: + +If the result is an error +*/ +fn get(res: t) -> T { + alt res { + ok(t) { t } + error(_) { + fail "get called on error result"; + } + } +} + +/* +Function: get + +Get the value out of an error result + +Failure: + +If the result is not an error +*/ +fn get_error(res: t) -> U { + alt res { + error(u) { u } + ok(_) { + fail "get_error called on ok result"; + } + } +} + +/* +Function: success + +Returns true if the result is +*/ +fn success(res: t) -> bool { + alt res { + ok(_) { true } + error(_) { false } + } +} + +/* +Function: failure + +Returns true if the result is +*/ +fn failure(res: t) -> bool { + !success(res) +} \ No newline at end of file diff --git a/src/lib/std.rc b/src/lib/std.rc index 7f2539fd4c59..fa9a74fc8087 100644 --- a/src/lib/std.rc +++ b/src/lib/std.rc @@ -97,6 +97,7 @@ mod test; mod unsafe; mod term; mod math; +mod result; #[cfg(unicode)] mod unicode;