Support mapping MaybeReadable
[ldk-c-bindings] / c-bindings-gen / src / main.rs
index 51f67f750678fe9cfb6e0328c1ef0234b843153e..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) {
@@ -670,7 +679,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, $new_name, struct_name).unwrap();
                                                types.write_c_type(w, &ref_type, Some(&gen_types), true);
                                                write!(w, " {{\n\tlet mut inner_val = &mut this_ptr.get_native_mut_ref().{};\n\t", $real_name).unwrap();
-                                               let local_var = types.write_to_c_conversion_new_var(w, &format_ident!("inner_val"), &ref_type, Some(&gen_types), true);
+                                               let local_var = types.write_to_c_conversion_from_ownable_ref_new_var(w, &format_ident!("inner_val"), &ref_type, Some(&gen_types));
                                                if local_var { write!(w, "\n\t").unwrap(); }
                                                types.write_to_c_conversion_inline_prefix(w, &ref_type, Some(&gen_types), true);
                                                write!(w, "inner_val").unwrap();
@@ -1259,7 +1268,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
                                                                        write!(w, "#[no_mangle]\npub extern \"C\" fn {}_{}(", ident, m.sig.ident).unwrap();
                                                                        let ret_type = match &declared_type {
                                                                                DeclType::MirroredEnum => format!("{}", ident),
-                                                                               DeclType::StructImported => format!("{}", ident),
+                                                                               DeclType::StructImported {..} => format!("{}", ident),
                                                                                _ => unimplemented!(),
                                                                        };
                                                                        write_method_params(w, &m.sig, &ret_type, types, Some(&meth_gen_types), false, true);
@@ -1280,7 +1289,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
                                                                        } else {
                                                                                match &declared_type {
                                                                                        DeclType::MirroredEnum => write!(w, "this_arg.to_native().{}(", m.sig.ident).unwrap(),
-                                                                                       DeclType::StructImported => {
+                                                                                       DeclType::StructImported {..} => {
                                                                                                if takes_owned_self {
                                                                                                        write!(w, "(*unsafe {{ Box::from_raw(this_arg.take_inner()) }}).{}(", m.sig.ident).unwrap();
                                                                                                } else if takes_mut_self {
@@ -1846,7 +1855,7 @@ fn walk_ast<'a>(ast_storage: &'a FullLibraryAST, crate_types: &mut CrateTypes<'a
                                                        ExportStatus::NotImplementable => panic!("(C-not implementable) must only appear on traits"),
                                                }
                                                let struct_path = format!("{}::{}", module, s.ident);
-                                               crate_types.opaques.insert(struct_path, &s.ident);
+                                               crate_types.opaques.insert(struct_path, (&s.ident, &s.generics));
                                        }
                                },
                                syn::Item::Trait(t) => {
@@ -1891,7 +1900,7 @@ fn walk_ast<'a>(ast_storage: &'a FullLibraryAST, crate_types: &mut CrateTypes<'a
                                                                                hash_map::Entry::Vacant(e) => { e.insert(vec![(path_obj, args_obj)]); },
                                                                        }
 
-                                                                       crate_types.opaques.insert(type_path, t_ident);
+                                                                       crate_types.opaques.insert(type_path, (t_ident, &t.generics));
                                                                },
                                                                _ => {
                                                                        crate_types.type_aliases.insert(type_path, import_resolver.resolve_imported_refs((*t.ty).clone()));
@@ -1908,7 +1917,7 @@ fn walk_ast<'a>(ast_storage: &'a FullLibraryAST, crate_types: &mut CrateTypes<'a
                                                        ExportStatus::NotImplementable => panic!("(C-not implementable) must only appear on traits"),
                                                }
                                                let enum_path = format!("{}::{}", module, e.ident);
-                                               crate_types.opaques.insert(enum_path, &e.ident);
+                                               crate_types.opaques.insert(enum_path, (&e.ident, &e.generics));
                                        }
                                },
                                syn::Item::Enum(e) => {