]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Use `quote::format_ident!()` to make new `syn::Ident`
authorAlekos Filini <alekos.filini@gmail.com>
Mon, 26 Apr 2021 11:10:49 +0000 (13:10 +0200)
committerAlekos Filini <alekos.filini@gmail.com>
Mon, 26 Apr 2021 11:10:49 +0000 (13:10 +0200)
c-bindings-gen/Cargo.toml
c-bindings-gen/src/blocks.rs
c-bindings-gen/src/main.rs
c-bindings-gen/src/types.rs

index d963e4304943103301ab8756921bb4fcd0ece13c..d5f808920bf275e75f458eea1c5bb7a66d18b269 100644 (file)
@@ -7,6 +7,7 @@ edition = "2018"
 [dependencies]
 syn = { version = "1", features = ["full", "extra-traits"] }
 proc-macro2 = "1"
+quote = "1"
 
 [profile.release]
 incremental = true
index bbb41e09f5df03adab603bbb9cfc701503e8060d..56267792adecc779da22f0bd631449522dade700 100644 (file)
@@ -11,7 +11,9 @@
 
 use std::fs::File;
 use std::io::Write;
-use proc_macro2::{TokenTree, Span};
+
+use proc_macro2::TokenTree;
+use quote::format_ident;
 
 use crate::types::*;
 
