From 1d19255237cc31397bf494c72a7a113bcd33c761 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 1 Jul 2022 20:08:13 +0000 Subject: [PATCH] Support `&[[u8; N]]` passed to Rust. --- c-bindings-gen/src/types.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index 2ebfde3..c6d69cc 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -2045,6 +2045,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { write!(w, "{}", sliceconv(false, None)).unwrap(); } } + } else if let syn::Type::Array(_) = &*s.elem { + write!(w, "{}", sliceconv(false, Some(".map(|a| *a)"))).unwrap(); } else { unimplemented!(); } }, syn::Type::Tuple(t) => { @@ -2397,6 +2399,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { ptr_for_ref = true; convert_container!("Slice", 1, || ty.iter()); unimplemented!("convert_container should return true as container_lookup should succeed for slices"); + } else if let syn::Type::Array(_) = &*s.elem { + is_ref = false; + ptr_for_ref = true; + let arr_elem = [(*s.elem).clone()]; + convert_container!("Slice", 1, || arr_elem.iter()); + unimplemented!("convert_container should return true as container_lookup should succeed for slices"); } else { unimplemented!() } }, syn::Type::Tuple(t) => { @@ -2907,6 +2915,22 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { let mut segments = syn::punctuated::Punctuated::new(); segments.push(parse_quote!(Vec<#args>)); self.write_c_type_intern(w, &syn::Type::Path(syn::TypePath { qself: None, path: syn::Path { leading_colon: None, segments } }), generics, false, is_mut, ptr_for_ref, with_ref_lifetime, c_ty) + } else if let syn::Type::Array(a) = &*s.elem { + if let syn::Expr::Lit(l) = &a.len { + if let syn::Lit::Int(i) = &l.lit { + let mut buf = Vec::new(); + self.write_rust_type(&mut buf, generics, &*a.elem, false); + let arr_ty = String::from_utf8(buf).unwrap(); + + let arr_str = format!("[{}; {}]", arr_ty, i.base10_digits()); + let ty = self.c_type_from_path(&arr_str, false, ptr_for_ref).unwrap() + .rsplitn(2, "::").next().unwrap(); + + let mangled_container = format!("CVec_{}Z", ty); + write!(w, "{}::{}", Self::generated_container_path(), mangled_container).unwrap(); + self.check_create_container(mangled_container, "Vec", vec![&*s.elem], generics, false) + } else { false } + } else { false } } else { false } }, syn::Type::Tuple(t) => { -- 2.39.5