Skip to main content

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.199")]
11#![deny(missing_docs)]
12#![expect(dead_code)]
13#![cfg_attr(not(check_cfg), allow(unexpected_cfgs))]
14#![allow(
15    clippy::assert_is_empty,
16    clippy::cast_sign_loss,
17    clippy::default_trait_access,
18    clippy::elidable_lifetime_names,
19    clippy::enum_glob_use,
20    clippy::expl_impl_clone_on_copy, // https://github.com/rust-lang/rust-clippy/issues/15842
21    clippy::inherent_to_string,
22    clippy::items_after_statements,
23    clippy::match_bool,
24    clippy::match_like_matches_macro,
25    clippy::match_same_arms,
26    clippy::missing_errors_doc,
27    clippy::must_use_candidate,
28    clippy::needless_continue,
29    clippy::needless_lifetimes,
30    clippy::needless_pass_by_value,
31    clippy::nonminimal_bool,
32    clippy::precedence,
33    clippy::redundant_else,
34    clippy::ref_option,
35    clippy::similar_names,
36    clippy::single_match_else,
37    clippy::struct_excessive_bools,
38    clippy::struct_field_names,
39    clippy::too_many_arguments,
40    clippy::too_many_lines,
41    clippy::toplevel_ref_arg,
42    clippy::uninlined_format_args
43)]
44#![allow(unknown_lints, mismatched_lifetime_syntaxes)]
45
46mod bridge;
47mod error;
48mod syntax;
49
50pub use crate::bridge::include::{HEADER, Include};
51pub use crate::bridge::{CfgEvaluator, CfgResult, GeneratedCode, Opt};
52pub use crate::error::Error;
53pub use crate::syntax::IncludeKind;
54use proc_macro2::TokenStream;
55
56/// Generate C++ bindings code from a Rust token stream. This should be a Rust
57/// token stream which somewhere contains a `#[cxx::bridge] mod {}`.
58pub fn generate_header_and_cc(rust_source: TokenStream, opt: &Opt) -> Result<GeneratedCode, Error> {
59    let syntax = syn::parse2(rust_source)
60        .map_err(crate::bridge::Error::from)
61        .map_err(Error::from)?;
62    bridge::generate(syntax, opt).map_err(Error::from)
63}