Allow aliases and C++ opaque types to be trivial.
This change allows aliases:
type Foo = bindgen::Bar;
and C++ opaque types:
type Foo;
to declare that they're 'trivial' in the sense that:
* They have trivial move constructors
* They have no destructors
and therefore may be passed and owned by value in Rust.
A subsequent commit will add C++ static assertions.
This commit is a BREAKING CHANGE as it requires existing
ExternTypes to gain a new associated type
diff --git a/macro/src/expand.rs b/macro/src/expand.rs
index 879431e..fc7c692 100644
--- a/macro/src/expand.rs
+++ b/macro/src/expand.rs
@@ -57,6 +57,10 @@
Api::TypeAlias(alias) => {
expanded.extend(expand_type_alias(alias));
hidden.extend(expand_type_alias_verify(namespace, alias));
+ let ident = &alias.ident;
+ if types.required_trivial_aliases.contains(ident) {
+ hidden.extend(expand_type_alias_kind_trivial_verify(alias));
+ }
}
}
}
@@ -179,6 +183,7 @@
unsafe impl ::cxx::ExternType for #ident {
type Id = #type_id;
+ type Kind = ::cxx::Opaque;
}
}
}
@@ -677,6 +682,18 @@
}
}
+fn expand_type_alias_kind_trivial_verify(type_alias: &TypeAlias) -> TokenStream {
+ let ident = &type_alias.ident;
+ let begin_span = type_alias.type_token.span;
+ let end_span = type_alias.semi_token.span;
+ let begin = quote_spanned!(begin_span=> ::cxx::private::verify_extern_kind::<);
+ let end = quote_spanned!(end_span=> >);
+
+ quote! {
+ const _: fn() = #begin #ident, ::cxx::Trivial #end;
+ }
+}
+
fn type_id(namespace: &Namespace, ident: &Ident) -> TokenStream {
let mut path = String::new();
for name in namespace {