Support non-u8 (primitive) arrays
[ldk-c-bindings] / c-bindings-gen / src / types.rs
index 7132918f19bb44c293bf6cc22c8c81b232fd0a49..3cf9643293ac1a25a199f1e2cf363d894ab44734 100644 (file)
@@ -668,7 +668,7 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr
                                // Now that we've resolved the path to the path as-imported, check whether the path
                                // is actually a pub(.*) use statement and map it to the real path.
                                let path_tmp = path.clone();
-                               let crate_name = path_tmp.splitn(1, "::").next().unwrap();
+                               let crate_name = path_tmp.splitn(2, "::").next().unwrap();
                                let mut module_riter = path_tmp.rsplitn(2, "::");
                                let obj = module_riter.next().unwrap();
                                if let Some(module_path) = module_riter.next() {
@@ -1026,10 +1026,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
                        "bitcoin::secp256k1::Message" if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
                        "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
-                       |"lightning::ln::channelmanager::PaymentId"|"lightning::chain::keysinterface::KeyMaterial"
+                       |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
+                       |"lightning::chain::keysinterface::KeyMaterial"
                                if is_ref => Some("*const [u8; 32]"),
                        "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
-                       |"lightning::ln::channelmanager::PaymentId"|"lightning::chain::keysinterface::KeyMaterial"
+                       |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
+                       |"lightning::chain::keysinterface::KeyMaterial"
                                if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
 
                        "lightning::io::Read" => Some("crate::c_types::u8slice"),
@@ -1126,6 +1128,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "lightning::ln::PaymentSecret" if !is_ref => Some("::lightning::ln::PaymentSecret("),
                        "lightning::ln::channelmanager::PaymentId" if !is_ref => Some("::lightning::ln::channelmanager::PaymentId("),
                        "lightning::ln::channelmanager::PaymentId" if is_ref=> Some("&::lightning::ln::channelmanager::PaymentId( unsafe { *"),
+                       "lightning::ln::channelmanager::InterceptId" if !is_ref => Some("::lightning::ln::channelmanager::InterceptId("),
+                       "lightning::ln::channelmanager::InterceptId" if is_ref=> Some("&::lightning::ln::channelmanager::InterceptId( unsafe { *"),
                        "lightning::chain::keysinterface::KeyMaterial" if !is_ref => Some("::lightning::chain::keysinterface::KeyMaterial("),
                        "lightning::chain::keysinterface::KeyMaterial" if is_ref=> Some("&::lightning::chain::keysinterface::KeyMaterial( unsafe { *"),
 
@@ -1203,10 +1207,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::hash_types::Txid" => Some(".data[..]).unwrap()"),
                        "bitcoin::hash_types::BlockHash" if !is_ref => Some(".data[..]).unwrap()"),
                        "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
-                       |"lightning::ln::channelmanager::PaymentId"|"lightning::chain::keysinterface::KeyMaterial"
+                       |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
+                       |"lightning::chain::keysinterface::KeyMaterial"
                                if !is_ref => Some(".data)"),
                        "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
-                       |"lightning::ln::channelmanager::PaymentId"|"lightning::chain::keysinterface::KeyMaterial"
+                       |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
+                       |"lightning::chain::keysinterface::KeyMaterial"
                                if is_ref => Some(" })"),
 
                        // List of traits we map (possibly during processing of other files):
@@ -1303,10 +1309,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: "),
                        "bitcoin::secp256k1::Message" if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: "),
                        "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
-                       |"lightning::ln::channelmanager::PaymentId"|"lightning::chain::keysinterface::KeyMaterial"
+                       |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
+                       |"lightning::chain::keysinterface::KeyMaterial"
                                if is_ref => Some("&"),
                        "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
-                       |"lightning::ln::channelmanager::PaymentId"|"lightning::chain::keysinterface::KeyMaterial"
+                       |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
+                       |"lightning::chain::keysinterface::KeyMaterial"
                                if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: "),
 
                        "lightning::io::Read" => Some("crate::c_types::u8slice::from_vec(&crate::c_types::reader_to_vec("),
