blob: a7073388c7a680a9363f0a78338dfb81394f8da8 [file] [log] [blame]
David Tolnaye3daefb2020-05-11 12:52:53 -07001//! [![github]](https://github.com/dtolnay/cxx) [![crates-io]](https://crates.io/crates/cxx) [![docs-rs]](https://docs.rs/cxx)
2//!
3//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
4//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
5//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K
David Tolnay864ab8c2020-03-29 22:25:40 -07006//!
7//! <br>
8//!
David Tolnay7db73692019-10-20 14:51:12 -04009//! This library provides a **safe** mechanism for calling C++ code from Rust
10//! and Rust code from C++, not subject to the many ways that things can go
11//! wrong when using bindgen or cbindgen to generate unsafe C-style bindings.
12//!
David Tolnayccd39752020-01-08 09:33:51 -080013//! This doesn't change the fact that 100% of C++ code is unsafe. When auditing
14//! a project, you would be on the hook for auditing all the unsafe Rust code
15//! and *all* the C++ code. The core safety claim under this new model is that
16//! auditing just the C++ side would be sufficient to catch all problems, i.e.
17//! the Rust side can be 100% safe.
18//!
David Tolnay7db73692019-10-20 14:51:12 -040019//! <br>
20//!
David Tolnaycf223852020-05-11 20:50:59 -070021//! *Compiler support: requires rustc 1.42+ and c++11 or newer*<br>
David Tolnay0e078f72020-05-11 20:49:59 -070022//! *[Release notes](https://github.com/dtolnay/cxx/releases)*
David Tolnay7db73692019-10-20 14:51:12 -040023//!
24//! <br>
25//!
26//! # Overview
27//!
28//! The idea is that we define the signatures of both sides of our FFI boundary
29//! embedded together in one Rust module (the next section shows an example).
30//! From this, CXX receives a complete picture of the boundary to perform static
31//! analyses against the types and function signatures to uphold both Rust's and
32//! C++'s invariants and requirements.
33//!
34//! If everything checks out statically, then CXX uses a pair of code generators
35//! to emit the relevant `extern "C"` signatures on both sides together with any
36//! necessary static assertions for later in the build process to verify
37//! correctness. On the Rust side this code generator is simply an attribute
38//! procedural macro. On the C++ side it can be a small Cargo build script if
39//! your build is managed by Cargo, or for other build systems like Bazel or
40//! Buck we provide a command line tool which generates the header and source
41//! file and should be easy to integrate.
42//!
43//! The resulting FFI bridge operates at zero or negligible overhead, i.e. no
44//! copying, no serialization, no memory allocation, no runtime checks needed.
45//!
46//! The FFI signatures are able to use native types from whichever side they
47//! please, such as Rust's `String` or C++'s `std::string`, Rust's `Box` or
48//! C++'s `std::unique_ptr`, Rust's `Vec` or C++'s `std::vector`, etc in any
49//! combination. CXX guarantees an ABI-compatible signature that both sides
50//! understand, based on builtin bindings for key standard library types to
51//! expose an idiomatic API on those types to the other language. For example
52//! when manipulating a C++ string from Rust, its `len()` method becomes a call
53//! of the `size()` member function defined by C++; when manipulation a Rust
54//! string from C++, its `size()` member function calls Rust's `len()`.
55//!
56//! <br>
57//!
58//! # Example
59//!
60//! A runnable version of this example is provided under the *demo-rs* directory
David Tolnayd763f182020-03-12 00:50:19 -070061//! of [https://github.com/dtolnay/cxx] (with the C++ side of the implementation
David Tolnay7db73692019-10-20 14:51:12 -040062//! in the *demo-cxx* directory). To try it out, jump into demo-rs and run
63//! `cargo run`.
64//!
65//! ```no_run
66//! #[cxx::bridge]
67//! mod ffi {
68//! // Any shared structs, whose fields will be visible to both languages.
69//! struct SharedThing {
70//! z: i32,
71//! y: Box<ThingR>,
72//! x: UniquePtr<ThingC>,
73//! }
74//!
75//! extern "C" {
76//! // One or more headers with the matching C++ declarations. Our code
77//! // generators don't read it but it gets #include'd and used in static
78//! // assertions to ensure our picture of the FFI boundary is accurate.
79//! include!("demo-cxx/demo.h");
80//!
81//! // Zero or more opaque types which both languages can pass around but
82//! // only C++ can see the fields.
83//! type ThingC;
84//!
85//! // Functions implemented in C++.
86//! fn make_demo(appname: &str) -> UniquePtr<ThingC>;
David Tolnayb6a5f672020-04-17 15:04:56 -070087//! fn get_name(thing: &ThingC) -> &CxxString;
David Tolnay7db73692019-10-20 14:51:12 -040088//! fn do_thing(state: SharedThing);
89//! }
90//!
91//! extern "Rust" {
92//! // Zero or more opaque types which both languages can pass around but
93//! // only Rust can see the fields.
94//! type ThingR;
95//!
96//! // Functions implemented in Rust.
97//! fn print_r(r: &ThingR);
98//! }
99//! }
100//! #
101//! # pub struct ThingR(usize);
102//! #
103//! # fn print_r(r: &ThingR) {
104//! # println!("called back with r={}", r.0);
105//! # }
106//! #
107//! # fn main() {}
108//! ```
109//!
110//! Now we simply provide C++ definitions of all the things in the `extern "C"`
111//! block and Rust definitions of all the things in the `extern "Rust"` block,
112//! and get to call back and forth safely.
113//!
114//! Here are links to the complete set of source files involved in the demo:
115//!
116//! - [demo-rs/src/main.rs](https://github.com/dtolnay/cxx/blob/master/demo-rs/src/main.rs)
117//! - [demo-rs/build.rs](https://github.com/dtolnay/cxx/blob/master/demo-rs/build.rs)
118//! - [demo-cxx/demo.h](https://github.com/dtolnay/cxx/blob/master/demo-cxx/demo.h)
119//! - [demo-cxx/demo.cc](https://github.com/dtolnay/cxx/blob/master/demo-cxx/demo.cc)
120//!
121//! To look at the code generated in both languages for the example by the CXX
122//! code generators:
123//!
124//! ```console
125//! # run Rust code generator and print to stdout
126//! # (requires https://github.com/dtolnay/cargo-expand)
127//! $ cargo expand --manifest-path demo-rs/Cargo.toml
128//!
129//! # run C++ code generator and print to stdout
David Tolnay32439da2020-05-09 09:59:40 -0700130//! $ cargo run --manifest-path gen/cmd/Cargo.toml -- demo-rs/src/main.rs
David Tolnay7db73692019-10-20 14:51:12 -0400131//! ```
132//!
133//! <br>
134//!
135//! # Details
136//!
137//! As seen in the example, the language of the FFI boundary involves 3 kinds of
138//! items:
139//!
140//! - **Shared structs** &mdash; their fields are made visible to both
141//! languages. The definition written within cxx::bridge is the single source
142//! of truth.
143//!
144//! - **Opaque types** &mdash; their fields are secret from the other language.
145//! These cannot be passed across the FFI by value but only behind an
146//! indirection, such as a reference `&`, a Rust `Box`, or a `UniquePtr`. Can
147//! be a type alias for an arbitrarily complicated generic language-specific
148//! type depending on your use case.
149//!
150//! - **Functions** &mdash; implemented in either language, callable from the
151//! other language.
152//!
153//! Within the `extern "C"` part of the CXX bridge we list the types and
154//! functions for which C++ is the source of truth, as well as the header(s)
155//! that declare those APIs. In the future it's possible that this section could
156//! be generated bindgen-style from the headers but for now we need the
157//! signatures written out; static assertions will verify that they are
158//! accurate.
159//!
160//! Within the `extern "Rust"` part, we list types and functions for which Rust
161//! is the source of truth. These all implicitly refer to the `super` module,
162//! the parent module of the CXX bridge. You can think of the two items listed
163//! in the example above as being like `use super::ThingR` and `use
164//! super::print_r` except re-exported to C++. The parent module will either
165//! contain the definitions directly for simple things, or contain the relevant
166//! `use` statements to bring them into scope from elsewhere.
167//!
168//! Your function implementations themselves, whether in C++ or Rust, *do not*
169//! need to be defined as `extern "C"` ABI or no\_mangle. CXX will put in the
170//! right shims where necessary to make it all work.
171//!
172//! <br>
173//!
174//! # Comparison vs bindgen and cbindgen
175//!
176//! Notice that with CXX there is repetition of all the function signatures:
177//! they are typed out once where the implementation is defined (in C++ or Rust)
178//! and again inside the cxx::bridge module, though compile-time assertions
179//! guarantee these are kept in sync. This is different from [bindgen] and
180//! [cbindgen] where function signatures are typed by a human once and the tool
181//! consumes them in one language and emits them in the other language.
182//!
183//! [bindgen]: https://github.com/rust-lang/rust-bindgen
184//! [cbindgen]: https://github.com/eqrion/cbindgen/
185//!
186//! This is because CXX fills a somewhat different role. It is a lower level
187//! tool than bindgen or cbindgen in a sense; you can think of it as being a
188//! replacement for the concept of `extern "C"` signatures as we know them,
189//! rather than a replacement for a bindgen. It would be reasonable to build a
190//! higher level bindgen-like tool on top of CXX which consumes a C++ header
191//! and/or Rust module (and/or IDL like Thrift) as source of truth and generates
192//! the cxx::bridge, eliminating the repetition while leveraging the static
193//! analysis safety guarantees of CXX.
194//!
195//! But note in other ways CXX is higher level than the bindgens, with rich
196//! support for common standard library types. Frequently with bindgen when we
197//! are dealing with an idiomatic C++ API we would end up manually wrapping that
198//! API in C-style raw pointer functions, applying bindgen to get unsafe raw
199//! pointer Rust functions, and replicating the API again to expose those
200//! idiomatically in Rust. That's a much worse form of repetition because it is
201//! unsafe all the way through.
202//!
203//! By using a CXX bridge as the shared understanding between the languages,
204//! rather than `extern "C"` C-style signatures as the shared understanding,
205//! common FFI use cases become expressible using 100% safe code.
206//!
207//! It would also be reasonable to mix and match, using CXX bridge for the 95%
208//! of your FFI that is straightforward and doing the remaining few oddball
209//! signatures the old fashioned way with bindgen and cbindgen, if for some
210//! reason CXX's static restrictions get in the way. Please file an issue if you
211//! end up taking this approach so that we know what ways it would be worthwhile
212//! to make the tool more expressive.
213//!
214//! <br>
215//!
216//! # Cargo-based setup
217//!
218//! For builds that are orchestrated by Cargo, you will use a build script that
219//! runs CXX's C++ code generator and compiles the resulting C++ code along with
220//! any other C++ code for your crate.
221//!
222//! The canonical build script is as follows. The indicated line returns a
223//! [`cc::Build`] instance (from the usual widely used `cc` crate) on which you
224//! can set up any additional source files and compiler flags as normal.
225//!
226//! [`cc::Build`]: https://docs.rs/cc/1.0/cc/struct.Build.html
227//!
David Tolnaycc9ece52020-04-29 18:57:05 -0700228//! ```toml
229//! # Cargo.toml
230//!
231//! [build-dependencies]
David Tolnay63a43842020-04-29 18:54:07 -0700232//! cxx-build = "0.3"
David Tolnaycc9ece52020-04-29 18:57:05 -0700233//! ```
234//!
David Tolnay7db73692019-10-20 14:51:12 -0400235//! ```no_run
236//! // build.rs
237//!
238//! fn main() {
David Tolnayf8ed0732020-04-29 12:34:47 -0700239//! cxx_build::bridge("src/main.rs") // returns a cc::Build
David Tolnay7db73692019-10-20 14:51:12 -0400240//! .file("../demo-cxx/demo.cc")
Philip Craig7e14e2e2020-05-09 10:42:30 +0100241//! .flag_if_supported("-std=c++11")
David Tolnay7db73692019-10-20 14:51:12 -0400242//! .compile("cxxbridge-demo");
243//!
244//! println!("cargo:rerun-if-changed=src/main.rs");
245//! println!("cargo:rerun-if-changed=../demo-cxx/demo.h");
246//! println!("cargo:rerun-if-changed=../demo-cxx/demo.cc");
247//! }
248//! ```
249//!
250//! <br><br>
251//!
252//! # Non-Cargo setup
253//!
254//! For use in non-Cargo builds like Bazel or Buck, CXX provides an alternate
255//! way of invoking the C++ code generator as a standalone command line tool.
256//! The tool is packaged as the `cxxbridge-cmd` crate on crates.io or can be
David Tolnayd763f182020-03-12 00:50:19 -0700257//! built from the *cmd* directory of [https://github.com/dtolnay/cxx].
David Tolnay7db73692019-10-20 14:51:12 -0400258//!
259//! ```bash
260//! $ cargo install cxxbridge-cmd
261//!
262//! $ cxxbridge src/main.rs --header > path/to/mybridge.h
263//! $ cxxbridge src/main.rs > path/to/mybridge.cc
264//! ```
265//!
266//! <br>
267//!
268//! # Safety
269//!
270//! Be aware that the design of this library is intentionally restrictive and
271//! opinionated! It isn't a goal to be powerful enough to handle arbitrary
272//! signatures in either language. Instead this project is about carving out a
273//! reasonably expressive set of functionality about which we can make useful
274//! safety guarantees today and maybe extend over time. You may find that it
275//! takes some practice to use CXX bridge effectively as it won't work in all
276//! the ways that you are used to.
277//!
278//! Some of the considerations that go into ensuring safety are:
279//!
280//! - By design, our paired code generators work together to control both sides
281//! of the FFI boundary. Ordinarily in Rust writing your own `extern "C"`
282//! blocks is unsafe because the Rust compiler has no way to know whether the
283//! signatures you've written actually match the signatures implemented in the
284//! other language. With CXX we achieve that visibility and know what's on the
285//! other side.
286//!
287//! - Our static analysis detects and prevents passing types by value that
288//! shouldn't be passed by value from C++ to Rust, for example because they
289//! may contain internal pointers that would be screwed up by Rust's move
290//! behavior.
291//!
292//! - To many people's surprise, it is possible to have a struct in Rust and a
293//! struct in C++ with exactly the same layout / fields / alignment /
294//! everything, and still not the same ABI when passed by value. This is a
295//! longstanding bindgen bug that leads to segfaults in absolutely
296//! correct-looking code ([rust-lang/rust-bindgen#778]). CXX knows about this
297//! and can insert the necessary zero-cost workaround transparently where
298//! needed, so go ahead and pass your structs by value without worries. This
299//! is made possible by owning both sides of the boundary rather than just
300//! one.
301//!
302//! - Template instantiations: for example in order to expose a UniquePtr\<T\>
303//! type in Rust backed by a real C++ unique\_ptr, we have a way of using a
304//! Rust trait to connect the behavior back to the template instantiations
305//! performed by the other language.
306//!
307//! [rust-lang/rust-bindgen#778]: https://github.com/rust-lang/rust-bindgen/issues/778
308//!
309//! <br>
310//!
311//! # Builtin types
312//!
David Tolnay559fbb32020-03-17 23:32:20 -0700313//! In addition to all the primitive types (i32 &lt;=&gt; int32_t), the
314//! following common types may be used in the fields of shared structs and the
315//! arguments and returns of functions.
David Tolnay7db73692019-10-20 14:51:12 -0400316//!
317//! <table>
318//! <tr><th>name in Rust</th><th>name in C++</th><th>restrictions</th></tr>
David Tolnay750755e2020-03-01 13:04:08 -0800319//! <tr><td>String</td><td>rust::String</td><td></td></tr>
320//! <tr><td>&amp;str</td><td>rust::Str</td><td></td></tr>
David Tolnayefe81052020-04-14 16:28:24 -0700321//! <tr><td>&amp;[u8]</td><td>rust::Slice&lt;uint8_t&gt;</td><td><sup><i>arbitrary &amp;[T] not implemented yet</i></sup></td></tr>
David Tolnay7a9b1302020-04-24 16:15:22 -0700322//! <tr><td><a href="struct.CxxString.html">CxxString</a></td><td>std::string</td><td><sup><i>cannot be passed by value</i></sup></td></tr>
David Tolnay750755e2020-03-01 13:04:08 -0800323//! <tr><td>Box&lt;T&gt;</td><td>rust::Box&lt;T&gt;</td><td><sup><i>cannot hold opaque C++ type</i></sup></td></tr>
David Tolnay7a9b1302020-04-24 16:15:22 -0700324//! <tr><td><a href="struct.UniquePtr.html">UniquePtr&lt;T&gt;</a></td><td>std::unique_ptr&lt;T&gt;</td><td><sup><i>cannot hold opaque Rust type</i></sup></td></tr>
David Tolnay347c3d02020-04-24 16:14:07 -0700325//! <tr><td>Vec&lt;T&gt;</td><td>rust::Vec&lt;T&gt;</td><td><sup><i>cannot hold opaque C++ type</i></sup></td></tr>
David Tolnaya8df0942020-04-25 19:08:40 -0700326//! <tr><td><a href="struct.CxxVector.html">CxxVector&lt;T&gt;</a></td><td>std::vector&lt;T&gt;</td><td><sup><i>cannot be passed by value, cannot hold opaque Rust type</i></sup></td></tr>
David Tolnayaddc7482020-03-29 22:19:44 -0700327//! <tr><td>fn(T, U) -&gt; V</td><td>rust::Fn&lt;V(T, U)&gt;</td><td><sup><i>only passing from Rust to C++ is implemented so far</i></sup></td></tr>
David Tolnay31b5aad2020-04-10 19:35:47 -0700328//! <tr><td>Result&lt;T&gt;</td><td>throw/catch</td><td><sup><i>allowed as return type only</i></sup></td></tr>
David Tolnay7db73692019-10-20 14:51:12 -0400329//! </table>
330//!
David Tolnay736cbca2020-03-11 16:49:18 -0700331//! The C++ API of the `rust` namespace is defined by the *include/cxx.h* file
David Tolnayd763f182020-03-12 00:50:19 -0700332//! in [https://github.com/dtolnay/cxx]. You will need to include this header in
David Tolnay736cbca2020-03-11 16:49:18 -0700333//! your C++ code when working with those types.
David Tolnay7db73692019-10-20 14:51:12 -0400334//!
335//! The following types are intended to be supported "soon" but are just not
336//! implemented yet. I don't expect any of these to be hard to make work but
337//! it's a matter of designing a nice API for each in its non-native language.
338//!
339//! <table>
340//! <tr><th>name in Rust</th><th>name in C++</th></tr>
David Tolnay84f232e2020-01-08 12:22:56 -0800341//! <tr><td>BTreeMap&lt;K, V&gt;</td><td><sup><i>tbd</i></sup></td></tr>
342//! <tr><td>HashMap&lt;K, V&gt;</td><td><sup><i>tbd</i></sup></td></tr>
David Tolnay239d05f2020-03-13 01:36:50 -0700343//! <tr><td>Arc&lt;T&gt;</td><td><sup><i>tbd</i></sup></td></tr>
David Tolnay85487b02020-08-22 06:13:27 -0700344//! <tr><td>Option&lt;T&gt;</td><td><sup><i>tbd</i></sup></td></tr>
David Tolnay84f232e2020-01-08 12:22:56 -0800345//! <tr><td><sup><i>tbd</i></sup></td><td>std::map&lt;K, V&gt;</td></tr>
346//! <tr><td><sup><i>tbd</i></sup></td><td>std::unordered_map&lt;K, V&gt;</td></tr>
David Tolnay239d05f2020-03-13 01:36:50 -0700347//! <tr><td><sup><i>tbd</i></sup></td><td>std::shared_ptr&lt;T&gt;</td></tr>
David Tolnay7db73692019-10-20 14:51:12 -0400348//! </table>
David Tolnayd763f182020-03-12 00:50:19 -0700349//!
350//! [https://github.com/dtolnay/cxx]: https://github.com/dtolnay/cxx
David Tolnay7db73692019-10-20 14:51:12 -0400351
David Tolnay9f1e3d72020-09-01 12:40:51 -0700352#![doc(html_root_url = "https://docs.rs/cxx/0.3.8")]
David Tolnay7db73692019-10-20 14:51:12 -0400353#![deny(improper_ctypes)]
David Tolnayc6ba2d22020-05-07 17:48:29 -0700354#![allow(non_camel_case_types)]
David Tolnay7db73692019-10-20 14:51:12 -0400355#![allow(
David Tolnay4bc98152020-04-09 23:24:01 -0700356 clippy::cognitive_complexity,
David Tolnayd2bb3da2020-03-18 17:19:39 -0700357 clippy::declare_interior_mutable_const,
David Tolnay30d214c2020-03-15 23:54:34 -0700358 clippy::inherent_to_string,
David Tolnay7db73692019-10-20 14:51:12 -0400359 clippy::large_enum_variant,
David Tolnayc79ba752020-05-05 10:13:00 -0700360 clippy::len_without_is_empty,
David Tolnay7db73692019-10-20 14:51:12 -0400361 clippy::missing_safety_doc,
362 clippy::module_inception,
David Tolnay30d214c2020-03-15 23:54:34 -0700363 clippy::needless_doctest_main,
David Tolnay7db73692019-10-20 14:51:12 -0400364 clippy::new_without_default,
365 clippy::or_fun_call,
366 clippy::ptr_arg,
367 clippy::toplevel_ref_arg,
David Tolnay7db73692019-10-20 14:51:12 -0400368 clippy::useless_let_if_seq
369)]
370
David Tolnay585a9fe2020-08-30 21:03:38 -0700371#[cfg(built_with_cargo)]
David Tolnayaf60e232020-01-24 15:22:09 -0800372extern crate link_cplusplus;
373
David Tolnayda5bd272020-03-16 21:53:22 -0700374#[macro_use]
David Tolnay3c90cd22020-04-30 07:28:21 -0700375mod macros;
David Tolnayda5bd272020-03-16 21:53:22 -0700376
David Tolnay7db73692019-10-20 14:51:12 -0400377mod cxx_string;
David Tolnaye90be1d2020-04-24 11:45:57 -0700378mod cxx_vector;
David Tolnayebef4a22020-03-17 15:33:47 -0700379mod exception;
David Tolnay5f9e8ca2020-05-07 17:21:05 -0700380mod extern_type;
David Tolnay75dca2e2020-03-25 20:17:52 -0700381mod function;
David Tolnay7db73692019-10-20 14:51:12 -0400382mod opaque;
David Tolnay486b6ec2020-03-17 01:19:57 -0700383mod result;
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700384mod rust_sliceu8;
David Tolnay7db73692019-10-20 14:51:12 -0400385mod rust_str;
386mod rust_string;
Myron Ahneba35cf2020-02-05 19:41:51 +0700387mod rust_vec;
David Tolnay7db73692019-10-20 14:51:12 -0400388mod unique_ptr;
389mod unwind;
390
David Tolnay3c90cd22020-04-30 07:28:21 -0700391#[cfg(not(no_export_symbols))]
392mod symbols;
393
David Tolnay7db73692019-10-20 14:51:12 -0400394pub use crate::cxx_string::CxxString;
David Tolnaye90be1d2020-04-24 11:45:57 -0700395pub use crate::cxx_vector::CxxVector;
David Tolnayebef4a22020-03-17 15:33:47 -0700396pub use crate::exception::Exception;
David Tolnay5f9e8ca2020-05-07 17:21:05 -0700397pub use crate::extern_type::ExternType;
David Tolnay7db73692019-10-20 14:51:12 -0400398pub use crate::unique_ptr::UniquePtr;
David Tolnay96c351b2020-05-08 13:06:18 -0700399pub use cxxbridge_macro::bridge;
David Tolnay094db3e2020-05-08 00:12:50 -0700400
401/// For use in impls of the `ExternType` trait. See [`ExternType`].
David Tolnay3cb75422020-08-27 23:40:43 -0700402///
403/// [`ExternType`]: trait.ExternType.html
David Tolnay094db3e2020-05-08 00:12:50 -0700404pub use cxxbridge_macro::type_id;
David Tolnay7db73692019-10-20 14:51:12 -0400405
David Tolnayf2eb3e72020-08-27 23:30:44 -0700406/// Synonym for `CxxString`.
407///
408/// To avoid confusion with Rust's standard library string you probably
409/// shouldn't import this type with `use`. Instead, write `cxx::String`, or
410/// import and use `CxxString`.
411pub type String = CxxString;
412
413/// Synonym for `CxxVector`.
414///
415/// To avoid confusion with Rust's standard library vector you probably
416/// shouldn't import this type with `use`. Instead, write `cxx::Vector<T>`, or
417/// import and use `CxxVector`.
418pub type Vector<T> = CxxVector<T>;
419
David Tolnay7db73692019-10-20 14:51:12 -0400420// Not public API.
421#[doc(hidden)]
422pub mod private {
David Tolnay67ba0b52020-04-24 12:43:39 -0700423 pub use crate::cxx_vector::VectorElement;
David Tolnay5f9e8ca2020-05-07 17:21:05 -0700424 pub use crate::extern_type::verify_extern_type;
David Tolnay75dca2e2020-03-25 20:17:52 -0700425 pub use crate::function::FatFunction;
David Tolnay7db73692019-10-20 14:51:12 -0400426 pub use crate::opaque::Opaque;
David Tolnay486b6ec2020-03-17 01:19:57 -0700427 pub use crate::result::{r#try, Result};
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700428 pub use crate::rust_sliceu8::RustSliceU8;
David Tolnay7db73692019-10-20 14:51:12 -0400429 pub use crate::rust_str::RustStr;
430 pub use crate::rust_string::RustString;
David Tolnay6c6b7e02020-04-24 11:42:59 -0700431 pub use crate::rust_vec::RustVec;
David Tolnay7db73692019-10-20 14:51:12 -0400432 pub use crate::unique_ptr::UniquePtrTarget;
433 pub use crate::unwind::catch_unwind;
434}
David Tolnayc6ba2d22020-05-07 17:48:29 -0700435
436macro_rules! chars {
437 ($($ch:ident)*) => {
438 $(
439 #[doc(hidden)]
440 pub enum $ch {}
441 )*
442 };
443}
444
445chars! {
446 _0 _1 _2 _3 _4 _5 _6 _7 _8 _9
447 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
448 a b c d e f g h i j k l m n o p q r s t u v w x y z
449 __ // underscore
450}