-
Notifications
You must be signed in to change notification settings - Fork 12.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This contains a result tag with ok(T) and error(U) variants. I expect to use it for error handling on functions that can recover from errors, like in the io module.
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
Module: result | ||
A type representing either success or failure | ||
*/ | ||
|
||
/* Section: Types */ | ||
|
||
/* | ||
Tag: t | ||
The result type | ||
*/ | ||
tag t<T, U> { | ||
/* | ||
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<T, U>(res: t<T, U>) -> 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<T, U>(res: t<T, U>) -> U { | ||
alt res { | ||
error(u) { u } | ||
ok(_) { | ||
fail "get_error called on ok result"; | ||
} | ||
} | ||
} | ||
|
||
/* | ||
Function: success | ||
Returns true if the result is <ok> | ||
*/ | ||
fn success<T, U>(res: t<T, U>) -> bool { | ||
alt res { | ||
ok(_) { true } | ||
error(_) { false } | ||
} | ||
} | ||
|
||
/* | ||
Function: failure | ||
Returns true if the result is <error> | ||
*/ | ||
fn failure<T, U>(res: t<T, U>) -> bool { | ||
!success(res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,7 @@ mod test; | |
mod unsafe; | ||
mod term; | ||
mod math; | ||
mod result; | ||
|
||
#[cfg(unicode)] | ||
mod unicode; | ||
|