]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Crack `ReadableArgs` params when they're tuples
authorMatt Corallo <git@bluematt.me>
Tue, 29 Mar 2022 22:33:27 +0000 (22:33 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 29 Mar 2022 22:53:34 +0000 (22:53 +0000)
This replaces the special handling we'd had in 0.0.105.2 for
tuples-containing-references by instead just cracking open the
tuples and converting each field individually when calling
`ReadableArgs` where the argument is a tuple.

c-bindings-gen/src/main.rs
c-bindings-gen/src/types.rs

index fbe3c99b64b1b9288784cd1921e280e592c9c93f..d62b2a315f5b366ec03c51fb44924a0abdf54120 100644 (file)
@@ -102,24 +102,44 @@ fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path
 
                                let mut arg_conv = Vec::new();
                                if t == "lightning::util::ser::ReadableArgs" {
-                                       write!(w, ", arg: ").unwrap();
                                        assert!(trait_path.leading_colon.is_none());
                                        let args_seg = trait_path.segments.iter().last().unwrap();
                                        assert_eq!(format!("{}", args_seg.ident), "ReadableArgs");
                                        if let syn::PathArguments::AngleBracketed(args) = &args_seg.arguments {
                                                assert_eq!(args.args.len(), 1);
                                                if let syn::GenericArgument::Type(args_ty) = args.args.iter().next().unwrap() {
-                                                       types.write_c_type(w, args_ty, Some(generics), false);
+                                                       macro_rules! write_arg_conv {
+                                                               ($ty: expr, $arg_name: expr) => {
+                                                                       write!(w, ", {}: ", $arg_name).unwrap();
+                                                                       types.write_c_type(w, $ty, Some(generics), false);
+
+                                                                       write!(&mut arg_conv, "\t").unwrap();
+                                                                       if types.write_from_c_conversion_new_var(&mut arg_conv, &format_ident!("{}", $arg_name), &$ty, Some(generics)) {
+                                                                               write!(&mut arg_conv, "\n\t").unwrap();
+                                                                       }
 
-                                                       write!(&mut arg_conv, "\t").unwrap();
-                                                       if types.write_from_c_conversion_new_var(&mut arg_conv, &format_ident!("arg"), &args_ty, Some(generics)) {
-                                                               write!(&mut arg_conv, "\n\t").unwrap();
+                                                                       write!(&mut arg_conv, "let {}_conv = ", $arg_name).unwrap();
+                                                                       types.write_from_c_conversion_prefix(&mut arg_conv, &$ty, Some(generics));
+                                                                       write!(&mut arg_conv, "{}", $arg_name).unwrap();
+                                                                       types.write_from_c_conversion_suffix(&mut arg_conv, &$ty, Some(generics));
+                                                                       write!(&mut arg_conv, ";\n").unwrap();
+                                                               }
                                                        }
 
-                                                       write!(&mut arg_conv, "let arg_conv = ").unwrap();
-                                                       types.write_from_c_conversion_prefix(&mut arg_conv, &args_ty, Some(generics));
-                                                       write!(&mut arg_conv, "arg").unwrap();
-                                                       types.write_from_c_conversion_suffix(&mut arg_conv, &args_ty, Some(generics));
+                                                       if let syn::Type::Tuple(tup) = args_ty {
+                                                               // Crack open tuples and make them separate arguments instead of
+                                                               // converting the full tuple. This makes it substantially easier to
+                                                               // reason about things like references in the tuple fields.
+                                                               let mut arg_conv_res = Vec::new();
+                                                               for (idx, elem) in tup.elems.iter().enumerate() {
+                                                                       let arg_name = format!("arg_{}", ('a' as u8 + idx as u8) as char);
+                                                                       write_arg_conv!(elem, arg_name);
+                                                                       write!(&mut arg_conv_res, "{}_conv{}", arg_name, if idx != tup.elems.len() - 1 { ", " } else { "" }).unwrap();
+                                                               }
+                                                               writeln!(&mut arg_conv, "\tlet arg_conv = ({});", String::from_utf8(arg_conv_res).unwrap()).unwrap();
+                                                       } else {
+                                                               write_arg_conv!(args_ty, "arg");
+                                                       }
                                                } else { unreachable!(); }
                                        } else { unreachable!(); }
                                } else if t == "lightning::util::ser::MaybeReadable" {
@@ -131,7 +151,6 @@ fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path
 
                                if t == "lightning::util::ser::ReadableArgs" {
                                        w.write(&arg_conv).unwrap();
-                                       write!(w, ";\n").unwrap();
                                }
 
                                write!(w, "\tlet res: ").unwrap();
index 28e05f8c363deac347ebfc503d1ae60ccbc9d1ce..ced1f34b0e91c31493127c14bf8c9e7db31748ac 100644 (file)
@@ -2714,7 +2714,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                if !c_ty {
                                        self.write_rust_path(w, generics, path);
                                } else {
-                                       write!(w, "{}", full_path).unwrap();
+                                       // We shouldn't be mapping references in types, so panic here
+                                       unimplemented!();
                                }
                        } else if is_ref {
                                write!(w, "&{}{}{}", if is_mut { "mut " } else { "" }, crate_pfx, full_path).unwrap();