54
54
/// ## Integrating with bindgen-generated types
55
55
///
56
56
/// Handwritten `ExternType` impls make it possible to plug in a data structure
57
- /// emitted by bindgen as the definition of an opaque C++ type emitted by CXX.
57
+ /// emitted by bindgen as the definition of a C++ type emitted by CXX.
58
58
///
59
59
/// By writing the unsafe `ExternType` impl, the programmer asserts that the C++
60
60
/// namespace and type name given in the type id refers to a C++ type that is
69
69
/// # pub struct StringPiece([usize; 2]);
70
70
/// # }
71
71
///
72
- /// use cxx::{type_id, ExternType};
72
+ /// use cxx::{type_id, ExternType, Opaque };
73
73
///
74
74
/// unsafe impl ExternType for folly_sys::StringPiece {
75
75
/// type Id = type_id!("folly::StringPiece");
76
+ /// type Kind = Opaque;
76
77
/// }
77
78
///
78
79
/// #[cxx::bridge(namespace = folly)]
92
93
/// #
93
94
/// # fn main() {}
94
95
/// ```
96
+ ///
97
+ /// ## Opaque and Trivial types
98
+ ///
99
+ /// Some C++ types are safe to hold and pass around in Rust, by value.
100
+ /// Those C++ types must have a trivial move constructor, and must
101
+ /// have no destructor.
102
+ ///
103
+ /// If you believe your C++ type is indeed trivial, you can specify
104
+ /// ```
105
+ /// # struct TypeName;
106
+ /// # unsafe impl cxx::ExternType for TypeName {
107
+ /// type Id = cxx::type_id!("name::space::of::TypeName");
108
+ /// type Kind = cxx::Trivial;
109
+ /// # }
110
+ /// ```
111
+ /// which will enable you to pass it into C++ functions by value,
112
+ /// return it by value from such functions, and include it in
113
+ /// `struct`s that you have declared to `cxx::bridge`. Your promises
114
+ /// about the triviality of the C++ type will be checked using
115
+ /// `static_assert`s in the generated C++.
116
+ ///
117
+ /// Opaque types can't be passed by value, but can still be held
118
+ /// in `UniquePtr`.
95
119
pub unsafe trait ExternType {
96
120
/// A type-level representation of the type's C++ namespace and type name.
97
121
///
@@ -101,12 +125,13 @@ pub unsafe trait ExternType {
101
125
/// # struct TypeName;
102
126
/// # unsafe impl cxx::ExternType for TypeName {
103
127
/// type Id = cxx::type_id!("name::space::of::TypeName");
128
+ /// type Kind = cxx::Opaque;
104
129
/// # }
105
130
/// ```
106
131
type Id ;
107
132
108
- /// Either `kind ::Opaque` or `kind ::Trivial`. If in doubt, use
109
- /// `kind ::Opaque`.
133
+ /// Either `cxx ::Opaque` or `cxx ::Trivial`. If in doubt, use
134
+ /// `cxx ::Opaque`.
110
135
type Kind ;
111
136
}
112
137
0 commit comments