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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
//! This crate provides 2 macros, `extract!` and `let_extract!`. See their //! individual macro-level documentation for more information. /// Extract the fields of a single variant from an enum, returning an /// `Option<T>` where `T` is either the single field, or a tuple of each of the /// fields in the order they are written. /// /// ## Examples /// /// Given the following enum: /// /// ```ignore /// enum Foo { /// A(i32), /// B(i32, i32), /// C { x: i32, y: i32 }, /// D { z: i32 }, /// } /// ``` /// /// If the variant matches, it produces a `Some` of the fields in the matched /// variant. /// /// ```rust /// # #[macro_use] extern crate enum_extract; /// # enum Foo { /// # A(i32), /// # B(i32, i32), /// # C { x: i32, y: i32 }, /// # D { z: i32 }, /// # } /// # fn main() { /// let a = Foo::A(10); /// assert_eq!(extract!(Foo::A(_), a), Some(10)); /// /// let d = Foo::D{ z: 20 }; /// assert_eq!(extract!(Foo::D{z}, d), Some(20)); /// # } /// ``` /// /// If there is more than one field in the enum variant, it produces a `Some` of a tuple /// /// ```rust /// # #[macro_use] extern crate enum_extract; /// # enum Foo { /// # A(i32), /// # B(i32, i32), /// # C { x: i32, y: i32 }, /// # D { z: i32 }, /// # } /// # fn main() { /// let b = Foo::B(10, 20); /// assert_eq!(extract!(Foo::B(_, _), b), Some((10, 20))); /// /// let c = Foo::C{ x: 30, y: 40 }; /// assert_eq!(extract!(Foo::C{x, y}, c), Some((30, 40))); /// /// // You can also control the order of the fields in the struct variant case! /// assert_eq!(extract!(Foo::C{y, x}, c), Some((40, 30))); /// # } /// ``` /// /// If the pattern doesn't match, it produces a `None` /// /// ```rust /// # #[macro_use] extern crate enum_extract; /// # enum Foo { /// # A(i32), /// # B(i32, i32), /// # C { x: i32, y: i32 }, /// # D { z: i32 }, /// # } /// # fn main() { /// let b = Foo::B(10, 20); /// assert_eq!(extract!(Foo::A(_), b), None); /// # } /// ``` #[macro_export] macro_rules! extract { // Internal Variants (@ANON_TUPLE [], [$($ids:ident)*], $($p:ident)::+, $t:expr) => { match $t { $($p)::+ ( $($ids),* ) => Some(( $($ids),* )), _ => None, } }; (@ANON_TUPLE [_], [$($ids:ident)*], $($p:ident)::+, $t:expr) => { extract!(@ANON_TUPLE [], [$($ids)* x], $($p)::+, $t) }; (@ANON_TUPLE [_, $($more:tt)*], [$($ids:ident)*], $($p:ident)::+, $t:expr) => { extract!(@ANON_TUPLE [$($more)*], [$($ids)* x], $($p)::+, $t) }; // Struct Variants ($($p:ident)::+ { $($i:ident),* } , $t:expr) => { match $t { $($p)::+ {$($i),*} => Some(($($i),*)), _ => None } }; // Tuple Variants ($($p:ident)::+ ( $($its:tt)* ) , $t:expr) => { extract!(@ANON_TUPLE [$($its)*], [], $($p)::+, $t) }; } /// Extract the fields of a single variant from an enum, binding them into the /// current scope. /// /// If the binding fails due to the variant being incorrect, execute the error /// expression. Alternatively, a partial `match` block may be written to handle /// the remaining cases. /// /// ## Examples /// /// Given the following enum: /// /// ```ignore /// enum Foo { /// A(i32), /// B(i32, i32), /// C { x: i32, y: i32 }, /// D { z: i32 }, /// } /// ``` /// /// If the extract succeeds, the variable names written bind into the current /// scope. /// /// ```rust /// # #[macro_use] extern crate enum_extract; /// # enum Foo { /// # A(i32), /// # B(i32, i32), /// # C { x: i32, y: i32 }, /// # D { z: i32 }, /// # } /// # fn main() { /// let a = Foo::A(10); /// let_extract!(Foo::A(x), a, panic!()); /// assert_eq!(x, 10); /// # } /// ``` /// /// If it fails, the expression passed in as the 3rd argument is executed /// instead, and can cause execution to diverge. /// /// ```rust /// # #[macro_use] extern crate enum_extract; /// # enum Foo { /// # A(i32), /// # B(i32, i32), /// # C { x: i32, y: i32 }, /// # D { z: i32 }, /// # } /// # fn main() { /// let a = Foo::A(10); /// let_extract!(Foo::B(x, y), a, return); /// unreachable!(); /// # } /// ``` /// /// Alternatively, it can provide default values for the variable bindings, from /// left to right. /// /// ```rust /// # #[macro_use] extern crate enum_extract; /// # enum Foo { /// # A(i32), /// # B(i32, i32), /// # C { x: i32, y: i32 }, /// # D { z: i32 }, /// # } /// # fn main() { /// let a = Foo::A(10); /// let_extract!(Foo::B(x, y), a, (40, 50)); /// assert_eq!(x, 40); /// assert_eq!(y, 50); /// # } /// ``` /// /// The other cases can each be handled specifically with a match-like block. /// /// ```rust /// # #[macro_use] extern crate enum_extract; /// # enum Foo { /// # A(i32), /// # B(i32, i32), /// # C { x: i32, y: i32 }, /// # D { z: i32 }, /// # } /// # fn main() { /// let a = Foo::A(10); /// let_extract!(Foo::B(x, y), a, match { /// Foo::A(x) => return, /// Foo::C{..} => unreachable!(), /// Foo::D{..} => unreachable!(), /// }); /// unreachable!(); /// # } /// ``` /// /// Struct variants are also supported, and can be written either as `{ field }` /// or `{ field: binding }`. /// /// ```rust /// # #[macro_use] extern crate enum_extract; /// # enum Foo { /// # A(i32), /// # B(i32, i32), /// # C { x: i32, y: i32 }, /// # D { z: i32 }, /// # } /// # fn main() { /// let d = Foo::D { z: 10 }; /// let_extract!(Foo::D{ z }, d, unreachable!()); /// assert_eq!(z, 10); /// /// let_extract!(Foo::D{ z: apples }, d, unreachable!()); /// assert_eq!(apples, 10); /// # } /// ``` #[macro_export] macro_rules! let_extract { ($($p:ident)::+ ( $($i:ident),* ) , $t:expr, match { $($body:tt)* }) => { let ($($i,)*) = match $t { $($p)::+ ($($i),*) => ($($i,)*), $($body)* }; }; ($($p:ident)::+ { $($i:ident),* } , $t:expr, match { $($body:tt)* }) => { let ($($i,)*) = match $t { $($p)::+ {$($i),*} => ($($i,)*), $($body)* }; }; ($($p:ident)::+ { $($k:ident : $v:ident),* } , $t:expr, match { $($body:tt)* }) => { let ($($v,)*) = match $t { $($p)::+ {$($k),*} => ($($k,)*), $($body)* }; }; ($($p:ident)::+ ( $($its:tt)* ) , $t:expr, $els:expr) => { let_extract!($($p)::+ ( $($its)* ) , $t, match { _ => $els }) }; ($($p:ident)::+ { $($its:tt)* } , $t:expr, $els:expr) => { let_extract!($($p)::+ { $($its)* } , $t, match { _ => $els }) }; } #[cfg(test)] mod test { enum Foo { A(u32, u32), B(u32) } enum Bar { C { x: u32, y: u32 }, D { z: u32 }, } #[test] fn test() { let f: Foo = Foo::A(10, 20); let x = extract!(Foo::A(_, _), f); assert_eq!(x, Some((10, 20))); let_extract!(Foo::A(x, y), f, panic!()); assert_eq!(x, 10); assert_eq!(y, 20); let f: Foo = Foo::B(8); let x = extract!(Foo::B(_), f); assert_eq!(x, Some(8)); let_extract!(Foo::B(z), f, panic!()); assert_eq!(z, 8); let f: Bar = Bar::C{ x: 10, y: 20 }; let x = extract!(Bar::C{x, y}, f); assert_eq!(x, Some((10, 20))); let_extract!(Bar::C{x, y}, f, panic!()); assert_eq!(x, 10); assert_eq!(y, 20); let_extract!(Bar::C{x: a, y: b}, f, panic!()); assert_eq!(a, 10); assert_eq!(b, 20); let f: Bar = Bar::D{ z: 8 }; let x = extract!(Bar::D{z}, f); assert_eq!(x, Some(8)); let_extract!(Bar::D{z}, f, panic!()); assert_eq!(z, 8); let_extract!(Bar::D{z: x}, f, panic!()); assert_eq!(x, 8); // let f: Bar = Bar::C{ x: 10, y: 20 }; let_extract!(Bar::D{z}, f, match { Bar::C{x, y} => panic!("Saw {:?} {:?}", x, y) }); assert_eq!(z, 8); } #[test] fn alternate_result() { let f: Foo = Foo::A(10, 20); let_extract!(Foo::B(x), f, match { Foo::A(x, _) => (x,) }); assert_eq!(x, 10); let_extract!(Some(y), None, (10,)); assert_eq!(y, 10); } }