forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.sw
180 lines (172 loc) · 4.51 KB
/
result.sw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! 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.
//!
//! Functions return `Result` whenever errors are expected and recoverable. In
//! the `std` crate, `Result` is most prominently used for `Identity`
//! interactions and cryptographic operations.
//!
//! A simple function returning `Result` might be defined and used like so:
//!
//! ```
//! enum Version {
//! Version1,
//! Version2,
//! }
//!
//! enum VersionError {
//! InvalidNumber,
//! }
//!
//! fn parse_version(version_number: u8) -> Result<Version, VersionError> {
//! match version_number {
//! 1 => Ok(Version::Version1),
//! 2 => Ok(Version::Version2),
//! _ => Err(VersionError::InvalidNumber),
//! }
//! }
//! ```
//!
//! ### Method overview
//!
//! In addition to working with pattern matching, `Result` provides a variety
//! of methods.
//!
//! ### Querying the variant
//!
//! The `is_ok` and `is_err` methods return `true` if the `Result` is
//! `Ok` or `Err`, respectively.
//!
//! `is_ok` : `Result::is_ok`
//! `is_err`: `Result::is_err`
//!
//! ### Extracting the contained value
//!
//! These methods exctract the contained value in a `Result<T,E>` when it is
//! the `Ok` variant. If the `Result` is `Err`:
//!
//! * `unwrap` reverts.
//! * `unwrap_or` returns the default provided value.
//!
//! `unwrap` : `Result::unwrap`
//! `unwrap_or`: `Result::unwrap_or`
library;
use ::revert::revert;
// ANCHOR: docs_result
/// `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,
}
// ANCHOR_END: docs_result
// Type implementation
//
impl<T, E> Result<T, E> {
// Querying the contained values
//
/// Returns `true` if the result is `Ok`.
///
/// ### Examples
///
/// ```
/// enum Error {
/// NotFound,
/// Invalid,
/// }
///
/// let x: Result<u64, Error> = Result::Ok(42);
/// assert(x.is_ok());
///
/// let y: Result<u64, Error> = Result::Err(Error::NotFound));
/// assert(!x.is_ok());
/// ```
pub fn is_ok(self) -> bool {
match self {
Result::Ok(_) => true,
_ => false,
}
}
/// Returns `true` if the result is `Err`.
///
/// ### Examples
///
/// ```
/// enum Error {
/// NotFound,
/// Invalid,
/// }
///
/// let x: Result<u64, Error> = Result::Ok(42);
/// assert(!x.is_err());
///
/// let y: Result<u64, Error> = Result::Err(Error::NotFound));
/// assert(x.is_err());
/// ```
pub fn is_err(self) -> bool {
match self {
Result::Ok(_) => false,
_ => true,
}
}
/// Returns the contained `Ok` value, consuming the `self` value.
///
/// Because this function may revert, its use is generally discouraged.
/// Instead, prefer to use pattern matching and handle the `Err`
/// case explicitly.
///
/// ### Reverts
///
/// Reverts if the self value is `Err`.
///
/// ### Examples
///
/// ```
/// enum Error {
/// NotFound,
/// Invalid,
/// }
///
/// let x: Result<u64, Error> = Result::Ok(42);
/// assert(x.unwrap() == 42);
///
/// let y: Result<u64, Error> = Result::Err(Error::NotFound));
/// assert(x.unwrap() == 42); // reverts
/// ```
pub fn unwrap(self) -> T {
match self {
Result::Ok(inner_value) => inner_value,
_ => revert(0),
}
}
/// Returns the contained `Ok` value or a provided default.
///
/// ### Examples
///
/// ```
/// enum Error {
/// NotFound,
/// Invalid,
/// }
///
/// let x: Result<u64, Error> = Result::Ok(42);
/// assert(x.unwrap_or(69) == 42);
///
/// let y: Result<u64, Error> = Result::Err(Error::NotFound));
/// assert(x.unwrap_or(69) == 69);
/// ```
pub fn unwrap_or(self, default: T) -> T {
match self {
Result::Ok(inner_value) => inner_value,
Result::Err(_) => default,
}
}
// TODO: Implement the following transforms when Option and Result can
// import one another:
// - `ok(self) -> Option<T>`
// - `err(self) -> Option<E>`
}