-
Notifications
You must be signed in to change notification settings - Fork 14
/
syn_utils.rs
177 lines (166 loc) · 4.82 KB
/
syn_utils.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
use proc_macro2::TokenStream;
use quote::{quote, quote_spanned, ToTokens};
use std::collections::HashSet;
use syn::{
ext::IdentExt,
parse::discouraged::Speculative,
parse::Parse,
parse::ParseStream,
parse2, parse_str,
punctuated::Punctuated,
visit::{visit_path, Visit},
DeriveInput, GenericParam, Generics, Ident, LitStr, Path, Result, Token, Type, WherePredicate,
};
macro_rules! bail {
($span:expr, $message:literal $(,)?) => {
return std::result::Result::Err(syn::Error::new($span, $message))
};
($span:expr, $err:expr $(,)?) => {
return std::result::Result::Err(syn::Error::new($span, $err))
};
($span:expr, $fmt:expr, $($arg:tt)*) => {
return std::result::Result::Err(syn::Error::new($span, std::format!($fmt, $($arg)*)))
};
}
pub fn into_macro_output(input: Result<TokenStream>) -> proc_macro::TokenStream {
match input {
Ok(s) => s,
Err(e) => e.to_compile_error(),
}
.into()
}
pub struct GenericParamSet {
idents: HashSet<Ident>,
}
impl GenericParamSet {
pub fn new(generics: &Generics) -> Self {
let mut idents = HashSet::new();
for p in &generics.params {
match p {
GenericParam::Type(t) => {
idents.insert(t.ident.unraw());
}
GenericParam::Const(t) => {
idents.insert(t.ident.unraw());
}
_ => {}
}
}
Self { idents }
}
fn contains(&self, ident: &Ident) -> bool {
self.idents.contains(&ident.unraw())
}
pub fn contains_in_type(&self, ty: &Type) -> bool {
struct Visitor<'a> {
generics: &'a GenericParamSet,
result: bool,
}
impl<'a, 'ast> Visit<'ast> for Visitor<'a> {
fn visit_path(&mut self, i: &'ast syn::Path) {
if i.leading_colon.is_none() {
if let Some(s) = i.segments.iter().next() {
if self.generics.contains(&s.ident) {
self.result = true;
}
}
}
visit_path(self, i);
}
}
let mut visitor = Visitor {
generics: self,
result: false,
};
visitor.visit_type(ty);
visitor.result
}
}
pub enum Quotable<T> {
Direct(T),
Quoted { s: LitStr, args: ArgsOf<T> },
}
impl<T: Parse> Parse for Quotable<T> {
fn parse(input: ParseStream) -> Result<Self> {
let fork = input.fork();
if let Ok(s) = fork.parse::<LitStr>() {
input.advance_to(&fork);
let token: TokenStream = parse_str(&s.value())?;
let tokens = quote_spanned!(s.span()=> #token);
let args = parse2(tokens)?;
Ok(Quotable::Quoted { s, args })
} else {
Ok(Quotable::Direct(input.parse()?))
}
}
}
impl<T: ToTokens> ToTokens for Quotable<T> {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Self::Direct(value) => value.to_tokens(tokens),
Self::Quoted { s, .. } => s.to_tokens(tokens),
}
}
}
impl<T> Quotable<T> {
pub fn into_iter(self) -> impl IntoIterator<Item = T> {
match self {
Self::Direct(item) => vec![item],
Self::Quoted { args, .. } => args.into_iter().collect(),
}
.into_iter()
}
}
pub struct ArgsOf<T>(Punctuated<T, Token![,]>);
impl<T: Parse> Parse for ArgsOf<T> {
fn parse(input: ParseStream) -> Result<Self> {
Ok(Self(Punctuated::parse_terminated(input)?))
}
}
impl<T: ToTokens> ToTokens for ArgsOf<T> {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.0.to_tokens(tokens)
}
}
impl<T> ArgsOf<T> {
pub fn into_iter(self) -> impl Iterator<Item = T> {
self.0.into_iter()
}
}
pub fn impl_trait(
input: &DeriveInput,
trait_path: &Path,
wheres: &[WherePredicate],
contents: TokenStream,
) -> TokenStream {
let ty = &input.ident;
let (impl_g, ty_g, where_clause) = input.generics.split_for_impl();
let mut wheres = wheres.to_vec();
if let Some(where_clause) = where_clause {
wheres.extend(where_clause.predicates.iter().cloned());
}
let where_clause = if wheres.is_empty() {
quote! {}
} else {
quote! { where #(#wheres,)*}
};
quote! {
#[automatically_derived]
impl #impl_g #trait_path for #ty #ty_g #where_clause {
#contents
}
}
}
pub fn impl_trait_result(
input: &DeriveInput,
trait_path: &Path,
wheres: &[WherePredicate],
contents: TokenStream,
dump: bool,
) -> Result<TokenStream> {
let ts = impl_trait(input, trait_path, wheres, contents);
if dump {
panic!("macro output:\n{ts}");
}
Ok(ts)
}