/
result.sw
61 lines (52 loc) 路 1.69 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
//! 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;
use ::revert::revert;
/// `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<T, E> Result<T, E> {
/////////////////////////////////////////////////////////////////////////
// Querying the contained values
/////////////////////////////////////////////////////////////////////////
/// Returns `true` if the result is [`Ok`].
fn is_ok(self) -> bool {
if let Result::Ok(t) = self {
true
} else {
false
}
}
/// Returns `true` if the result is [`Err`].
fn is_err(self) -> bool {
if let Result::Ok(t) = self {
false
} else {
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.
fn unwrap(self) -> T {
if let Result::Ok(inner_value) = self {
inner_value
} else {
revert(0);
}
}
}