Skip to content

Commit 296ea5c

Browse files
committed
Documenting ExternType::Kind.
1 parent 25f1e2e commit 296ea5c

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/extern_type.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
/// ## Integrating with bindgen-generated types
5555
///
5656
/// 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.
5858
///
5959
/// By writing the unsafe `ExternType` impl, the programmer asserts that the C++
6060
/// namespace and type name given in the type id refers to a C++ type that is
@@ -69,10 +69,11 @@
6969
/// # pub struct StringPiece([usize; 2]);
7070
/// # }
7171
///
72-
/// use cxx::{type_id, ExternType};
72+
/// use cxx::{type_id, ExternType, Opaque};
7373
///
7474
/// unsafe impl ExternType for folly_sys::StringPiece {
7575
/// type Id = type_id!("folly::StringPiece");
76+
/// type Kind = Opaque;
7677
/// }
7778
///
7879
/// #[cxx::bridge(namespace = folly)]
@@ -92,6 +93,29 @@
9293
/// #
9394
/// # fn main() {}
9495
/// ```
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`.
95119
pub unsafe trait ExternType {
96120
/// A type-level representation of the type's C++ namespace and type name.
97121
///
@@ -101,12 +125,13 @@ pub unsafe trait ExternType {
101125
/// # struct TypeName;
102126
/// # unsafe impl cxx::ExternType for TypeName {
103127
/// type Id = cxx::type_id!("name::space::of::TypeName");
128+
/// type Kind = cxx::Opaque;
104129
/// # }
105130
/// ```
106131
type Id;
107132

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`.
110135
type Kind;
111136
}
112137

0 commit comments

Comments
 (0)