Rust to Rust reference across dylib FFI boundary

First of all, here's a quick example demonstrating what I'm trying to accomplish: Rust Playground

I need to pass a Rust reference to a function declared in a shared library (written in Rust). The library keeps the reference, but does not do anything with it other than passing it back to the calling process at a later time as "context" in static callback.

Compiling the code I get the following:

warning: `extern` block uses type `Box<dyn Trait>`, which is not FFI-safe

I understand that this stems from the fact that my Context structure contains Rust-types that are not FFI safe (in this case a Box).

Ideally, I'd want to get rid of this warning, but I'm wondering if this might pose problems if I'm just passing the reference from Rust <--> Rust?

What would be the idiomatic Rust way of doing this?

You can cast your reference to a *mut (), then convert it back when you need it again.

No, there aren't any issues with this.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.