From: Matt Corallo Date: Wed, 2 Mar 2022 03:19:46 +0000 (+0000) Subject: Add function-call generic parameters when they're resovable X-Git-Tag: v0.0.105.0^2~4 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=76eed6fb43c1d675195aff896efb325828c76498;p=ldk-c-bindings Add function-call generic parameters when they're resovable We really shouldn't ever actually need to do this - rustc should be smart enough to figure out what we're doing given all the parameters are concrete types, but this appears to be failing sometimes. --- diff --git a/c-bindings-gen/src/blocks.rs b/c-bindings-gen/src/blocks.rs index 5034b09..e97fb65 100644 --- a/c-bindings-gen/src/blocks.rs +++ b/c-bindings-gen/src/blocks.rs @@ -745,27 +745,47 @@ pub fn write_method_call_params(w: &mut W, sig: &syn::Signatu pub fn maybe_write_generics(w: &mut W, generics: &syn::Generics, types: &TypeResolver, concrete_lifetimes: bool) { let mut gen_types = GenericTypes::new(None); assert!(gen_types.learn_generics(generics, types)); - if !generics.params.is_empty() { - write!(w, "<").unwrap(); - for (idx, generic) in generics.params.iter().enumerate() { - match generic { - syn::GenericParam::Type(type_param) => { - write!(w, "{}", if idx != 0 { ", " } else { "" }).unwrap(); - let type_ident = &type_param.ident; - types.write_c_type_in_generic_param(w, &syn::parse_quote!(#type_ident), Some(&gen_types), false); - }, - syn::GenericParam::Lifetime(lt) => { - if concrete_lifetimes { - write!(w, "'static").unwrap(); - } else { - write!(w, "{}'{}", if idx != 0 { ", " } else { "" }, lt.lifetime.ident).unwrap(); + if generics.params.is_empty() { return; } + for generic in generics.params.iter() { + match generic { + syn::GenericParam::Type(type_param) => { + for bound in type_param.bounds.iter() { + match bound { + syn::TypeParamBound::Trait(t) => { + if let Some(trait_bound) = types.maybe_resolve_path(&t.path, None) { + if types.skip_path(&trait_bound) { + // Just hope rust deduces generic params if some bounds are skipable. + return; + } + } + } + _ => {}, } - }, - _ => unimplemented!(), + } } + _ => {}, + } + } + + write!(w, "<").unwrap(); + for (idx, generic) in generics.params.iter().enumerate() { + match generic { + syn::GenericParam::Type(type_param) => { + write!(w, "{}", if idx != 0 { ", " } else { "" }).unwrap(); + let type_ident = &type_param.ident; + types.write_c_type_in_generic_param(w, &syn::parse_quote!(#type_ident), Some(&gen_types), false); + }, + syn::GenericParam::Lifetime(lt) => { + if concrete_lifetimes { + write!(w, "'static").unwrap(); + } else { + write!(w, "{}'{}", if idx != 0 { ", " } else { "" }, lt.lifetime.ident).unwrap(); + } + }, + _ => unimplemented!(), } - write!(w, ">").unwrap(); } + write!(w, ">").unwrap(); } pub fn maybe_write_lifetime_generics(w: &mut W, generics: &syn::Generics, types: &TypeResolver) { diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index c6a74f6..97f44a4 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -1647,10 +1647,20 @@ fn writeln_fn<'a, 'b, W: std::io::Write>(w: &mut W, f: &'a syn::ItemFn, types: & writeln_fn_docs(w, &f.attrs, "", types, Some(&gen_types), f.sig.inputs.iter(), &f.sig.output); write!(w, "#[no_mangle]\npub extern \"C\" fn {}(", f.sig.ident).unwrap(); + + write_method_params(w, &f.sig, "", types, Some(&gen_types), false, true); write!(w, " {{\n\t").unwrap(); write_method_var_decl_body(w, &f.sig, "", types, Some(&gen_types), false); - write!(w, "{}::{}(", types.module_path, f.sig.ident).unwrap(); + write!(w, "{}::{}", types.module_path, f.sig.ident).unwrap(); + + let mut function_generic_args = Vec::new(); + maybe_write_generics(&mut function_generic_args, &f.sig.generics, types, true); + if !function_generic_args.is_empty() { + write!(w, "::{}", String::from_utf8(function_generic_args).unwrap()).unwrap(); + } + write!(w, "(").unwrap(); + write_method_call_params(w, &f.sig, "", types, Some(&gen_types), "", false); writeln!(w, "\n}}\n").unwrap(); } diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index 21b0458..88d6640 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -827,7 +827,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { // ************************************************* /// Returns true we if can just skip passing this to C entirely - fn skip_path(&self, full_path: &str) -> bool { + pub fn skip_path(&self, full_path: &str) -> bool { full_path == "bitcoin::secp256k1::Secp256k1" || full_path == "bitcoin::secp256k1::Signing" || full_path == "bitcoin::secp256k1::Verification"