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
/// Unwraps in the ```Some``` case; returns in the ```None``` case.
///```
/// unwrap_return!(expression)
///```
/// is shorthand for
///```
/// match expression {
///     Some(ret) => ret,
///     None => return
/// }
/// ```
#[macro_export]
macro_rules! unwrap_return {
    ($e:expr) => {
        match { $e } {
            Some(ret) => ret,
            None => return,
        }
    };
}

/// Unwraps in the ```Some``` case; breaks in the ```None``` case.
///```
/// unwrap_break!(expression)
///```
/// is shorthand for
///```
/// match expression {
///     Some(ret) => ret,
///     None => break
/// }
/// ```
#[macro_export]
macro_rules! unwrap_break {
    ($e:expr) => {
        match { $e } {
            Some(ret) => ret,
            None => break,
        }
    };
}

/// Unwraps in the ```Some``` case; does something in the ```None``` case.
///
/// Usually you want ```unwrap_or_else``` from the standard library. Use this for cases where 
/// you need to do something simple before returning or breaking, or you want to return a value. 
///
///```
/// unwrap_do!(expression, do)
///```
/// is shorthand for
///```
/// match expression {
///     Some(ret) => ret,
///     None => {
///         do
///     }
/// }
/// ```
#[macro_export]
macro_rules! unwrap_do {
    ($e:expr, $d:expr) => {
        match { $e } {
            Some(ret) => ret,
            None => $d
        }
    };
}

/// Unwraps in the ```Ok``` case; returns in the ```Err``` case.
///
/// Drops everything contained within Err.
///
///```
/// ok_unwrap_return!(expression)
///```
/// is shorthand for
///```
/// match expression {
///     Ok(ret) => ret,
///     Err(_) => return
/// }
/// ```
#[macro_export]
macro_rules! ok_unwrap_return {
    ($e:expr) => {
        match { $e } {
            Ok(ret) => ret,
            Err(_) => return,
        }
    };
}

/// Unwraps in the ```Ok``` case; breaks in the ```Result``` case.
///
/// Drops everything contained within Err.
///
///```
/// ok_unwrap_break!(expression)
///```
/// is shorthand for
///```
/// match expression {
///     Ok(ret) => ret,
///     Err(_) => break
/// }
/// ```
#[macro_export]
macro_rules! ok_unwrap_break {
    ($e:expr) => {
        match { $e } {
            Ok(ret) => ret,
            Err(_) => break,
        }
    };
}

/// Unwraps in the ```Ok``` case; does something in the ```Err``` case.
///
/// Drops everything contained within Err.
///
/// Usually you want ```unwrap_or_else``` from the standard library. Use this for cases where 
/// you need to do something simple before returning or breaking, or you want to return a value.
///
///```
/// ok_unwrap_do!(expression, do)
///```
/// is shorthand for
///```
/// match expression {
///     Ok(ret) => ret,
///     Err(_) => {
///         do
///     }
/// }
/// ```
#[macro_export]
macro_rules! ok_unwrap_do {
    ($e:expr, $d:expr) => {
        match { $e } {
            Ok(ret) => ret,
            Err(_) => $d
        }
    };
}