]> git.bitcoin.ninja Git - rust-lightning/commitdiff
[bindings] Use common conv in `_write` impls, drop type restrictions
authorMatt Corallo <git@bluematt.me>
Wed, 30 Dec 2020 22:50:43 +0000 (17:50 -0500)
committerMatt Corallo <git@bluematt.me>
Wed, 3 Feb 2021 15:11:28 +0000 (10:11 -0500)
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.

c-bindings-gen/src/main.rs
c-bindings-gen/src/types.rs

index 5905f62f8cf0d111c7e98205cc7bb0c11475abb2..c197839c9249a3443396ff90159280f4c5a4857f 100644 (file)
@@ -63,28 +63,46 @@ fn maybe_convert_trait_impl<W: std::io::Write>(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<Object, DecodeError> syn::Type
index b5c17f3eb8bb900e7afed88416c28f2e357008a6..7ecd9ef1ed4b6c57feb69ea9178ce6c41b560676 100644 (file)
@@ -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()
        }