X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=c-bindings-gen%2Fsrc%2Fmain.rs;h=9c11cf374f1f2956205b6fe37dd4551b0f4908e8;hb=f180d14d64d6513b9c96573075680b2b1b9b92f3;hp=b9dba8c92e78188d3bb4abf4a80779fcb9ec2732;hpb=276f2e7e6cf0149d56ca72c780e963938feb7206;p=ldk-c-bindings diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index b9dba8c..9c11cf3 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -102,7 +102,7 @@ fn maybe_convert_trait_impl(w: &mut W, trait_path: &syn::Path }, "lightning::util::ser::Readable"|"lightning::util::ser::ReadableArgs"|"lightning::util::ser::MaybeReadable" => { // Create the Result 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: &mut W, trait_path: &syn::Path } else { unreachable!(); } } else { unreachable!(); } } else if t == "lightning::util::ser::MaybeReadable" { - res_ty = parse_quote!(Result, ::ln::msgs::DecodeError>); + res_ty = parse_quote!(Result, lightning::ln::msgs::DecodeError>); } write!(w, ") -> ").unwrap(); types.write_c_type(w, &res_ty, Some(generics), false); @@ -163,7 +163,7 @@ fn maybe_convert_trait_impl(w: &mut W, trait_path: &syn::Path 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); + types.write_rust_type(w, Some(generics), &res_ty, false); if t == "lightning::util::ser::ReadableArgs" { writeln!(w, " = crate::c_types::deserialize_obj_arg(ser, arg_conv);").unwrap(); @@ -484,7 +484,7 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty } _ => panic!("5"), } - $type_resolver.write_rust_type(w, Some(&gen_types), &*arg.ty); + $type_resolver.write_rust_type(w, Some(&gen_types), &*arg.ty, false); } } } @@ -492,7 +492,7 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty match &m.sig.output { syn::ReturnType::Type(_, rtype) => { write!(w, " -> ").unwrap(); - $type_resolver.write_rust_type(w, Some(&gen_types), &*rtype) + $type_resolver.write_rust_type(w, Some(&gen_types), &*rtype, 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(); + } + } } }