cxx_gen/
lib.rs

1//! The CXX code generator for constructing and compiling C++ code.
2//!
3//! This is intended as a mechanism for embedding the `cxx` crate into
4//! higher-level code generators. See [dtolnay/cxx#235] and
5//! [https://github.com/google/autocxx].
6//!
7//! [dtolnay/cxx#235]: https://github.com/dtolnay/cxx/issues/235
8//! [https://github.com/google/autocxx]: https://github.com/google/autocxx
9
10#![doc(html_root_url = "https://docs.rs/cxx-gen/0.7.187")]
11#![deny(missing_docs)]
12#![expect(dead_code)]
13#![cfg_attr(not(check_cfg), allow(unexpected_cfgs))]
14#![allow(
15    clippy::cast_sign_loss,
16    clippy::default_trait_access,
17    clippy::elidable_lifetime_names,
18    clippy::enum_glob_use,
19    clippy::expl_impl_clone_on_copy, // https://github.com/rust-lang/rust-clippy/issues/15842
20    clippy::inherent_to_string,
21    clippy::items_after_statements,
22    clippy::match_bool,
23    clippy::match_like_matches_macro,
24    clippy::match_same_arms,
25    clippy::missing_errors_doc,
26    clippy::must_use_candidate,
27    clippy::needless_continue,
28    clippy::needless_lifetimes,
29    clippy::needless_pass_by_value,
30    clippy::nonminimal_bool,
31    clippy::redundant_else,
32    clippy::ref_option,
33    clippy::similar_names,
34    clippy::single_match_else,
35    clippy::struct_excessive_bools,
36    clippy::struct_field_names,
37    clippy::too_many_arguments,
38    clippy::too_many_lines,
39    clippy::toplevel_ref_arg,
40    clippy::uninlined_format_args
41)]
42#![allow(unknown_lints, mismatched_lifetime_syntaxes)]
43
44mod error;
45mod gen;
46mod syntax;
47
48pub use crate::error::Error;
49pub use crate::gen::include::{Include, HEADER};
50pub use crate::gen::{CfgEvaluator, CfgResult, GeneratedCode, Opt};
51pub use crate::syntax::IncludeKind;
52use proc_macro2::TokenStream;
53
54/// Generate C++ bindings code from a Rust token stream. This should be a Rust
55/// token stream which somewhere contains a `#[cxx::bridge] mod {}`.
56pub fn generate_header_and_cc(rust_source: TokenStream, opt: &Opt) -> Result<GeneratedCode, Error> {
57    let syntax = syn::parse2(rust_source)
58        .map_err(crate::gen::Error::from)
59        .map_err(Error::from)?;
60    gen::generate(syntax, opt).map_err(Error::from)
61}