blob: 94a9037451ea4b46143a435425158e32697cc9b0 [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//!
David Tolnay278f6fc2020-09-01 16:16:57 -070060//! A runnable version of this example is provided under the *demo* directory of
61//! [https://github.com/dtolnay/cxx]. To try it out, run `cargo run` from that
62//! directory.
David Tolnay7db73692019-10-20 14:51:12 -040063//!
64//! ```no_run
65//! #[cxx::bridge]
66//! mod ffi {
67//! // Any shared structs, whose fields will be visible to both languages.
68//! struct SharedThing {
69//! z: i32,
70//! y: Box<ThingR>,
71//! x: UniquePtr<ThingC>,
72//! }
73//!
74//! extern "C" {
75//! // One or more headers with the matching C++ declarations. Our code
76//! // generators don't read it but it gets #include'd and used in static
77//! // assertions to ensure our picture of the FFI boundary is accurate.
David Tolnay278f6fc2020-09-01 16:16:57 -070078//! include!("demo/include/demo.h");
David Tolnay7db73692019-10-20 14:51:12 -040079//!
80//! // Zero or more opaque types which both languages can pass around but
81//! // only C++ can see the fields.
82//! type ThingC;
83//!
84//! // Functions implemented in C++.
85//! fn make_demo(appname: &str) -> UniquePtr<ThingC>;
David Tolnayb6a5f672020-04-17 15:04:56 -070086//! fn get_name(thing: &ThingC) -> &CxxString;
David Tolnay7db73692019-10-20 14:51:12 -040087//! fn do_thing(state: SharedThing);
88//! }
89//!
90//! extern "Rust" {
91//! // Zero or more opaque types which both languages can pass around but
92//! // only Rust can see the fields.
93//! type ThingR;
94//!
95//! // Functions implemented in Rust.
96//! fn print_r(r: &ThingR);
97//! }
98//! }
99//! #
100//! # pub struct ThingR(usize);
101//! #
102//! # fn print_r(r: &ThingR) {
103//! # println!("called back with r={}", r.0);
104//! # }
105//! #
106//! # fn main() {}
107//! ```
108//!
109//! Now we simply provide C++ definitions of all the things in the `extern "C"`
110//! block and Rust definitions of all the things in the `extern "Rust"` block,
111//! and get to call back and forth safely.
112//!
113//! Here are links to the complete set of source files involved in the demo:
114//!
David Tolnay278f6fc2020-09-01 16:16:57 -0700115//! - [demo/src/main.rs](https://github.com/dtolnay/cxx/blob/master/demo/src/main.rs)
116//! - [demo/build.rs](https://github.com/dtolnay/cxx/blob/master/demo/build.rs)
117//! - [demo/include/demo.h](https://github.com/dtolnay/cxx/blob/master/demo/include/demo.h)
118//! - [demo/src/demo.cc](https://github.com/dtolnay/cxx/blob/master/demo/src/demo.cc)
David Tolnay7db73692019-10-20 14:51:12 -0400119//!
120//! To look at the code generated in both languages for the example by the CXX
121//! code generators:
122//!
123//! ```console
124//! # run Rust code generator and print to stdout
125//! # (requires https://github.com/dtolnay/cargo-expand)
David Tolnay278f6fc2020-09-01 16:16:57 -0700126//! $ cargo expand --manifest-path demo/Cargo.toml
David Tolnay7db73692019-10-20 14:51:12 -0400127//!
128//! # run C++ code generator and print to stdout
David Tolnay278f6fc2020-09-01 16:16:57 -0700129//! $ cargo run --manifest-path gen/cmd/Cargo.toml -- demo/src/main.rs
David Tolnay7db73692019-10-20 14:51:12 -0400130//! ```
131//!
132//! <br>
133//!
134//! # Details
135//!
136//! As seen in the example, the language of the FFI boundary involves 3 kinds of
137//! items:
138//!
139//! - **Shared structs** &mdash; their fields are made visible to both
140//! languages. The definition written within cxx::bridge is the single source
141//! of truth.
142//!
143//! - **Opaque types** &mdash; their fields are secret from the other language.
144//! These cannot be passed across the FFI by value but only behind an
145//! indirection, such as a reference `&`, a Rust `Box`, or a `UniquePtr`. Can
146//! be a type alias for an arbitrarily complicated generic language-specific
147//! type depending on your use case.
148//!
149//! - **Functions** &mdash; implemented in either language, callable from the
150//! other language.
151//!
152//! Within the `extern "C"` part of the CXX bridge we list the types and
153//! functions for which C++ is the source of truth, as well as the header(s)
154//! that declare those APIs. In the future it's possible that this section could
155//! be generated bindgen-style from the headers but for now we need the
156//! signatures written out; static assertions will verify that they are
157//! accurate.
158//!
159//! Within the `extern "Rust"` part, we list types and functions for which Rust
160//! is the source of truth. These all implicitly refer to the `super` module,
161//! the parent module of the CXX bridge. You can think of the two items listed
162//! in the example above as being like `use super::ThingR` and `use
163//! super::print_r` except re-exported to C++. The parent module will either
164//! contain the definitions directly for simple things, or contain the relevant
165//! `use` statements to bring them into scope from elsewhere.
166//!
167//! Your function implementations themselves, whether in C++ or Rust, *do not*
168//! need to be defined as `extern "C"` ABI or no\_mangle. CXX will put in the
169//! right shims where necessary to make it all work.
170//!
171//! <br>
172//!
173//! # Comparison vs bindgen and cbindgen
174//!
175//! Notice that with CXX there is repetition of all the function signatures:
176//! they are typed out once where the implementation is defined (in C++ or Rust)
177//! and again inside the cxx::bridge module, though compile-time assertions
178//! guarantee these are kept in sync. This is different from [bindgen] and
179//! [cbindgen] where function signatures are typed by a human once and the tool
180//! consumes them in one language and emits them in the other language.
181//!
182//! [bindgen]: https://github.com/rust-lang/rust-bindgen
183//! [cbindgen]: https://github.com/eqrion/cbindgen/
184//!
185//! This is because CXX fills a somewhat different role. It is a lower level
186//! tool than bindgen or cbindgen in a sense; you can think of it as being a
187//! replacement for the concept of `extern "C"` signatures as we know them,
188//! rather than a replacement for a bindgen. It would be reasonable to build a
189//! higher level bindgen-like tool on top of CXX which consumes a C++ header
190//! and/or Rust module (and/or IDL like Thrift) as source of truth and generates
191//! the cxx::bridge, eliminating the repetition while leveraging the static
192//! analysis safety guarantees of CXX.
193//!
194//! But note in other ways CXX is higher level than the bindgens, with rich
195//! support for common standard library types. Frequently with bindgen when we
196//! are dealing with an idiomatic C++ API we would end up manually wrapping that
197//! API in C-style raw pointer functions, applying bindgen to get unsafe raw
198//! pointer Rust functions, and replicating the API again to expose those
199//! idiomatically in Rust. That's a much worse form of repetition because it is
200//! unsafe all the way through.
201//!
202//! By using a CXX bridge as the shared understanding between the languages,
203//! rather than `extern "C"` C-style signatures as the shared understanding,
204//! common FFI use cases become expressible using 100% safe code.
205//!
206//! It would also be reasonable to mix and match, using CXX bridge for the 95%
207//! of your FFI that is straightforward and doing the remaining few oddball
208//! signatures the old fashioned way with bindgen and cbindgen, if for some
209//! reason CXX's static restrictions get in the way. Please file an issue if you
210//! end up taking this approach so that we know what ways it would be worthwhile
211//! to make the tool more expressive.
212//!
213//! <br>
214//!
215//! # Cargo-based setup
216//!
217//! For builds that are orchestrated by Cargo, you will use a build script that
218//! runs CXX's C++ code generator and compiles the resulting C++ code along with
219//! any other C++ code for your crate.
220//!
221//! The canonical build script is as follows. The indicated line returns a
222//! [`cc::Build`] instance (from the usual widely used `cc` crate) on which you
223//! can set up any additional source files and compiler flags as normal.
224//!
225//! [`cc::Build`]: https://docs.rs/cc/1.0/cc/struct.Build.html
226//!
David Tolnaycc9ece52020-04-29 18:57:05 -0700227//! ```toml
228//! # Cargo.toml
229//!
230//! [build-dependencies]
David Tolnay63a43842020-04-29 18:54:07 -0700231//! cxx-build = "0.3"
David Tolnaycc9ece52020-04-29 18:57:05 -0700232//! ```
233//!
David Tolnay7db73692019-10-20 14:51:12 -0400234//! ```no_run
235//! // build.rs
236//!
237//! fn main() {
David Tolnayf8ed0732020-04-29 12:34:47 -0700238//! cxx_build::bridge("src/main.rs") // returns a cc::Build
David Tolnay278f6fc2020-09-01 16:16:57 -0700239//! .file("src/demo.cc")
Philip Craig7e14e2e2020-05-09 10:42:30 +0100240//! .flag_if_supported("-std=c++11")
David Tolnay7db73692019-10-20 14:51:12 -0400241//! .compile("cxxbridge-demo");
242//!
243//! println!("cargo:rerun-if-changed=src/main.rs");
David Tolnay278f6fc2020-09-01 16:16:57 -0700244//! println!("cargo:rerun-if-changed=src/demo.cc");
245//! println!("cargo:rerun-if-changed=include/demo.h");
David Tolnay7db73692019-10-20 14:51:12 -0400246//! }
247//! ```
248//!
249//! <br><br>
250//!
251//! # Non-Cargo setup
252//!
253//! For use in non-Cargo builds like Bazel or Buck, CXX provides an alternate
254//! way of invoking the C++ code generator as a standalone command line tool.
255//! The tool is packaged as the `cxxbridge-cmd` crate on crates.io or can be
David Tolnayd763f182020-03-12 00:50:19 -0700256//! built from the *cmd* directory of [https://github.com/dtolnay/cxx].
David Tolnay7db73692019-10-20 14:51:12 -0400257//!
258//! ```bash
259//! $ cargo install cxxbridge-cmd
260//!
261//! $ cxxbridge src/main.rs --header > path/to/mybridge.h
262//! $ cxxbridge src/main.rs > path/to/mybridge.cc
263//! ```
264//!
265//! <br>
266//!
267//! # Safety
268//!
269//! Be aware that the design of this library is intentionally restrictive and
270//! opinionated! It isn't a goal to be powerful enough to handle arbitrary
271//! signatures in either language. Instead this project is about carving out a
272//! reasonably expressive set of functionality about which we can make useful
273//! safety guarantees today and maybe extend over time. You may find that it
274//! takes some practice to use CXX bridge effectively as it won't work in all
275//! the ways that you are used to.
276//!
277//! Some of the considerations that go into ensuring safety are:
278//!
279//! - By design, our paired code generators work together to control both sides
280//! of the FFI boundary. Ordinarily in Rust writing your own `extern "C"`
281//! blocks is unsafe because the Rust compiler has no way to know whether the
282//! signatures you've written actually match the signatures implemented in the
283//! other language. With CXX we achieve that visibility and know what's on the
284//! other side.
285//!
286//! - Our static analysis detects and prevents passing types by value that
287//! shouldn't be passed by value from C++ to Rust, for example because they
288//! may contain internal pointers that would be screwed up by Rust's move
289//! behavior.
290//!
291//! - To many people's surprise, it is possible to have a struct in Rust and a
292//! struct in C++ with exactly the same layout / fields / alignment /
293//! everything, and still not the same ABI when passed by value. This is a
294//! longstanding bindgen bug that leads to segfaults in absolutely
295//! correct-looking code ([rust-lang/rust-bindgen#778]). CXX knows about this
296//! and can insert the necessary zero-cost workaround transparently where
297//! needed, so go ahead and pass your structs by value without worries. This
298//! is made possible by owning both sides of the boundary rather than just
299//! one.
300//!
301//! - Template instantiations: for example in order to expose a UniquePtr\<T\>
302//! type in Rust backed by a real C++ unique\_ptr, we have a way of using a
303//! Rust trait to connect the behavior back to the template instantiations
304//! performed by the other language.
305//!
306//! [rust-lang/rust-bindgen#778]: https://github.com/rust-lang/rust-bindgen/issues/778
307//!
308//! <br>
309//!
310//! # Builtin types
311//!
David Tolnay559fbb32020-03-17 23:32:20 -0700312//! In addition to all the primitive types (i32 &lt;=&gt; int32_t), the
313//! following common types may be used in the fields of shared structs and the
314//! arguments and returns of functions.
David Tolnay7db73692019-10-20 14:51:12 -0400315//!
316//! <table>
317//! <tr><th>name in Rust</th><th>name in C++</th><th>restrictions</th></tr>
David Tolnay750755e2020-03-01 13:04:08 -0800318//! <tr><td>String</td><td>rust::String</td><td></td></tr>
319//! <tr><td>&amp;str</td><td>rust::Str</td><td></td></tr>
David Tolnayefe81052020-04-14 16:28:24 -0700320//! <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 -0700321//! <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 -0800322//! <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 -0700323//! <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 -0700324//! <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 -0700325//! <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 -0700326//! <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 -0700327//! <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 -0400328//! </table>
329//!
David Tolnay736cbca2020-03-11 16:49:18 -0700330//! The C++ API of the `rust` namespace is defined by the *include/cxx.h* file
David Tolnayd763f182020-03-12 00:50:19 -0700331//! in [https://github.com/dtolnay/cxx]. You will need to include this header in
David Tolnay736cbca2020-03-11 16:49:18 -0700332//! your C++ code when working with those types.
David Tolnay7db73692019-10-20 14:51:12 -0400333//!
334//! The following types are intended to be supported "soon" but are just not
335//! implemented yet. I don't expect any of these to be hard to make work but
336//! it's a matter of designing a nice API for each in its non-native language.
337//!
338//! <table>
339//! <tr><th>name in Rust</th><th>name in C++</th></tr>
David Tolnay84f232e2020-01-08 12:22:56 -0800340//! <tr><td>BTreeMap&lt;K, V&gt;</td><td><sup><i>tbd</i></sup></td></tr>
341//! <tr><td>HashMap&lt;K, V&gt;</td><td><sup><i>tbd</i></sup></td></tr>
David Tolnay239d05f2020-03-13 01:36:50 -0700342//! <tr><td>Arc&lt;T&gt;</td><td><sup><i>tbd</i></sup></td></tr>
David Tolnay85487b02020-08-22 06:13:27 -0700343//! <tr><td>Option&lt;T&gt;</td><td><sup><i>tbd</i></sup></td></tr>
David Tolnay84f232e2020-01-08 12:22:56 -0800344//! <tr><td><sup><i>tbd</i></sup></td><td>std::map&lt;K, V&gt;</td></tr>
345//! <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 -0700346//! <tr><td><sup><i>tbd</i></sup></td><td>std::shared_ptr&lt;T&gt;</td></tr>
David Tolnay7db73692019-10-20 14:51:12 -0400347//! </table>
David Tolnayd763f182020-03-12 00:50:19 -0700348//!
349//! [https://github.com/dtolnay/cxx]: https://github.com/dtolnay/cxx
David Tolnay7db73692019-10-20 14:51:12 -0400350
David Tolnay9f1e3d72020-09-01 12:40:51 -0700351#![doc(html_root_url = "https://docs.rs/cxx/0.3.8")]
David Tolnay7db73692019-10-20 14:51:12 -0400352#![deny(improper_ctypes)]
David Tolnayc6ba2d22020-05-07 17:48:29 -0700353#![allow(non_camel_case_types)]
David Tolnay7db73692019-10-20 14:51:12 -0400354#![allow(
David Tolnay4bc98152020-04-09 23:24:01 -0700355 clippy::cognitive_complexity,
David Tolnayd2bb3da2020-03-18 17:19:39 -0700356 clippy::declare_interior_mutable_const,
David Tolnay30d214c2020-03-15 23:54:34 -0700357 clippy::inherent_to_string,
David Tolnay7db73692019-10-20 14:51:12 -0400358 clippy::large_enum_variant,
David Tolnayc79ba752020-05-05 10:13:00 -0700359 clippy::len_without_is_empty,
David Tolnay7db73692019-10-20 14:51:12 -0400360 clippy::missing_safety_doc,
361 clippy::module_inception,
David Tolnay30d214c2020-03-15 23:54:34 -0700362 clippy::needless_doctest_main,
David Tolnay7db73692019-10-20 14:51:12 -0400363 clippy::new_without_default,
364 clippy::or_fun_call,
365 clippy::ptr_arg,
366 clippy::toplevel_ref_arg,
David Tolnay7db73692019-10-20 14:51:12 -0400367 clippy::useless_let_if_seq
368)]
369
David Tolnay585a9fe2020-08-30 21:03:38 -0700370#[cfg(built_with_cargo)]
David Tolnayaf60e232020-01-24 15:22:09 -0800371extern crate link_cplusplus;
372
David Tolnayda5bd272020-03-16 21:53:22 -0700373#[macro_use]
David Tolnay3c90cd22020-04-30 07:28:21 -0700374mod macros;
David Tolnayda5bd272020-03-16 21:53:22 -0700375
David Tolnay7db73692019-10-20 14:51:12 -0400376mod cxx_string;
David Tolnaye90be1d2020-04-24 11:45:57 -0700377mod cxx_vector;
David Tolnayebef4a22020-03-17 15:33:47 -0700378mod exception;
David Tolnay5f9e8ca2020-05-07 17:21:05 -0700379mod extern_type;
David Tolnay75dca2e2020-03-25 20:17:52 -0700380mod function;
David Tolnay7db73692019-10-20 14:51:12 -0400381mod opaque;
David Tolnay486b6ec2020-03-17 01:19:57 -0700382mod result;
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700383mod rust_sliceu8;
David Tolnay7db73692019-10-20 14:51:12 -0400384mod rust_str;
385mod rust_string;
Myron Ahneba35cf2020-02-05 19:41:51 +0700386mod rust_vec;
David Tolnay7db73692019-10-20 14:51:12 -0400387mod unique_ptr;
388mod unwind;
389
David Tolnay3c90cd22020-04-30 07:28:21 -0700390#[cfg(not(no_export_symbols))]
391mod symbols;
392
David Tolnay7db73692019-10-20 14:51:12 -0400393pub use crate::cxx_string::CxxString;
David Tolnaye90be1d2020-04-24 11:45:57 -0700394pub use crate::cxx_vector::CxxVector;
David Tolnayebef4a22020-03-17 15:33:47 -0700395pub use crate::exception::Exception;
David Tolnay5f9e8ca2020-05-07 17:21:05 -0700396pub use crate::extern_type::ExternType;
David Tolnay7db73692019-10-20 14:51:12 -0400397pub use crate::unique_ptr::UniquePtr;
David Tolnay96c351b2020-05-08 13:06:18 -0700398pub use cxxbridge_macro::bridge;
David Tolnay094db3e2020-05-08 00:12:50 -0700399
400/// For use in impls of the `ExternType` trait. See [`ExternType`].
David Tolnay3cb75422020-08-27 23:40:43 -0700401///
402/// [`ExternType`]: trait.ExternType.html
David Tolnay094db3e2020-05-08 00:12:50 -0700403pub use cxxbridge_macro::type_id;
David Tolnay7db73692019-10-20 14:51:12 -0400404
David Tolnayf2eb3e72020-08-27 23:30:44 -0700405/// Synonym for `CxxString`.
406///
407/// To avoid confusion with Rust's standard library string you probably
408/// shouldn't import this type with `use`. Instead, write `cxx::String`, or
409/// import and use `CxxString`.
410pub type String = CxxString;
411
412/// Synonym for `CxxVector`.
413///
414/// To avoid confusion with Rust's standard library vector you probably
415/// shouldn't import this type with `use`. Instead, write `cxx::Vector<T>`, or
416/// import and use `CxxVector`.
417pub type Vector<T> = CxxVector<T>;
418
David Tolnay7db73692019-10-20 14:51:12 -0400419// Not public API.
420#[doc(hidden)]
421pub mod private {
David Tolnay67ba0b52020-04-24 12:43:39 -0700422 pub use crate::cxx_vector::VectorElement;
David Tolnay5f9e8ca2020-05-07 17:21:05 -0700423 pub use crate::extern_type::verify_extern_type;
David Tolnay75dca2e2020-03-25 20:17:52 -0700424 pub use crate::function::FatFunction;
David Tolnay7db73692019-10-20 14:51:12 -0400425 pub use crate::opaque::Opaque;
David Tolnay486b6ec2020-03-17 01:19:57 -0700426 pub use crate::result::{r#try, Result};
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700427 pub use crate::rust_sliceu8::RustSliceU8;
David Tolnay7db73692019-10-20 14:51:12 -0400428 pub use crate::rust_str::RustStr;
429 pub use crate::rust_string::RustString;
David Tolnay6c6b7e02020-04-24 11:42:59 -0700430 pub use crate::rust_vec::RustVec;
David Tolnay7db73692019-10-20 14:51:12 -0400431 pub use crate::unique_ptr::UniquePtrTarget;
432 pub use crate::unwind::catch_unwind;
433}
David Tolnayc6ba2d22020-05-07 17:48:29 -0700434
435macro_rules! chars {
436 ($($ch:ident)*) => {
437 $(
438 #[doc(hidden)]
439 pub enum $ch {}
440 )*
441 };
442}
443
444chars! {
445 _0 _1 _2 _3 _4 _5 _6 _7 _8 _9
446 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
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 __ // underscore
449}