]>
Commit | Line | Data |
---|---|---|
b039eaaf SL |
1 | // Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | // zip!(a1,a2,a3,a4) is equivalent to: | |
12 | // a1.zip(a2).zip(a3).zip(a4).map(|(((x1,x2),x3),x4)| (x1,x2,x3,x4)) | |
13 | macro_rules! zip { | |
14 | // Entry point | |
15 | ([$a:expr, $b:expr, $($rest:expr),*]) => { | |
16 | zip!([$($rest),*], $a.zip($b), (x,y), [x,y]) | |
17 | }; | |
18 | ||
19 | // Intermediate steps to build the zipped expression, the match pattern, and | |
20 | // and the output tuple of the closure, using macro hygene to repeatedly | |
21 | // introduce new variables named 'x'. | |
22 | ([$a:expr, $($rest:expr),*], $zip:expr, $pat:pat, [$($flat:expr),*]) => { | |
23 | zip!([$($rest),*], $zip.zip($a), ($pat,x), [$($flat),*, x]) | |
24 | }; | |
25 | ||
26 | // Final step | |
27 | ([], $zip:expr, $pat:pat, [$($flat:expr),+]) => { | |
28 | $zip.map(|$pat| ($($flat),+)) | |
29 | }; | |
30 | ||
31 | // Comma | |
32 | ([$a:expr], $zip:expr, $pat:pat, [$($flat:expr),*]) => { | |
33 | zip!([$a,], $zip, $pat, [$($flat),*]) | |
34 | }; | |
35 | } | |
36 | ||
37 | fn main() { | |
38 | let p1 = vec![1i32, 2].into_iter(); | |
39 | let p2 = vec!["10", "20"].into_iter(); | |
40 | let p3 = vec![100u16, 200].into_iter(); | |
41 | let p4 = vec![1000i64, 2000].into_iter(); | |
42 | ||
43 | let e = zip!([p1,p2,p3,p4]).collect::<Vec<_>>(); | |
44 | assert_eq!(e[0], (1i32,"10",100u16,1000i64)); | |
45 | } |