I have C++ code that calls Rust code with data. It knows which object to send the data to. Here's an example of the Rust function that C++ calls back:
extern "C" fn on_open_vpn_receive(
instance: Box<OpenVpn>,
data: *mut c_uchar,
size: *mut size_t,
) -> u8
It receives the pointer as a Box
, so I created a function openvpn_set_rust_parent
that sets which object the C++ must call back. This object is a pointer to itself. I'm using Pin
so the Box
is not reallocated to somewhere else, making C++ call an invalid address.
impl OpenVpn {
pub fn new() -> Pin<Box<OpenVpn>> {
let instance = unsafe { interface::openvpn_new(profile.as_ptr()) };
let o = OpenVpn { instance: instance };
let p = Box::pin(o);
unsafe {
interface::openvpn_set_rust_parent(o.instance, p.as_ptr());
};
p
}
}
Signature:
pub fn openvpn_set_rust_parent(instance: *mut OpenVpnInstance, parent: *mut OpenVpn)
I don't know how to transform p
into *mut OpenVpn
to pass to C++. Is my idea ok? I think the usage of Pin
is good here, and I think this is a good way of calling the object from C++.