@@ -502,7 +504,7 @@ pub fn write_method_params<W: std::io::Write>(w: &mut W, sig: &syn::Signature, t
 /// mut ret = " assuming the next print will be the unmapped Rust function to call followed by the
 /// parameters we mapped to/from C here.
 pub fn write_method_var_decl_body<W: std::io::Write>(w: &mut W, sig: &syn::Signature, extra_indent: &str, types: &TypeResolver, generics: Option<&GenericTypes>, to_c: bool) {
-       let mut num_unused = 0;
+       let mut num_unused = 0u32;
        for inp in sig.inputs.iter() {
                match inp {
                        syn::FnArg::Receiver(_) => {},
@@ -531,7 +533,7 @@ pub fn write_method_var_decl_body<W: std::io::Write>(w: &mut W, sig: &syn::Signa
                                        },
                                        syn::Pat::Wild(w) => {
                                                if !w.attrs.is_empty() { unimplemented!(); }
-                                               write_new_var!(syn::Ident::new(&format!("unused_{}", num_unused), Span::call_site()), *arg.ty);
+                                               write_new_var!(format_ident!("unused_{}", num_unused), *arg.ty);
                                                num_unused += 1;
                                        },
                                        _ => unimplemented!(),
@@ -624,7 +626,7 @@ pub fn write_method_call_params<W: std::io::Write>(w: &mut W, sig: &syn::Signatu
                                // If we're returning "Self" (and not "Self::X"), just do it manually
                                write!(w, "{} {{ inner: Box::into_raw(Box::new(ret)), is_owned: true }}", this_type).unwrap();
                        } else if to_c {
-                               let new_var = types.write_from_c_conversion_new_var(w, &syn::Ident::new("ret", Span::call_site()), rtype, generics);
+                               let new_var = types.write_from_c_conversion_new_var(w, &format_ident!("ret"), rtype, generics);
                                if new_var {
                                        write!(w, "\n\t{}", extra_indent).unwrap();
                                }
@@ -633,7 +635,7 @@ pub fn write_method_call_params<W: std::io::Write>(w: &mut W, sig: &syn::Signatu
                                types.write_from_c_conversion_suffix(w, &*rtype, generics);
                        } else {
                                let ret_returned = if let syn::Type::Reference(_) = &**rtype { true } else { false };
-                               let new_var = types.write_to_c_conversion_new_var(w, &syn::Ident::new("ret", Span::call_site()), &rtype, generics, true);
+                               let new_var = types.write_to_c_conversion_new_var(w, &format_ident!("ret"), &rtype, generics, true);
                                if new_var {
                                        write!(w, "\n\t{}", extra_indent).unwrap();
                                }
index e30bb160247b3de02957ec89b8192db38bfee53c..c869e327bcdcf7956c83772a88da08893caffc57 100644 (file)
@@ -25,6 +25,7 @@ use std::io::{Read, Write};
 use std::process;
 
 use proc_macro2::Span;
+use quote::format_ident;
 use syn::parse_quote;
 
 mod types;
@@ -65,7 +66,7 @@ fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path
                                writeln!(w, "pub extern \"C\" fn {}_write(obj: &{}) -> crate::c_types::derived::CVec_u8Z {{", for_obj, full_obj_path).unwrap();
 
                                let ref_type: syn::Type = syn::parse_quote!(&#for_ty);
-                               assert!(!types.write_from_c_conversion_new_var(w, &syn::Ident::new("obj", Span::call_site()), &ref_type, Some(generics)));
+                               assert!(!types.write_from_c_conversion_new_var(w, &format_ident!("obj"), &ref_type, Some(generics)));
 
                                write!(w, "\tcrate::c_types::serialize_obj(").unwrap();
                                types.write_from_c_conversion_prefix(w, &ref_type, Some(generics));
@@ -100,7 +101,7 @@ fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path
                                                if let syn::GenericArgument::Type(args_ty) = args.args.iter().next().unwrap() {
                                                        types.write_c_type(w, args_ty, Some(generics), false);
 
-                                                       assert!(!types.write_from_c_conversion_new_var(&mut arg_conv, &syn::Ident::new("arg", Span::call_site()), &args_ty, Some(generics)));
+                                                       assert!(!types.write_from_c_conversion_new_var(&mut arg_conv, &format_ident!("arg"), &args_ty, Some(generics)));
 
                                                        write!(&mut arg_conv, "\tlet arg_conv = ").unwrap();
                                                        types.write_from_c_conversion_prefix(&mut arg_conv, &args_ty, Some(generics));
@@ -123,7 +124,7 @@ fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path
                                        writeln!(w, "\tlet res = crate::c_types::deserialize_obj(ser);").unwrap();
                                }
                                write!(w, "\t").unwrap();
-                               if types.write_to_c_conversion_new_var(w, &syn::Ident::new("res", Span::call_site()), &res_ty, Some(generics), false) {
+                               if types.write_to_c_conversion_new_var(w, &format_ident!("res"), &res_ty, Some(generics), false) {
                                        write!(w, "\n\t").unwrap();
                                }
                                types.write_to_c_conversion_inline_prefix(w, &res_ty, Some(generics), false);
@@ -602,7 +603,7 @@ fn writeln_struct<'a, 'b, W: std::io::Write>(w: &mut W, s: &'a syn::ItemStruct,
                                                write!(w, "#[no_mangle]\npub extern \"C\" fn {}_get_{}(this_ptr: &{}) -> ", struct_name, ident, struct_name).unwrap();
                                                types.write_c_type(w, &ref_type, Some(&gen_types), true);
                                                write!(w, " {{\n\tlet mut inner_val = &mut unsafe {{ &mut *this_ptr.inner }}.{};\n\t", ident).unwrap();
-                                               let local_var = types.write_to_c_conversion_new_var(w, &syn::Ident::new("inner_val", Span::call_site()), &ref_type, Some(&gen_types), true);
+                                               let local_var = types.write_to_c_conversion_new_var(w, &format_ident!("inner_val"), &ref_type, Some(&gen_types), true);
                                                if local_var { write!(w, "\n\t").unwrap(); }
                                                types.write_to_c_conversion_inline_prefix(w, &ref_type, Some(&gen_types), true);
                                                if local_var {
@@ -619,7 +620,7 @@ fn writeln_struct<'a, 'b, W: std::io::Write>(w: &mut W, s: &'a syn::ItemStruct,
                                                write!(w, "#[no_mangle]\npub extern \"C\" fn {}_set_{}(this_ptr: &mut {}, mut val: ", struct_name, ident, struct_name).unwrap();
                                                types.write_c_type(w, &field.ty, Some(&gen_types), false);
                                                write!(w, ") {{\n\t").unwrap();
-                                               let local_var = types.write_from_c_conversion_new_var(w, &syn::Ident::new("val", Span::call_site()), &field.ty, Some(&gen_types));
+                                               let local_var = types.write_from_c_conversion_new_var(w, &format_ident!("val"), &field.ty, Some(&gen_types));
                                                if local_var { write!(w, "\n\t").unwrap(); }
                                                write!(w, "unsafe {{ &mut *this_ptr.inner }}.{} = ", ident).unwrap();
                                                types.write_from_c_conversion_prefix(w, &field.ty, Some(&gen_types));
@@ -642,8 +643,8 @@ fn writeln_struct<'a, 'b, W: std::io::Write>(w: &mut W, s: &'a syn::ItemStruct,
                        }
                        write!(w, ") -> {} {{\n\t", struct_name).unwrap();
                        for field in fields.named.iter() {
-                               let field_name = format!("{}_arg", field.ident.as_ref().unwrap());
-                               if types.write_from_c_conversion_new_var(w, &syn::Ident::new(&field_name, Span::call_site()), &field.ty, Some(&gen_types)) {
+                               let field_ident = format_ident!("{}_arg", field.ident.as_ref().unwrap());
+                               if types.write_from_c_conversion_new_var(w, &field_ident, &field.ty, Some(&gen_types)) {
                                        write!(w, "\n\t").unwrap();
                                }
                        }
@@ -1165,7 +1166,7 @@ fn writeln_enum<'a, 'b, W: std::io::Write>(w: &mut W, e: &'a syn::ItemEnum, type
                                                        if $ref {
                                                                write!(w, "let mut {}_nonref = (*{}).clone();\n\t\t\t\t", $field_ident, $field_ident).unwrap();
                                                                if new_var {
-                                                                       let nonref_ident = syn::Ident::new(&format!("{}_nonref", $field_ident), Span::call_site());
+                                                                       let nonref_ident = format_ident!("{}_nonref", $field_ident);
                                                                        if $to_c {
                                                                                types.write_to_c_conversion_new_var(w, &nonref_ident, &$field.ty, None, false);
                                                                        } else {
@@ -1187,7 +1188,7 @@ fn writeln_enum<'a, 'b, W: std::io::Write>(w: &mut W, e: &'a syn::ItemEnum, type
                                } else if let syn::Fields::Unnamed(fields) = &var.fields {
                                        write!(w, " {{\n\t\t\t\t").unwrap();
                                        for (idx, field) in fields.unnamed.iter().enumerate() {
-                                               handle_field_a!(field, &syn::Ident::new(&(('a' as u8 + idx as u8) as char).to_string(), Span::call_site()));
+                                               handle_field_a!(field, &format_ident!("{}", ('a' as u8 + idx as u8) as char));
                                        }
                                } else { write!(w, " ").unwrap(); }
 
@@ -1225,7 +1226,7 @@ fn writeln_enum<'a, 'b, W: std::io::Write>(w: &mut W, e: &'a syn::ItemEnum, type
                                        write!(w, " (").unwrap();
                                        for (idx, field) in fields.unnamed.iter().enumerate() {
                                                write!(w, "\n\t\t\t\t\t").unwrap();
-                                               handle_field_b!(field, &syn::Ident::new(&(('a' as u8 + idx as u8) as char).to_string(), Span::call_site()));
+                                               handle_field_b!(field, &format_ident!("{}", ('a' as u8 + idx as u8) as char));
                                        }
                                        writeln!(w, "\n\t\t\t\t)").unwrap();
                                        write!(w, "\t\t\t}}").unwrap();
index 85fd01c9e6bf2e7efdb90248e47ad1d8e5dad409..d27c0b96b2a209794e7ecd645c16521f704a7b10 100644 (file)
@@ -15,6 +15,7 @@ use std::hash;
 use crate::blocks::*;
 
 use proc_macro2::{TokenTree, Span};
+use quote::format_ident;
 use syn::parse_quote;
 
 // The following utils are used purely to build our known types maps - they break down all the
@@ -342,7 +343,7 @@ 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: syn::Ident::new(crate_name, Span::call_site()), arguments: syn::PathArguments::None });
+                                       path.push(syn::PathSegment { ident: format_ident!("{}", crate_name), arguments: syn::PathArguments::None });
                                } else {
                                        new_path = format!("{}{}{}", partial_path, $ident, $path_suffix);
                                }
@@ -384,7 +385,7 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr
        }
 
        fn insert_primitive(imports: &mut HashMap<syn::Ident, (String, syn::Path)>, id: &str) {
-               let ident = syn::Ident::new(id, Span::call_site());
+               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 }));
@@ -1851,7 +1852,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
 
                                                write!(w, "{} {{ ", pfx).unwrap();
                                                let new_var_name = format!("{}_{}", ident, idx);
-                                               let new_var = self.write_conversion_new_var_intern(w, &syn::Ident::new(&new_var_name, Span::call_site()),
+                                               let new_var = self.write_conversion_new_var_intern(w, &format_ident!("{}", new_var_name),
                                                                &var_access, conv_ty, generics, contains_slice || (is_ref && ty_has_inner), ptr_for_ref, to_c, path_lookup, container_lookup, var_prefix, var_suffix);
                                                if new_var { write!(w, " ").unwrap(); }
 
@@ -1976,7 +1977,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                        for (idx, elem) in t.elems.iter().enumerate() {
                                                if let syn::Type::Path(p) = elem {
                                                        let v_name = format!("orig_{}_{}", ident, idx);
-                                                       let tuple_elem_ident = syn::Ident::new(&v_name, Span::call_site());
+                                                       let tuple_elem_ident = format_ident!("{}", &v_name);
                                                        if self.write_conversion_new_var_intern(w, &tuple_elem_ident, &v_name, elem, generics,
                                                                        false, ptr_for_ref, to_c,
                                                                        path_lookup, container_lookup, var_prefix, var_suffix) {