From c3696824856ba541c664428293e959c2ad500658 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 18 Apr 2021 00:44:55 +0000 Subject: [PATCH] Handle `Vec<&Thing>` conversion to C. --- c-bindings-gen/src/types.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index 567cc7d..3e7d0d6 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -1158,6 +1158,11 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "Vec" if !is_ref => { Some(("Vec::new(); for mut item in ", vec![(format!(".drain(..) {{ local_{}.push(", var_name), "item".to_string())], "); }", ContainerPrefixLocation::PerConv)) }, + "Vec" => { + // We should only get here if the single contained has an inner + assert!(self.c_type_has_inner(single_contained.unwrap())); + Some(("Vec::new(); for mut item in ", vec![(format!(".drain(..) {{ local_{}.push(", var_name), "*item".to_string())], "); }", ContainerPrefixLocation::PerConv)) + }, "Slice" => { Some(("Vec::new(); for item in ", vec![(format!(".iter() {{ local_{}.push(", var_name), "*item".to_string())], "); }", ContainerPrefixLocation::PerConv)) }, @@ -1271,9 +1276,19 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { self.types.get_declared_type(ident) } /// Returns true if the object at the given path is mapped as X { inner: *mut origX, .. }. - pub 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() } + /// Returns true if the object at the given path is mapped as X { inner: *mut origX, .. }. + pub fn c_type_has_inner(&self, ty: &syn::Type) -> bool { + match ty { + syn::Type::Path(p) => { + let full_path = self.resolve_path(&p.path, None); + self.c_type_has_inner_from_path(&full_path) + }, + _ => false, + } + } pub fn maybe_resolve_ident(&self, id: &syn::Ident) -> Option { self.types.maybe_resolve_ident(id) -- 2.30.2