Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/syn-2.0.119/src/tt.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / syn-2.0.119 / src / tt.rs` 97 lines ยท 3315 bytes Raw Blame History 1 use alloc::string::ToString; 2 use core::hash::{Hash, Hasher}; 3 use proc_macro2::{Delimiter, Spacing, TokenStream, TokenTree}; 4 5 pub(crate) struct TokenTreeHelper<'a>(pub &'a TokenTree); 6 7 impl<'a> PartialEq for TokenTreeHelper<'a> { 8 fn eq(&self, other: &Self) -> bool { 9 match (self.0, other.0) { 10 (TokenTree::Group(g1), TokenTree::Group(g2)) => { 11 match (g1.delimiter(), g2.delimiter()) { 12 (Delimiter::Parenthesis, Delimiter::Parenthesis) 13 | (Delimiter::Brace, Delimiter::Brace) 14 | (Delimiter::Bracket, Delimiter::Bracket) 15 | (Delimiter::None, Delimiter::None) => {} 16 _ => return false, 17 } 18 19 TokenStreamHelper(&g1.stream()) == TokenStreamHelper(&g2.stream()) 20 } 21 (TokenTree::Punct(o1), TokenTree::Punct(o2)) => { 22 o1.as_char() == o2.as_char() 23 && match (o1.spacing(), o2.spacing()) { 24 (Spacing::Alone, Spacing::Alone) | (Spacing::Joint, Spacing::Joint) => true, 25 _ => false, 26 } 27 } 28 (TokenTree::Literal(l1), TokenTree::Literal(l2)) => l1.to_string() == l2.to_string(), 29 (TokenTree::Ident(s1), TokenTree::Ident(s2)) => s1 == s2, 30 _ => false, 31 } 32 } 33 } 34 35 impl<'a> Hash for TokenTreeHelper<'a> { 36 fn hash(&self, h: &mut H) { 37 match self.0 { 38 TokenTree::Group(g) => { 39 0u8.hash(h); 40 match g.delimiter() { 41 Delimiter::Parenthesis => 0u8.hash(h), 42 Delimiter::Brace => 1u8.hash(h), 43 Delimiter::Bracket => 2u8.hash(h), 44 Delimiter::None => 3u8.hash(h), 45 } 46 47 for item in g.stream() { 48 TokenTreeHelper(&item).hash(h); 49 } 50 0xFFu8.hash(h); // terminator w/ a variant we don't normally hash 51 } 52 TokenTree::Punct(op) => { 53 1u8.hash(h); 54 op.as_char().hash(h); 55 match op.spacing() { 56 Spacing::Alone => 0u8.hash(h), 57 Spacing::Joint => 1u8.hash(h), 58 } 59 } 60 TokenTree::Literal(lit) => (2u8, lit.to_string()).hash(h), 61 TokenTree::Ident(word) => (3u8, word).hash(h), 62 } 63 } 64 } 65 66 pub(crate) struct TokenStreamHelper<'a>(pub &'a TokenStream); 67 68 impl<'a> PartialEq for TokenStreamHelper<'a> { 69 fn eq(&self, other: &Self) -> bool { 70 let left = self.0.clone().into_iter(); 71 let mut right = other.0.clone().into_iter(); 72 73 for item1 in left { 74 let item2 = match right.next() { 75 Some(item) => item, 76 None => return false, 77 }; 78 if TokenTreeHelper(&item1) != TokenTreeHelper(&item2) { 79 return false; 80 } 81 } 82 83 right.next().is_none() 84 } 85 } 86 87 impl<'a> Hash for TokenStreamHelper<'a> { 88 fn hash(&self, state: &mut H) { 89 let tokens = self.0.clone().into_iter(); 90 91 tokens.clone().count().hash(state); 92 93 for tt in tokens { 94 TokenTreeHelper(&tt).hash(state); 95 } 96 } 97 }