I am almost there.. I have refactored the C++ project I was originally working in. Rust is now the goal and I am wrapping the C++ dependency via CXX.. (And ConanRS)
but I am getting an error for what seems like basic translation
main.rs
use std::ffi::c_char;
#[cxx::bridge(namespace = "com::enserio")]
mod ffi {
unsafe extern "C++" {
include!("twsapi_grpc_server/include/twsapi-client.h");
include!("twsapi_grpc_server/include/AvailableAlgoParams.h");
include!("twsapi_grpc_server/include/AccountSummaryTags.h");
include!("twsapi_grpc_server/include/Utils.h");
type TWSApiClient;
fn new_twsapi_client() -> UniquePtr<TWSApiClient>;
// C++ VERSION = int32_t connect(const char * host, int32_t port, int32_t client_id = 0);
fn init_connect(&self, host: *const c_char , port: i32, client_id: i32) -> i32;
}
}
fn main() {
println!("Starting TWSApiClient connect()");
let client = ffi::new_twsapi_client();
client.connect("127.0.0.1", 4002, 10);
println!("Finished TWSApiClient connect()");
}
client.cpp
...
int32_t TWSApiClient::init_connect(const char *host, int32_t port, int32_t client_id)
{
...
The Error
error[cxxbridge]: pointer argument requires that the function be marked unsafe
┌─ src/main.rs:16:32
│
16 │ fn init_connect(&self, host: *const c_char , port: i32, client_id: i32) -> i32;
│ ^^^^^^^^^^^^^^^^^^^ pointer argument requires that the function be marked unsafe
If I take away the *const c_char and instead try
&c_char` .. it gives
/src/main.rs.cc:55:150: error: cannot convert ‘int32_t (com::enserio::TWSApiClient::*)(const char*, int32_t, int32_t)’ {aka ‘int (com::enserio::TWSApiClient::*)(const char*, int, int)’} to ‘int32_t (com::enserio::TWSApiClient::*)(const char&, int32_t, int32_t) const’ {aka ‘int (com::enserio::TWSApiClient::*)(const char&, int, int) const’} in initialization
cargo:warning= 55 | ::std::int32_t (::com::enserio::TWSApiClient::*init_connect$)(const char &, ::std::int32_t, ::std::int32_t) const = &::com::enserio::TWSApiClient::init_connect;
cargo:warning= | ^~~~~~~~~~~~
exit status: 1
are my integers looking okay? should I just change to a std::string
?