From: Matt Corallo Date: Wed, 30 Dec 2020 22:50:43 +0000 (-0500) Subject: [bindings] Use common conv in `_write` impls, drop type restrictions X-Git-Tag: v0.0.13~37^2~10 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=dfef21c66628dae5e38d3273572ff0ba668b9ffc;p=rust-lightning [bindings] Use common conv in `_write` impls, drop type restrictions This expands the manual implementation logic for `*_write` and `*_read` methods to most types, converting the `*_write` path to the common type-conversion logic to ensure it works. Note that `*_write_void` is still only implemented for has-inner types, as its unclear what the `void*` would point to for others. --- diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index 5905f62f8..c197839c9 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -63,28 +63,46 @@ fn maybe_convert_trait_impl(w: &mut W, trait_path: &syn::Path if let Some(t) = types.maybe_resolve_path(&trait_path, None) { let for_obj; let full_obj_path; + let mut has_inner = false; if let syn::Type::Path(ref p) = for_ty { if let Some(ident) = p.path.get_ident() { - let s = types.maybe_resolve_ident(ident).unwrap(); - if !types.crate_types.opaques.get(&s).is_some() { return; } - for_obj = format!("{}", ident); full_obj_path = for_obj.clone(); + has_inner = types.c_type_has_inner_from_path(&types.resolve_path(&p.path, None)); } else { return; } } else { - return; + // We assume that anything that isn't a Path is somehow a generic that ends up in our + // derived-types module. + let mut for_obj_vec = Vec::new(); + types.write_c_type(&mut for_obj_vec, for_ty, None, false); + full_obj_path = String::from_utf8(for_obj_vec).unwrap(); + assert!(full_obj_path.starts_with(TypeResolver::generated_container_path())); + for_obj = full_obj_path[TypeResolver::generated_container_path().len() + 2..].into(); } match &t as &str { "util::ser::Writeable" => { writeln!(w, "#[no_mangle]").unwrap(); writeln!(w, "pub extern \"C\" fn {}_write(obj: *const {}) -> crate::c_types::derived::CVec_u8Z {{", for_obj, full_obj_path).unwrap(); - writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &(*(*obj).inner) }})").unwrap(); - writeln!(w, "}}").unwrap(); - writeln!(w, "#[no_mangle]").unwrap(); - writeln!(w, "pub(crate) extern \"C\" fn {}_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {{", for_obj).unwrap(); - writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &*(obj as *const native{}) }})", for_obj).unwrap(); + + let ref_type = syn::Type::Reference(syn::TypeReference { + and_token: syn::Token!(&)(Span::call_site()), lifetime: None, mutability: None, + elem: Box::new(for_ty.clone()) }); + assert!(!types.write_from_c_conversion_new_var(w, &syn::Ident::new("obj", Span::call_site()), &ref_type, None)); + + write!(w, "\tcrate::c_types::serialize_obj(").unwrap(); + types.write_from_c_conversion_prefix(w, &ref_type, None); + write!(w, "unsafe {{ &*obj }}").unwrap(); + types.write_from_c_conversion_suffix(w, &ref_type, None); + writeln!(w, ")").unwrap(); + writeln!(w, "}}").unwrap(); + if has_inner { + writeln!(w, "#[no_mangle]").unwrap(); + writeln!(w, "pub(crate) extern \"C\" fn {}_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {{", for_obj).unwrap(); + writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &*(obj as *const native{}) }})", for_obj).unwrap(); + writeln!(w, "}}").unwrap(); + } }, "util::ser::Readable" => { // Create the Result syn::Type diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index b5c17f3eb..7ecd9ef1e 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -727,7 +727,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { /// Returns the module path in the generated mapping crate to the containers which we generate /// when writing to CrateTypes::template_file. - fn generated_container_path() -> &'static str { + pub fn generated_container_path() -> &'static str { "crate::c_types::derived" } /// Returns the module path in the generated mapping crate to the container templates, which @@ -914,7 +914,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { self.declared.get(ident) } /// Returns true if the object at the given path is mapped as X { inner: *mut origX, .. }. - fn c_type_has_inner_from_path(&self, full_path: &str) -> bool{ + pub fn c_type_has_inner_from_path(&self, full_path: &str) -> bool{ self.crate_types.opaques.get(full_path).is_some() }