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 Option type & is_some/is_none methods (FuelLabs#31)
* Feat:Add Option type & is_some/is_none methods * Doc:Improve module-level docs * Fix:Add generic type T to match
- Loading branch information
Showing
2 changed files
with
52 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 |
---|---|---|
|
@@ -8,6 +8,7 @@ dep chain; | |
dep contract_id; | ||
dep context; | ||
dep address; | ||
dep option; | ||
dep block; | ||
dep token; | ||
dep result; | ||
|
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,51 @@ | ||
//! Error handling with the `Option` type. | ||
//! | ||
//! [`Option<T>`][`Option`] is the type used for representing the existence or absence of a value. It is an enum with the variants, [`Some(T)`], representing | ||
//! some value, and [`None()`], representing | ||
//! no value. | ||
library option; | ||
|
||
/// `Option` is a type that represents either the existence of a value ([`Some`]) or a value's absence | ||
/// ([`None`]). | ||
pub enum Option<T> { | ||
/// Contains the value | ||
Some: T, | ||
|
||
/// Signifies the absence of a value | ||
None: (), | ||
} | ||
|
||
///////////////////////////////////////////////////////////////////////////// | ||
// Type implementation | ||
///////////////////////////////////////////////////////////////////////////// | ||
|
||
impl Option<T> { | ||
///////////////////////////////////////////////////////////////////////// | ||
// Querying the contained values | ||
///////////////////////////////////////////////////////////////////////// | ||
|
||
/// Returns `true` if the result is [`Some`]. | ||
fn is_some(self) -> bool { | ||
match self { | ||
Option::Some(T) => { | ||
true | ||
}, | ||
_ => { | ||
false | ||
}, | ||
} | ||
} | ||
|
||
/// Returns `true` if the result is [`None`]. | ||
fn is_none(self) -> bool { | ||
match self { | ||
Option::Some(T) => { | ||
false | ||
}, | ||
_ => { | ||
true | ||
}, | ||
} | ||
} | ||
} |