From 4b3c6041d34566b1f398cb3277482e505a6a36aa Mon Sep 17 00:00:00 2001 From: Alekos Filini Date: Mon, 26 Apr 2021 14:20:47 +0200 Subject: [PATCH] Replace more manually-built `syn::Path` with `parse_quote!()` --- c-bindings-gen/src/main.rs | 11 ++++------- c-bindings-gen/src/types.rs | 11 ++++++----- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index c869e32..d107f3e 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -1486,20 +1486,17 @@ fn walk_ast<'a>(ast_storage: &'a FullLibraryAST, crate_types: &mut CrateTypes<'a if process_alias { match &*t.ty { syn::Type::Path(p) => { + let t_ident = &t.ident; + // If its a path with no generics, assume we don't map the aliased type and map it opaque - let mut segments = syn::punctuated::Punctuated::new(); - segments.push(syn::PathSegment { - ident: t.ident.clone(), - arguments: syn::PathArguments::None, - }); - let path_obj = syn::Path { leading_colon: None, segments }; + let path_obj = parse_quote!(#t_ident); let args_obj = p.path.segments.last().unwrap().arguments.clone(); match crate_types.reverse_alias_map.entry(import_resolver.maybe_resolve_path(&p.path, None).unwrap()) { hash_map::Entry::Occupied(mut e) => { e.get_mut().push((path_obj, args_obj)); }, hash_map::Entry::Vacant(e) => { e.insert(vec![(path_obj, args_obj)]); }, } - crate_types.opaques.insert(type_path.clone(), &t.ident); + crate_types.opaques.insert(type_path, t_ident); }, _ => { crate_types.type_aliases.insert(type_path, import_resolver.resolve_imported_refs((*t.ty).clone())); diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index d27c0b9..c8b71d6 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -343,11 +343,13 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr ($ident: expr, $path_suffix: expr) => { if partial_path == "" && !dependencies.contains(&$ident) { new_path = format!("{}::{}{}", crate_name, $ident, $path_suffix); - path.push(syn::PathSegment { ident: format_ident!("{}", crate_name), arguments: syn::PathArguments::None }); + let crate_name_ident = format_ident!("{}", crate_name); + path.push(parse_quote!(#crate_name_ident)); } else { new_path = format!("{}{}{}", partial_path, $ident, $path_suffix); } - path.push(syn::PathSegment { ident: $ident.clone(), arguments: syn::PathArguments::None }); + let ident = &$ident; + path.push(parse_quote!(#ident)); } } match u { @@ -386,9 +388,8 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr fn insert_primitive(imports: &mut HashMap, id: &str) { let ident = format_ident!("{}", id); - let mut path = syn::punctuated::Punctuated::new(); - path.push(syn::PathSegment { ident: ident.clone(), arguments: syn::PathArguments::None }); - imports.insert(ident, (id.to_owned(), syn::Path { leading_colon: None, segments: path })); + let path = parse_quote!(#ident); + imports.insert(ident, (id.to_owned(), path)); } pub fn new(crate_name: &'mod_lifetime str, dependencies: &'mod_lifetime HashSet, module_path: &'mod_lifetime str, contents: &'crate_lft [syn::Item]) -> Self { -- 2.39.5