1 | use syn::punctuated::{Pair, Punctuated}; |
2 | use syn::Token; |
3 | |
4 | #[macro_use ] |
5 | mod macros; |
6 | |
7 | macro_rules! check_exact_size_iterator { |
8 | ($iter:expr) => {{ |
9 | let iter = $iter; |
10 | let size_hint = iter.size_hint(); |
11 | let len = iter.len(); |
12 | let count = iter.count(); |
13 | assert_eq!(len, count); |
14 | assert_eq!(size_hint, (count, Some(count))); |
15 | }}; |
16 | } |
17 | |
18 | #[test] |
19 | fn pairs() { |
20 | let mut p: Punctuated<_, Token![,]> = punctuated!(2, 3, 4); |
21 | |
22 | check_exact_size_iterator!(p.pairs()); |
23 | check_exact_size_iterator!(p.pairs_mut()); |
24 | check_exact_size_iterator!(p.into_pairs()); |
25 | |
26 | let mut p: Punctuated<_, Token![,]> = punctuated!(2, 3, 4); |
27 | |
28 | assert_eq!(p.pairs().next_back().map(Pair::into_value), Some(&4)); |
29 | assert_eq!( |
30 | p.pairs_mut().next_back().map(Pair::into_value), |
31 | Some(&mut 4) |
32 | ); |
33 | assert_eq!(p.into_pairs().next_back().map(Pair::into_value), Some(4)); |
34 | } |
35 | |
36 | #[test] |
37 | fn iter() { |
38 | let mut p: Punctuated<_, Token![,]> = punctuated!(2, 3, 4); |
39 | |
40 | check_exact_size_iterator!(p.iter()); |
41 | check_exact_size_iterator!(p.iter_mut()); |
42 | check_exact_size_iterator!(p.into_iter()); |
43 | |
44 | let mut p: Punctuated<_, Token![,]> = punctuated!(2, 3, 4); |
45 | |
46 | assert_eq!(p.iter().next_back(), Some(&4)); |
47 | assert_eq!(p.iter_mut().next_back(), Some(&mut 4)); |
48 | assert_eq!(p.into_iter().next_back(), Some(4)); |
49 | } |
50 | |
51 | #[test] |
52 | fn may_dangle() { |
53 | let p: Punctuated<_, Token![,]> = punctuated!(2, 3, 4); |
54 | for element in &p { |
55 | if *element == 2 { |
56 | drop(p); |
57 | break; |
58 | } |
59 | } |
60 | |
61 | let mut p: Punctuated<_, Token![,]> = punctuated!(2, 3, 4); |
62 | for element in &mut p { |
63 | if *element == 2 { |
64 | drop(p); |
65 | break; |
66 | } |
67 | } |
68 | } |
69 | |