Support mapping `Readable` `impl`s outside of the `lightning` crate
[ldk-c-bindings] / c-bindings-gen / src / main.rs
index 42fd470da778207a5dcd039c56dc869e670b3c1d..9c11cf374f1f2956205b6fe37dd4551b0f4908e8 100644 (file)
@@ -102,7 +102,7 @@ fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path
                        },
                        "lightning::util::ser::Readable"|"lightning::util::ser::ReadableArgs"|"lightning::util::ser::MaybeReadable" => {
                                // Create the Result<Object, DecodeError> syn::Type
-                               let mut res_ty: syn::Type = parse_quote!(Result<#for_ty, ::ln::msgs::DecodeError>);
+                               let mut res_ty: syn::Type = parse_quote!(Result<#for_ty, lightning::ln::msgs::DecodeError>);
 
                                writeln!(w, "#[no_mangle]").unwrap();
                                writeln!(w, "/// Read a {} from a byte array, created by {}_write", for_obj, for_obj).unwrap();
@@ -151,7 +151,7 @@ fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path
                                                } else { unreachable!(); }
                                        } else { unreachable!(); }
                                } else if t == "lightning::util::ser::MaybeReadable" {
-                                       res_ty = parse_quote!(Result<Option<#for_ty>, ::ln::msgs::DecodeError>);
+                                       res_ty = parse_quote!(Result<Option<#for_ty>, lightning::ln::msgs::DecodeError>);
                                }
                                write!(w, ") -> ").unwrap();
                                types.write_c_type(w, &res_ty, Some(generics), false);
@@ -735,6 +735,25 @@ fn writeln_struct<'a, 'b, W: std::io::Write>(w: &mut W, s: &'a syn::ItemStruct,
                                                write!(w, "inner_val").unwrap();
                                                types.write_to_c_conversion_inline_suffix(w, &ref_type, Some(&gen_types), true);
                                                writeln!(w, "\n}}").unwrap();
+                                       } else {
+                                               // If the type isn't reference-able, but is clonable, export a getter that just clones
+                                               if types.understood_c_type(&$field.ty, Some(&gen_types)) {
+                                                       let mut v = Vec::new();
+                                                       types.write_c_type(&mut v, &$field.ty, Some(&gen_types), true);
+                                                       let s = String::from_utf8(v).unwrap();
+                                                       if types.is_clonable(&s) {
+                                                               writeln_arg_docs(w, &$field.attrs, "", types, Some(&gen_types), vec![].drain(..), Some(&$field.ty));
+                                                               writeln!(w, "///\n/// Returns a copy of the field.").unwrap();
+                                                               write!(w, "#[no_mangle]\npub extern \"C\" fn {}_get_{}(this_ptr: &{}) -> {}", struct_name, $new_name, struct_name, s).unwrap();
+                                                               write!(w, " {{\n\tlet mut inner_val = this_ptr.get_native_mut_ref().{}.clone();\n\t", $real_name).unwrap();
+                                                               let local_var = types.write_to_c_conversion_new_var(w, &format_ident!("inner_val"), &$field.ty, Some(&gen_types), true);
+                                                               if local_var { write!(w, "\n\t").unwrap(); }
+                                                               types.write_to_c_conversion_inline_prefix(w, &$field.ty, Some(&gen_types), true);
+                                                               write!(w, "inner_val").unwrap();
+                                                               types.write_to_c_conversion_inline_suffix(w, &$field.ty, Some(&gen_types), true);
+                                                               writeln!(w, "\n}}").unwrap();
+                                                       }
+                                               }
                                        }
                                }