forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more methods to Result. (FuelLabs#16)
* Add more methods to Result. * Don't use catch all. * Use catch up, but fix typo. * Fix return type. * Compile workarounds. * fmt * Remove todo comments. * fmt * Make Result public.
- Loading branch information
Showing
2 changed files
with
47 additions
and
2 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
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 |
---|---|---|
@@ -1,6 +1,52 @@ | ||
//! Error handling with the `Result` type. | ||
//! | ||
//! [`Result<T, E>`][`Result`] is the type used for returning and propagating | ||
//! errors. It is an enum with the variants, [`Ok(T)`], representing | ||
//! success and containing a value, and [`Err(E)`], representing error | ||
//! and containing an error value. | ||
library result; | ||
|
||
enum Result<T, E> { | ||
/// `Result` is a type that represents either success ([`Ok`]) or failure | ||
/// ([`Err`]). | ||
pub enum Result<T, E> { | ||
/// Contains the success value | ||
Ok: T, | ||
|
||
/// Contains the error value | ||
Err: E, | ||
} | ||
|
||
///////////////////////////////////////////////////////////////////////////// | ||
// Type implementation | ||
///////////////////////////////////////////////////////////////////////////// | ||
|
||
impl Result<T, E> { | ||
///////////////////////////////////////////////////////////////////////// | ||
// Querying the contained values | ||
///////////////////////////////////////////////////////////////////////// | ||
|
||
/// Returns `true` if the result is [`Ok`]. | ||
fn is_ok(self) -> bool { | ||
match self { | ||
Result::Ok(T) => { | ||
true | ||
}, | ||
_ => { | ||
false | ||
}, | ||
} | ||
} | ||
|
||
/// Returns `true` if the result is [`Err`]. | ||
fn is_err(self) -> bool { | ||
match self { | ||
Result::Ok(T) => { | ||
false | ||
}, | ||
_ => { | ||
true | ||
}, | ||
} | ||
} | ||
} |