]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Support non-u8 (primitive) arrays
authorMatt Corallo <git@bluematt.me>
Tue, 28 Feb 2023 23:25:26 +0000 (23:25 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 6 Mar 2023 04:16:09 +0000 (04:16 +0000)
c-bindings-gen/src/types.rs

index 029fc63f807b721c9add26e21488d69c86f9c62c..3cf9643293ac1a25a199f1e2cf363d894ab44734 100644 (file)
@@ -2104,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!(); }
                        },
@@ -2958,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 }