Skip to content

Commit 84cefc5

Browse files
committed
Better detection of PhantomDeserData
1 parent 62a441a commit 84cefc5

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

epserde-derive/src/lib.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,16 @@ fn check_attrs(input: &DeriveInput) -> (bool, bool, bool) {
210210
(is_repr_c, is_zero_copy, is_deep_copy)
211211
}
212212

213+
/// Check if a type is PhantomDeserData
214+
fn is_phantom_deser_data(ty: &syn::Type) -> bool {
215+
if let syn::Type::Path(type_path) = ty {
216+
if let Some(segment) = type_path.path.segments.last() {
217+
return segment.ident == "PhantomDeserData";
218+
}
219+
}
220+
false
221+
}
222+
213223
/// Generate an ε-serde implementation for custom types.
214224
///
215225
/// It generates implementations for the traits `CopyType`,
@@ -291,10 +301,9 @@ pub fn epserde_derive(input: TokenStream) -> TokenStream {
291301
let mut methods: Vec<proc_macro2::TokenStream> = vec![];
292302

293303
s.fields.iter().for_each(|field| {
294-
let ty = &field.ty.to_token_stream().to_string();
295-
if ty.starts_with("PhantomDeserData") {
304+
if is_phantom_deser_data(&field.ty) {
296305
methods.push(syn::parse_quote!(_deserialize_eps_inner_special));
297-
} else if generics_names_raw.contains(ty) {
306+
} else if generics_names_raw.contains(&field.ty.to_token_stream().to_string()) {
298307
methods.push(syn::parse_quote!(_deserialize_eps_inner));
299308
} else {
300309
methods.push(syn::parse_quote!(_deserialize_full_inner));
@@ -610,11 +619,9 @@ pub fn epserde_derive(input: TokenStream) -> TokenStream {
610619
bounds: bounds_des,
611620
}));
612621

613-
// TODO don't do just string comparison
614-
let ty = ty.to_token_stream().to_string();
615-
if ty.starts_with("PhantomDeserData") {
622+
if is_phantom_deser_data(&ty) {
616623
methods.push(syn::parse_quote!(_deserialize_eps_inner_special));
617-
} else if generics_names_raw.contains(&ty) {
624+
} else if generics_names_raw.contains(&ty.to_token_stream().to_string()) {
618625
methods.push(syn::parse_quote!(_deserialize_eps_inner));
619626
} else {
620627
methods.push(syn::parse_quote!(_deserialize_full_inner));

0 commit comments

Comments
 (0)