I'm writing a library in Rust that has a C interface. C side must be able to create and destroy Rust objects (C side owns them and controls their lifetime).
I've managed to "leak" an object to C, but I'm not sure how to properly free it:
pub extern "C" fn create() -> *mut Foo { let obj = Foo; // oops, a bug let ptr = std::mem::transmute(&mut obj); // bad std::mem::forget(obj); // not needed return ptr; } pub extern "C" fn destroy(handle: *mut Foo) { // get Foo back and Drop it??? }
I'm not sure how can I turn pointer back to an object that Rust will call Drop on. Simply dereferencing *handle
doesn't compile.