File: egui_inspect_derive.rs

package info (click to toggle)
rustc-web 1.78.0%2Bdfsg1-2~deb11u3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,245,360 kB
  • sloc: xml: 147,985; javascript: 18,022; sh: 11,083; python: 10,265; ansic: 6,172; cpp: 5,023; asm: 4,390; makefile: 4,269
file content (90 lines) | stat: -rw-r--r-- 2,385 bytes parent folder | download
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
87
88
89
90
//@ force-host
//@ no-prefer-dynamic
#![crate_type = "proc-macro"]

extern crate proc_macro;

use proc_macro::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};

#[proc_macro]
pub fn expand(_: TokenStream) -> TokenStream {
    // Hand expansion/rewriting of
    // ```
    // quote! {
    //     output_mut(|o| o.copied_text = "".into());
    //     output_mut(|o| o.copied_text = format!("{:?}", self.tile_db));
    // }.into()
    // ```
    stream([
        ident("output_mut"),
        group(
            Delimiter::Parenthesis,
            [
                or(),
                ident("o"),
                or(),
                ident("o"),
                dot(),
                ident("copied_text"),
                eq(),
                string(""),
                dot(),
                ident("into"),
                group(Delimiter::Parenthesis, []),
            ],
        ),
        semi(),
        ident("output_mut"),
        group(
            Delimiter::Parenthesis,
            [
                or(),
                ident("o"),
                or(),
                ident("o"),
                dot(),
                ident("copied_text"),
                eq(),
                ident("format"),
                bang(),
                group(
                    Delimiter::Parenthesis,
                    [string("{:?}"), comma(), ident("self"), dot(), ident("tile_db")],
                ),
            ],
        ),
        semi(),
    ])
}

fn stream(s: impl IntoIterator<Item = TokenTree>) -> TokenStream {
    s.into_iter().collect()
}

fn ident(i: &str) -> TokenTree {
    TokenTree::Ident(Ident::new(i, Span::call_site()))
}
fn group(d: Delimiter, s: impl IntoIterator<Item = TokenTree>) -> TokenTree {
    TokenTree::Group(Group::new(d, s.into_iter().collect()))
}
fn semi() -> TokenTree {
    TokenTree::Punct(Punct::new(';', Spacing::Alone))
}
fn or() -> TokenTree {
    TokenTree::Punct(Punct::new('|', Spacing::Alone))
}
fn dot() -> TokenTree {
    TokenTree::Punct(Punct::new('.', Spacing::Alone))
}
fn eq() -> TokenTree {
    TokenTree::Punct(Punct::new('=', Spacing::Alone))
}
fn bang() -> TokenTree {
    TokenTree::Punct(Punct::new('!', Spacing::Alone))
}
fn comma() -> TokenTree {
    TokenTree::Punct(Punct::new(',', Spacing::Alone))
}
fn string(s: &str) -> TokenTree {
    TokenTree::Literal(Literal::string(s))
}