Can I use cxx.rs types in cbindgen C callback?

Hello Rustaceans:

I'm trying to embed a small rust lib into my C++ codebase, I use cargo-c(it use cbindgen I think ?) to create some simple C api of rust, rust side api like this:

#[no_mangle]
pub extern "C" write_result_to_cpp_callback(
    cpp_callback : Option<extern "C" fn(  i64 )>)

(it's based on callback instead of returning value because the rust part actually runs a tokio runtime to do the io jobs, and any call to rust is non-blocking)

but now I need to pass some complex structure instead of simple i64, seems that cxx.rs makes this simpler, but the function pointer part doc said that

Passing a function pointer from C++ to Rust is not implemented yet, only from Rust to an extern "C++" function is implemented.

I'd like to know if I can use cxx.rs generated type within my cbindgen C API ? for example

// for cxx.rs
#[cxx::bridge]
mod ffi{
   struct MyStruct{
      my_vec: Vec<String>
 }
}

// for cbindgen
#[no_mangle]
pub extern "C" write_result_to_cpp_callback(
    cpp_callback : Option<extern "C" fn( *const ffi::MyStruct )>)

and if such usage is OK, will the cbindgen exported api also using the same API as cxx.rs for some container , for example the following code will also work ?

// will this Vec have compatible memory layout as I use 
// the cxx.rs generated C++ code ?

#[no_mangle]
pub extern "C" write_result_to_cpp_callback(
    cpp_callback : Option<extern "C" fn( *const Vec<ffi::MyStruct> )>)

// or I need to also wrap the Vec as a cxx.rs struct member 
// just like following ?

mod ffi{
   struct WrapVec{
      my_vec: Vec<MyStruct>
 }
}

#[no_mangle]
pub extern "C" write_result_to_cpp_callback(
    cpp_callback : Option<extern "C" fn( *const  WrapVec)>)


Thanks for advise

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.