@@ -1384,10 +1392,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                if !is_ref => Some(".into_inner() }"),
                        "bitcoin::secp256k1::Message" if !is_ref => Some(".as_ref().clone() }"),
                        "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
-                       |"lightning::ln::channelmanager::PaymentId"|"lightning::chain::keysinterface::KeyMaterial"
+                       |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
+                       |"lightning::chain::keysinterface::KeyMaterial"
                                if is_ref => Some(".0"),
                        "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
-                       |"lightning::ln::channelmanager::PaymentId"|"lightning::chain::keysinterface::KeyMaterial"
+                       |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
+                       |"lightning::chain::keysinterface::KeyMaterial"
                                if !is_ref => Some(".0 }"),
 
                        "lightning::io::Read" => Some("))"),
@@ -2094,11 +2104,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                } else { unimplemented!(); }
                        },
                        syn::Type::Array(a) => {
-                               // We assume all arrays contain only [int_literal; X]s.
-                               // This may result in some outputs not compiling.
-                               if let syn::Expr::Lit(l) = &a.len {
-                                       if let syn::Lit::Int(i) = &l.lit {
-                                               write!(w, "{}", path_lookup(&format!("[u8; {}]", i.base10_digits()), is_ref, ptr_for_ref).unwrap()).unwrap();
+                               if let syn::Type::Path(p) = &*a.elem {
+                                       let inner_ty = self.resolve_path(&p.path, generics);
+                                       if let syn::Expr::Lit(l) = &a.len {
+                                               if let syn::Lit::Int(i) = &l.lit {
+                                                       write!(w, "{}", path_lookup(&format!("[{}; {}]", inner_ty, i.base10_digits()), is_ref, ptr_for_ref).unwrap()).unwrap();
+                                               } else { unimplemented!(); }
                                        } else { unimplemented!(); }
                                } else { unimplemented!(); }
                        },
@@ -2367,7 +2378,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                        write!(w, "let mut local_{}{} = ", ident,
                                                if (!to_c && needs_ref_map) || (to_c && $container_type == "Option" && contains_slice) {"_base"} else { "" }).unwrap();
                                        if prefix_location == ContainerPrefixLocation::OutsideConv {
-                                               var_prefix(w, $args_iter().next().unwrap(), generics, is_ref, ptr_for_ref, true);
+                                               var_prefix(w, $args_iter().next().unwrap(), generics, is_ref, true, true);
                                        }
                                        write!(w, "{}{}", prefix, var).unwrap();
 
@@ -2948,15 +2959,14 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                } else if is_ref {
                                        write!(w, "*const [").unwrap();
                                        if !self.write_c_type_intern(w, &a.elem, generics, false, false, ptr_for_ref, with_ref_lifetime, c_ty) { return false; }
-                               } else {
-                                       let mut typecheck = Vec::new();
-                                       if !self.write_c_type_intern(&mut typecheck, &a.elem, generics, false, false, ptr_for_ref, with_ref_lifetime, c_ty) { return false; }
-                                       if typecheck[..] != ['u' as u8, '8' as u8] { return false; }
                                }
                                if let syn::Expr::Lit(l) = &a.len {
                                        if let syn::Lit::Int(i) = &l.lit {
+                                               let mut inner_ty = Vec::new();
+                                               if !self.write_c_type_intern(&mut inner_ty, &*a.elem, generics, false, false, ptr_for_ref, false, c_ty) { return false; }
+                                               let inner_ty_str = String::from_utf8(inner_ty).unwrap();
                                                if !is_ref {
-                                                       if let Some(ty) = self.c_type_from_path(&format!("[u8; {}]", i.base10_digits()), false, ptr_for_ref) {
+                                                       if let Some(ty) = self.c_type_from_path(&format!("[{}; {}]", inner_ty_str, i.base10_digits()), false, ptr_for_ref) {
                                                                write!(w, "{}", ty).unwrap();
                                                                true
                                                        } else { false }