forked from parcel-bundler/lightningcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinto_owned.rs
More file actions
183 lines (171 loc) · 5.4 KB
/
into_owned.rs
File metadata and controls
183 lines (171 loc) · 5.4 KB
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
use proc_macro::{self, TokenStream};
use proc_macro2::Span;
use quote::quote;
use syn::{
parse_macro_input, Data, DataEnum, DeriveInput, Field, Fields, GenericArgument, Ident, Member, PathArguments,
Type,
};
pub(crate) fn derive_into_owned(input: TokenStream) -> TokenStream {
let DeriveInput {
ident: self_name,
data,
generics,
..
} = parse_macro_input!(input);
let res = match data {
Data::Struct(s) => {
let fields = s
.fields
.iter()
.enumerate()
.map(|(index, Field { ty, ident, .. })| {
let name = ident
.as_ref()
.map_or_else(|| Member::Unnamed(index.into()), |ident| Member::Named(ident.clone()));
let value = into_owned(ty, quote! { self.#name });
if let Some(ident) = ident {
quote! { #ident: #value }
} else {
value
}
})
.collect::<Vec<proc_macro2::TokenStream>>();
match s.fields {
Fields::Unnamed(_) => {
quote! {
#self_name(#(#fields),*)
}
}
Fields::Named(_) => {
quote! {
#self_name { #(#fields),* }
}
}
Fields::Unit => quote! {},
}
}
Data::Enum(DataEnum { variants, .. }) => {
let variants = variants
.iter()
.map(|variant| {
if !variant.fields.iter().any(|f| has_lifetime(&f.ty)) {
return quote! {};
}
let name = &variant.ident;
let mut field_names = Vec::new();
let mut static_fields = Vec::new();
for (index, Field { ty, ident, .. }) in variant.fields.iter().enumerate() {
let name = ident.as_ref().map_or_else(
|| Ident::new(&format!("_{}", index), Span::call_site()),
|ident| ident.clone(),
);
field_names.push(name.clone());
let value = into_owned(ty, quote! { #name });
static_fields.push(if let Some(ident) = ident {
quote! { #ident: #value }
} else {
value
})
}
match variant.fields {
Fields::Unnamed(_) => {
quote! {
#self_name::#name(#(#field_names),*) => {
#self_name::#name(#(#static_fields),*)
}
}
}
Fields::Named(_) => {
quote! {
#self_name::#name { #(#field_names),* } => {
#self_name::#name { #(#static_fields),* }
}
}
}
Fields::Unit => quote! {},
}
})
.collect::<proc_macro2::TokenStream>();
quote! {
match self {
#variants
_ => unsafe { std::mem::transmute(self) }
}
}
}
_ => {
panic!("can only derive IntoOwned for enums and structs")
}
};
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
let into_owned = if generics.lifetimes().next().is_none() {
panic!("can't derive IntoOwned on a type without any lifetimes")
} else {
quote! {
impl #impl_generics #self_name #ty_generics #where_clause {
/// Consumes the value and returns an owned clone.
pub fn into_owned<'x>(self) -> #self_name<'x> {
#res
}
}
}
};
into_owned.into()
}
fn into_owned(ty: &Type, name: proc_macro2::TokenStream) -> proc_macro2::TokenStream {
if has_lifetime(ty) {
match ty {
Type::Path(path) => {
let last = path.path.segments.last().unwrap();
if last.ident == "Option" {
let v = quote! { v };
let into_owned = match &last.arguments {
PathArguments::AngleBracketed(args) => match args.args.first().unwrap() {
GenericArgument::Type(ty) => into_owned(ty, v.clone()),
_ => quote! { #v.into_owned() },
},
_ => quote! { #v.into_owned() },
};
quote! { #name.map(|#v| #into_owned) }
} else if last.ident == "Vec"
|| last.ident == "SmallVec"
|| last.ident == "CustomIdentList"
|| last.ident == "AnimationList"
|| last.ident == "AnimationNameList"
{
let v = quote! { v };
let into_owned = match &last.arguments {
PathArguments::AngleBracketed(args) => match args.args.first().unwrap() {
GenericArgument::Type(ty) => into_owned(ty, v.clone()),
_ => quote! { #v.into_owned() },
},
_ => quote! { #v.into_owned() },
};
quote! { #name.into_iter().map(|#v| #into_owned).collect() }
} else if last.ident == "Box" {
quote! { Box::new(#name.into_owned()) }
} else {
quote! { #name.into_owned() }
}
}
Type::Reference(_) => panic!("can't derive IntoOwned on a type with references"),
_ => quote! { #name.into_owned() },
}
} else {
quote! { #name }
}
}
fn has_lifetime(ty: &Type) -> bool {
match ty {
Type::Path(path) => path.path.segments.iter().any(|s| match &s.arguments {
PathArguments::AngleBracketed(args) => args.args.iter().any(|arg| match arg {
GenericArgument::Lifetime(..) => true,
GenericArgument::Type(ty) => has_lifetime(ty),
_ => false,
}),
_ => false,
}),
Type::Array(arr) => has_lifetime(&*arr.elem),
_ => false, // TODO
}
}