I’m trying to wrap a 3rd-party C API and I hit a snag. The C API accepts a void* for user-specified data and associates that pointer with an object. The C API does not do anything with the pointer, no dereferencing or freeing, it just sets and gets the pointer. From the Rust side, I think I would want to use a
Box<Any> to set the pointer, and return references with a lifetime of the wrapped object to the downcasted data. I’d also want the data to be dropped correctly when the object is dropped.
I created a non-working playground demo of the interface I’m trying to achieve, modeling the C calls as unsafe functions that get/set static data. It produces the same error I’m getting in my real code, a panic when calling unwrap because the
get_foreign_ref method returns
None. I don’t understand why it returns
I’m hoping that someone here can send me down the right path. And if there is a better/alternative/idiomatic way to model this interface, please don’t hesitate to suggest it.