Skip to content

Commit

Permalink
Add more methods to Result. (FuelLabs#16)
Browse files Browse the repository at this point in the history
* 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
adlerjohn authored Jan 27, 2022
1 parent 888f0ab commit 68fcc90
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
1 change: 0 additions & 1 deletion Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ entry = "lib.sw"
license = "Apache-2.0"
name = "lib-std"


[dependencies]
"core" = { git = "http://github.com/FuelLabs/sway-lib-core" }
48 changes: 47 additions & 1 deletion src/result.sw
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
},
}
}
}

0 comments on commit 68fcc90

Please sign in to comment.