I am writing a library in Rust that has a C interface. The C side should be able to create and destroy Rust objects (the C side owns them and controls their lifespan).
I managed to βskipβ the object in C, but I'm not sure how to release it correctly:
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 I can turn the pointer back to the object that Rust will call Drop. Just dereferencing *handle does not compile.
source share