# enum_hack.rs - source
Source of the Rust file `/home/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/paste-impl-0.1.18/src/enum_hack.rs`.
Keywords: rust, rustlang, rust-lang

     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

    use proc_macro::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};
    use std::iter::FromIterator;

    pub fn wrap(output: TokenStream) -> TokenStream {
        let mut hasher = DefaultHasher::default();
        output.to_string().hash(&mut hasher);
        let mangled_name = format!("_paste_{}", hasher.finish());
        let ident = Ident::new(&mangled_name, Span::call_site());

        // #[derive(paste::EnumHack)]
        // enum #ident {
        //     Value = (stringify! {
        //         #output
        //     }, 0).1,
        // }
        TokenStream::from_iter(vec![
            TokenTree::Punct(Punct::new('#', Spacing::Alone)),
            TokenTree::Group(Group::new(
                Delimiter::Bracket,
                TokenStream::from_iter(vec![
                    TokenTree::Ident(Ident::new("derive", Span::call_site())),
                    TokenTree::Group(Group::new(
                        Delimiter::Parenthesis,
                        TokenStream::from_iter(vec![
                            TokenTree::Ident(Ident::new("paste", Span::call_site())),
                            TokenTree::Punct(Punct::new(':', Spacing::Joint)),
                            TokenTree::Punct(Punct::new(':', Spacing::Alone)),
                            TokenTree::Ident(Ident::new("EnumHack", Span::call_site())),
                        ]),
                    )),
                ]),
            )),
            TokenTree::Ident(Ident::new("enum", Span::call_site())),
            TokenTree::Ident(ident),
            TokenTree::Group(Group::new(
                Delimiter::Brace,
                TokenStream::from_iter(vec![
                    TokenTree::Ident(Ident::new("Value", Span::call_site())),
                    TokenTree::Punct(Punct::new('=', Spacing::Alone)),
                    TokenTree::Group(Group::new(
                        Delimiter::Parenthesis,
                        TokenStream::from_iter(vec![
                            TokenTree::Ident(Ident::new("stringify", Span::call_site())),
                            TokenTree::Punct(Punct::new('!', Spacing::Alone)),
                            TokenTree::Group(Group::new(Delimiter::Brace, output)),
                            TokenTree::Punct(Punct::new(',', Spacing::Alone)),
                            TokenTree::Literal(Literal::usize_unsuffixed(0)),
                        ]),
                    )),
                    TokenTree::Punct(Punct::new('.', Spacing::Alone)),
                    TokenTree::Literal(Literal::usize_unsuffixed(1)),
                    TokenTree::Punct(Punct::new(',', Spacing::Alone)),
                ]),
            )),
        ])
    }

    pub fn extract(input: TokenStream) -> TokenStream {
        let mut tokens = input.into_iter();
        let _ = tokens.next().expect("enum");
        let _ = tokens.next().expect("#ident");
        let mut braces = match tokens.next().expect("{...}") {
            TokenTree::Group(group) => group.stream().into_iter(),
            _ => unreachable!("{...}"),
        };
        let _ = braces.next().expect("Value");
        let _ = braces.next().expect("=");
        let mut parens = match braces.next().expect("(...)") {
            TokenTree::Group(group) => group.stream().into_iter(),
            _ => unreachable!("(...)"),
        };
        let _ = parens.next().expect("stringify");
        let _ = parens.next().expect("!");
        let token_stream = match parens.next().expect("{...}") {
            TokenTree::Group(group) => group.stream(),
            _ => unreachable!("{...}"),
        };
        let _ = parens.next().expect(",");
        let _ = parens.next().expect("0");
        let _ = braces.next().expect(".");
        let _ = braces.next().expect("1");
        let _ = braces.next().expect(",");
        token_stream
    }