David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 1 | use std::hash::{Hash, Hasher}; |
| 2 | |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 3 | use proc_macro2::{Delimiter, TokenStream, TokenTree}; |
David Tolnay | cc54371 | 2018-01-08 11:29:54 -0800 | [diff] [blame] | 4 | |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 5 | pub struct TokenTreeHelper<'a>(pub &'a TokenTree); |
| 6 | |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 7 | impl<'a> PartialEq for TokenTreeHelper<'a> { |
| 8 | fn eq(&self, other: &Self) -> bool { |
| 9 | use proc_macro2::Spacing; |
| 10 | |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 11 | match (self.0, other.0) { |
| 12 | (&TokenTree::Group(ref g1), &TokenTree::Group(ref g2)) => { |
| 13 | match (g1.delimiter(), g2.delimiter()) { |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 14 | (Delimiter::Parenthesis, Delimiter::Parenthesis) |
| 15 | | (Delimiter::Brace, Delimiter::Brace) |
| 16 | | (Delimiter::Bracket, Delimiter::Bracket) |
| 17 | | (Delimiter::None, Delimiter::None) => {} |
| 18 | _ => return false, |
| 19 | } |
| 20 | |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 21 | let s1 = g1.stream().clone().into_iter(); |
| 22 | let mut s2 = g2.stream().clone().into_iter(); |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 23 | |
| 24 | for item1 in s1 { |
| 25 | let item2 = match s2.next() { |
| 26 | Some(item) => item, |
| 27 | None => return false, |
| 28 | }; |
| 29 | if TokenTreeHelper(&item1) != TokenTreeHelper(&item2) { |
| 30 | return false; |
| 31 | } |
| 32 | } |
| 33 | s2.next().is_none() |
| 34 | } |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 35 | (&TokenTree::Punct(ref o1), &TokenTree::Punct(ref o2)) => { |
David Tolnay | e614f28 | 2018-10-27 22:50:12 -0700 | [diff] [blame] | 36 | o1.as_char() == o2.as_char() |
| 37 | && match (o1.spacing(), o2.spacing()) { |
| 38 | (Spacing::Alone, Spacing::Alone) | (Spacing::Joint, Spacing::Joint) => true, |
| 39 | _ => false, |
| 40 | } |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 41 | } |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 42 | (&TokenTree::Literal(ref l1), &TokenTree::Literal(ref l2)) => { |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 43 | l1.to_string() == l2.to_string() |
| 44 | } |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 45 | (&TokenTree::Ident(ref s1), &TokenTree::Ident(ref s2)) => s1 == s2, |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 46 | _ => false, |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 51 | impl<'a> Hash for TokenTreeHelper<'a> { |
| 52 | fn hash<H: Hasher>(&self, h: &mut H) { |
| 53 | use proc_macro2::Spacing; |
| 54 | |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 55 | match *self.0 { |
| 56 | TokenTree::Group(ref g) => { |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 57 | 0u8.hash(h); |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 58 | match g.delimiter() { |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 59 | Delimiter::Parenthesis => 0u8.hash(h), |
| 60 | Delimiter::Brace => 1u8.hash(h), |
| 61 | Delimiter::Bracket => 2u8.hash(h), |
| 62 | Delimiter::None => 3u8.hash(h), |
| 63 | } |
| 64 | |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 65 | for item in g.stream().clone() { |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 66 | TokenTreeHelper(&item).hash(h); |
| 67 | } |
| 68 | 0xffu8.hash(h); // terminator w/ a variant we don't normally hash |
| 69 | } |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 70 | TokenTree::Punct(ref op) => { |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 71 | 1u8.hash(h); |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 72 | op.as_char().hash(h); |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 73 | match op.spacing() { |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 74 | Spacing::Alone => 0u8.hash(h), |
| 75 | Spacing::Joint => 1u8.hash(h), |
| 76 | } |
| 77 | } |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 78 | TokenTree::Literal(ref lit) => (2u8, lit.to_string()).hash(h), |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 79 | TokenTree::Ident(ref word) => (3u8, word).hash(h), |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 84 | pub struct TokenStreamHelper<'a>(pub &'a TokenStream); |
| 85 | |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 86 | impl<'a> PartialEq for TokenStreamHelper<'a> { |
| 87 | fn eq(&self, other: &Self) -> bool { |
| 88 | let left = self.0.clone().into_iter().collect::<Vec<_>>(); |
| 89 | let right = other.0.clone().into_iter().collect::<Vec<_>>(); |
| 90 | if left.len() != right.len() { |
| 91 | return false; |
| 92 | } |
| 93 | for (a, b) in left.into_iter().zip(right) { |
| 94 | if TokenTreeHelper(&a) != TokenTreeHelper(&b) { |
| 95 | return false; |
| 96 | } |
| 97 | } |
| 98 | true |
| 99 | } |
| 100 | } |
| 101 | |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 102 | impl<'a> Hash for TokenStreamHelper<'a> { |
| 103 | fn hash<H: Hasher>(&self, state: &mut H) { |
| 104 | let tts = self.0.clone().into_iter().collect::<Vec<_>>(); |
| 105 | tts.len().hash(state); |
| 106 | for tt in tts { |
| 107 | TokenTreeHelper(&tt).hash(state); |
| 108 | } |
| 109 | } |
| 110 | } |