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.184")]
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::inherent_to_string,
20    clippy::items_after_statements,
21    clippy::match_bool,
22    clippy::match_like_matches_macro,
23    clippy::match_same_arms,
24    clippy::missing_errors_doc,
25    clippy::must_use_candidate,
26    clippy::needless_continue,
27    clippy::needless_lifetimes,
28    clippy::needless_pass_by_value,
29    clippy::nonminimal_bool,
30    clippy::redundant_else,
31    clippy::ref_option,
32    clippy::similar_names,
33    clippy::single_match_else,
34    clippy::struct_excessive_bools,
35    clippy::struct_field_names,
36    clippy::too_many_arguments,
37    clippy::too_many_lines,
38    clippy::toplevel_ref_arg,
39    clippy::uninlined_format_args
40)]
41#![allow(unknown_lints, mismatched_lifetime_syntaxes)]
42
43mod error;
44mod gen;
45mod syntax;
46
47pub use crate::error::Error;
48pub use crate::gen::include::{Include, HEADER};
49pub use crate::gen::{CfgEvaluator, CfgResult, GeneratedCode, Opt};
50pub use crate::syntax::IncludeKind;
51use proc_macro2::TokenStream;
52
53/// Generate C++ bindings code from a Rust token stream. This should be a Rust
54/// token stream which somewhere contains a `#[cxx::bridge] mod {}`.
55pub fn generate_header_and_cc(rust_source: TokenStream, opt: &Opt) -> Result<GeneratedCode, Error> {
56    let syntax = syn::parse2(rust_source)
57        .map_err(crate::gen::Error::from)
58        .map_err(Error::from)?;
59    gen::generate(syntax, opt).map_err(Error::from)
60}