Repository: google/autocxx (rust)

Issue:
How to call rust function from c++ main()
I have found a sample here, but it use rust main recall c++ function. How can I call rust function directly in c++ main?
[cpp_calling_rust](https://github.com/google/autocxx/tree/main/examples/cpp_calling_rust)

I think I need a extra build system to compile c++, and link the rust staticlib/cdylib. But how to transfer the rust code and build staticlib/cdylib using autocxx and cargo, please help.

Pull request description:
Remove no mod found error
Fixes #1236.

Patch:
diff --git a/engine/src/lib.rs b/engine/src/lib.rs
index 78527443..4e8d4764 100644
--- a/engine/src/lib.rs
+++ b/engine/src/lib.rs
@@ -286,6 +286,16 @@ impl IncludeCppEngine {
         Ok(this)
     }
 
+    /// Used if we find that we're asked to auto-discover extern_rust_type and similar
+    /// but didn't have any include_cpp macro at all.
+    pub fn new_for_autodiscover() -> Self {
+        Self {
+            config: IncludeCppConfig::default(),
+            state: State::NotGenerated,
+            source_code: None,
+        }
+    }
+
     pub fn config_mut(&mut self) -> &mut IncludeCppConfig {
         assert!(
             matches!(self.state, State::NotGenerated),
diff --git a/engine/src/parse_file.rs b/engine/src/parse_file.rs
index d4574600..b5b05a0b 100644
--- a/engine/src/parse_file.rs
+++ b/engine/src/parse_file.rs
@@ -52,8 +52,6 @@ pub enum ParseError {
     /// mod name.
     #[error("there are two or more include_cpp! mods with the same mod name")]
     ConflictingModNames,
-    #[error("dynamic discovery was enabled but no mod was found")]
-    ZeroModsForDynamicDiscovery,
     #[error("dynamic discovery was enabled but multiple mods were found")]
     MultipleModsForDynamicDiscovery,
     #[error("a problem occurred while discovering C++ APIs used within the Rust: {0}")]
@@ -215,13 +213,18 @@ fn parse_file_contents(
     // We do not want to enter this 'if' block unless the above conditions are true,
     // since we may emit errors.
     if must_handle_discovered_things {
+        // If we have to handle discovered things but there was no include_cpp! macro,
+        // fake one.
+        if !results.iter().any(|seg| matches!(seg, Segment::Autocxx(_))) {
+            results.push(Segment::Autocxx(IncludeCppEngine::new_for_autodiscover()));
+        }
         let mut autocxx_seg_iterator = results.iter_mut().filter_map(|seg| match seg {
             Segment::Autocxx(engine) => Some(engine),
             _ => None,
         });
         let our_seg = autocxx_seg_iterator.next();
         match our_seg {
-            None => return Err(ParseError::ZeroModsForDynamicDiscovery),
+            None => panic!("We should have just added a fake mod but apparently didn't"),
             Some(engine) => {
                 engine
                     .config_mut()


Test patch:
diff --git a/integration-tests/tests/integration_test.rs b/integration-tests/tests/integration_test.rs
index 04be428a..4c129555 100644
--- a/integration-tests/tests/integration_test.rs
+++ b/integration-tests/tests/integration_test.rs
@@ -7246,6 +7246,32 @@ fn test_box_via_extern_rust() {
     );
 }
 
+#[test]
+fn test_box_via_extern_rust_no_include_cpp() {
+    let hdr = indoc! {"
+        #include <cxx.h>
+        struct Foo;
+        inline void take_box(rust::Box<Foo>) {
+        }
+    "};
+    do_run_test_manual(
+        "",
+        hdr,
+        quote! {
+            #[autocxx::extern_rust::extern_rust_type]
+            pub struct Foo {
+                a: String,
+            }
+
+            fn main() {
+            }
+        },
+        Some(Box::new(EnableAutodiscover)),
+        None,
+    )
+    .unwrap();
+}
+
 #[test]
 fn test_box_via_extern_rust_in_mod() {
     let hdr = indoc! {"