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/src/extern_type.rs b/src/extern_type.rs
index 6701ef5..951bc6c 100644
--- a/src/extern_type.rs
+++ b/src/extern_type.rs
@@ -104,7 +104,28 @@
/// # }
/// ```
type Id;
+
+ /// Either `kind::Opaque` or `kind::Trivial`. If in doubt, use
+ /// `kind::Opaque`.
+ type Kind;
+}
+
+pub(crate) mod kind {
+
+ /// An opaque type which can't be passed or held by value within Rust.
+ /// For example, a C++ type with a destructor, or a non-trivial move
+ /// constructor. Rust's strict move semantics mean that we can't own
+ /// these by value in Rust, but they can still be owned by a
+ /// `UniquePtr`...
+ pub struct Opaque;
+
+ /// A type with trivial move constructors and no destructor, which
+ /// can therefore be owned and moved around in Rust code directly.
+ pub struct Trivial;
}
#[doc(hidden)]
pub fn verify_extern_type<T: ExternType<Id = Id>, Id>() {}
+
+#[doc(hidden)]
+pub fn verify_extern_kind<T: ExternType<Kind = Kind>, Kind>() {}
diff --git a/src/lib.rs b/src/lib.rs
index 9efac51..2f21de9 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -395,6 +395,8 @@
pub use crate::cxx_string::CxxString;
pub use crate::cxx_vector::CxxVector;
pub use crate::exception::Exception;
+pub use crate::extern_type::kind::Opaque;
+pub use crate::extern_type::kind::Trivial;
pub use crate::extern_type::ExternType;
pub use crate::unique_ptr::UniquePtr;
pub use cxxbridge_macro::bridge;
@@ -422,6 +424,7 @@
#[doc(hidden)]
pub mod private {
pub use crate::cxx_vector::VectorElement;
+ pub use crate::extern_type::verify_extern_kind;
pub use crate::extern_type::verify_extern_type;
pub use crate::function::FatFunction;
pub use crate::opaque::Opaque;