I need to pass a raw pointer to a struct to C++ and then still want to use that box afterwards.
I found the into_raw function but it eats the box and I can't use that box any more in Rust. Is there a way to get a *const T
? I could get away with const_cast
on C++ side.
This is what I'm trying to do:
let handle_ptr = Box::<Handle>::into_raw(handle);
std::thread::spawn(move || {
let rt = tokio::runtime::Builder::new_current_thread()
.build()
.unwrap();
rt.block_on( handle.run() );
});
return handle_ptr
I know that this is unsafe but I don't know any other way to allow C++ code running on other thread to call a thread-safe method of a Rust object.
Box
around after you've passed a pointer to another thread would allow you to drop it while the other thread is still running. How do you avoid that problem? – trent ᶠᵒʳᵐᵉʳˡʸ ᶜˡ