type_sig_proc_macro/
lib.rs

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
extern crate proc_macro;
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::{quote, ToTokens};
use syn::{
  self, 
  Expr, 
  ExprLit,
  Lit, 
  LitInt,
  Type, 
  TypeArray, 
  TypeBareFn,
  TypeGroup,
  TypeParen,
  TypePtr, 
  TypeReference,
  TypeSlice, 
  TypeTuple,
};

// type_sig format:
//
// NAME
// FLAGS...
// TYPE_ID
// ADDITIONAL_TYPE_IDS...

#[proc_macro]
pub fn type_sig(input: TokenStream) -> TokenStream {
  let input_ = input.clone();
  let input_: Type = syn::parse_macro_input!(input_);
  let ty_name = quote! { unsafe { std::any::type_name::<#input_>() } };
  let ty_id = quote! { unsafe { std::any::TypeId::of::<#input_>() } };
  let (
    mut is_const,
    mut is_mut,
    mut is_unsafe,
    mut is_impl_trait,
    mut is_dyn_trait,
    mut is_infer,
    mut is_macro,
    mut is_never,
    mut is_paren,
    mut is_path,
    mut is_array,
    mut is_slice,
    mut is_tuple,
    mut is_closure,
    mut is_ref,
    mut is_ptr,
    mut is_verbatim,
    mut ty_len,
  ) = (
    false, false, false, 
    false, false, false, 
    false, false, false, 
    false, false, false,
    false, false, false,
    false, false,
    None as Option<Expr>,
  );
  let mut ty_lts: Vec<String> = Vec::new();
  let mut ty_children: Vec<Expr> = Vec::new();
  match input_ {
    Type::Array(TypeArray {
      elem: ref ty,
      len,
      ..
    }) => {
      is_array = true;
      let ty_child = type_sig(ty.into_token_stream().into());
      let ty_child: Expr = syn::parse_macro_input!(ty_child);
      ty_children.push(ty_child);
      ty_len = Some(len);
    }
    Type::BareFn(TypeBareFn {
      unsafety: is_unsafe_,
      inputs: ref ty_args,
      output: ref ty_ret,
      ..
    }) => {
      is_closure = true;
      is_unsafe = is_unsafe_.is_some();
      for ty_arg in ty_args {
        let ty_child = type_sig(ty_arg.ty.clone().into_token_stream().into());
        let ty_child: Expr = syn::parse_macro_input!(ty_child);
        ty_children.push(ty_child);
      }
      let ty_child = type_sig(ty_ret.into_token_stream().into());
      let ty_child: Expr = syn::parse_macro_input!(ty_child);
      ty_children.push(ty_child);
    }
    Type::Group(TypeGroup {
      elem: ref ty,
      ..
    }) => {
      let ty_child = type_sig(ty.into_token_stream().into());
      let ty_child: Expr = syn::parse_macro_input!(ty_child);
      ty_children.push(ty_child);
    }
    Type::ImplTrait(_) => {
      is_impl_trait = true;
    }
    Type::Infer(_) => {
      is_infer = true;
    }
    Type::Macro(_) => {
      is_macro = true;
    }
    Type::Never(_) => {
      is_never = true;
    }
    Type::Paren(TypeParen {
      elem: ref ty,
      ..
    }) => {
      is_paren = true;
      let ty_child = type_sig(ty.into_token_stream().into());
      let ty_child: Expr = syn::parse_macro_input!(ty_child);
      ty_children.push(ty_child);
    }
    Type::Path(_) => {
      is_path = true;
    }
    Type::Ptr(TypePtr {
      const_token: is_const_,
      mutability: is_mut_,
      elem: ref ty,
      ..
    }) => {
      is_ptr = true;
      is_const = is_const_.is_some();
      is_mut = is_mut_.is_some();
      let ty_child = type_sig(ty.into_token_stream().into());
      let ty_child: Expr = syn::parse_macro_input!(ty_child);
      ty_children.push(ty_child);
    }
    Type::Reference(TypeReference {
      lifetime: ref ty_lt,
      mutability: is_mut_,
      elem: ref ty,
      ..
    }) => {
      is_ref = true;
      is_const = is_mut_.is_none();
      is_mut = is_mut_.is_some();
      if let Some(ty_lt) = ty_lt {
        ty_lts.push(ty_lt.ident.to_string());
      }
      let ty_child = type_sig(ty.into_token_stream().into());
      let ty_child: Expr = syn::parse_macro_input!(ty_child);
      ty_children.push(ty_child);
    }
    Type::Slice(TypeSlice {
      elem: ref ty,
      ..
    }) => {
      is_slice = true;
      let ty_child = type_sig(ty.into_token_stream().into());
      let ty_child: Expr = syn::parse_macro_input!(ty_child);
      ty_children.push(ty_child);
    }
    Type::TraitObject(_) => {
      is_dyn_trait = true;
    }
    Type::Tuple(TypeTuple {
      elems: ref ty_args,
      ..
    }) => {
      is_tuple = true;
      for ty_arg in ty_args {
        let ty_child = type_sig(ty_arg.into_token_stream().into());
        let ty_child: Expr = syn::parse_macro_input!(ty_child);
        ty_children.push(ty_child);
      }
      ty_len = Some(Expr::Lit(ExprLit {
        attrs: Vec::new(),
        lit: Lit::Int(LitInt::new(&ty_args.len().to_string(), Span::call_site())),
      }));
    }
    Type::Verbatim(_) => {
      is_verbatim = true;
    }
    _ => {}
  }

  let ty_len = match ty_len {
    Some(_) => quote! { Option::<usize>::Some(#ty_len) },
    None => quote! { Option::<usize>::None },
  };

  quote! {
    TypeSignature {
      name: (#ty_name).to_string(),
      id: #ty_id,
      is_const: #is_const,
      is_mut: #is_mut,
      is_unsafe: #is_unsafe,
      is_impl_trait: #is_impl_trait,
      is_dyn_trait: #is_dyn_trait,
      is_infer: #is_infer,
      is_macro: #is_macro,
      is_never: #is_never,
      is_paren: #is_paren,
      is_path: #is_path,
      is_tuple: #is_tuple,
      is_array: #is_array,
      is_slice: #is_slice,
      is_closure: #is_closure,
      is_ref: #is_ref,
      is_ptr: #is_ptr,
      is_verbatim: #is_verbatim,
      len: #ty_len,
      lifetimes: vec![#(#ty_lts),*],
      children: vec![#(#ty_children),*],
    }
  }.into()
}