]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Support mapping MaybeReadable
authorMatt Corallo <git@bluematt.me>
Fri, 19 Nov 2021 20:04:36 +0000 (20:04 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 30 Nov 2021 15:39:48 +0000 (15:39 +0000)
This fixes https://github.com/lightningdevkit/ldk-garbagecollected/issues/62

c-bindings-gen/src/main.rs
lightning-c-bindings/src/c_types/mod.rs

index 1976160f84e4148f8cd086879605e784be407135..4d5b07021c7cd110aafe6917fa77a2f5dd70880a 100644 (file)
@@ -84,9 +84,9 @@ fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path
                                        writeln!(w, "}}").unwrap();
                                }
                        },
-                       "lightning::util::ser::Readable"|"lightning::util::ser::ReadableArgs" => {
+                       "lightning::util::ser::Readable"|"lightning::util::ser::ReadableArgs"|"lightning::util::ser::MaybeReadable" => {
                                // Create the Result<Object, DecodeError> syn::Type
-                               let res_ty: syn::Type = parse_quote!(Result<#for_ty, ::ln::msgs::DecodeError>);
+                               let mut res_ty: syn::Type = parse_quote!(Result<#for_ty, ::ln::msgs::DecodeError>);
 
                                writeln!(w, "#[no_mangle]").unwrap();
                                writeln!(w, "/// Read a {} from a byte array, created by {}_write", for_obj, for_obj).unwrap();
@@ -111,6 +111,8 @@ fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path
                                                        types.write_from_c_conversion_suffix(&mut arg_conv, &args_ty, Some(generics));
                                                } else { unreachable!(); }
                                        } else { unreachable!(); }
+                               } else if t == "lightning::util::ser::MaybeReadable" {
+                                       res_ty = parse_quote!(Result<Option<#for_ty>, ::ln::msgs::DecodeError>);
                                }
                                write!(w, ") -> ").unwrap();
                                types.write_c_type(w, &res_ty, Some(generics), false);
@@ -118,12 +120,19 @@ 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\tlet res: ").unwrap();
-                                       // At least in one case we need type annotations here, so provide them.
-                                       types.write_rust_type(w, Some(generics), &res_ty);
+                                       write!(w, ";\n").unwrap();
+                               }
+
+                               write!(w, "\tlet res: ").unwrap();
+                               // At least in one case we need type annotations here, so provide them.
+                               types.write_rust_type(w, Some(generics), &res_ty);
+
+                               if t == "lightning::util::ser::ReadableArgs" {
                                        writeln!(w, " = crate::c_types::deserialize_obj_arg(ser, arg_conv);").unwrap();
+                               } else if t == "lightning::util::ser::MaybeReadable" {
+                                       writeln!(w, " = crate::c_types::maybe_deserialize_obj(ser);").unwrap();
                                } else {
-                                       writeln!(w, "\tlet res = crate::c_types::deserialize_obj(ser);").unwrap();
+                                       writeln!(w, " = crate::c_types::deserialize_obj(ser);").unwrap();
                                }
                                write!(w, "\t").unwrap();
                                if types.write_to_c_conversion_new_var(w, &format_ident!("res"), &res_ty, Some(generics), false) {
index c0c44bcf7470ae4b65924b2b552b012b2fb9f688..3d6f08accc3926d3153ecd45aab38498ce0c5f0e 100644 (file)
@@ -430,6 +430,9 @@ pub(crate) fn serialize_obj<I: lightning::util::ser::Writeable>(i: &I) -> derive
 pub(crate) fn deserialize_obj<I: lightning::util::ser::Readable>(s: u8slice) -> Result<I, lightning::ln::msgs::DecodeError> {
        I::read(&mut s.to_slice())
 }
+pub(crate) fn maybe_deserialize_obj<I: lightning::util::ser::MaybeReadable>(s: u8slice) -> Result<Option<I>, lightning::ln::msgs::DecodeError> {
+       I::read(&mut s.to_slice())
+}
 pub(crate) fn deserialize_obj_arg<A, I: lightning::util::ser::ReadableArgs<A>>(s: u8slice, args: A) -> Result<I, lightning::ln::msgs::DecodeError> {
        I::read(&mut s.to_slice(), args)
 }