Best way to use FFI-unsafe types in functions?

Red IO redio.development at gmail.com
Thu Mar 28 07:37:27 UTC 2024


If I understand you correctly you want to access fields of a rust struct
using the Panama ffi.
The problem is that rust doesn't have an ABI (yet). This means rust makes
no guarantees about calling conventions or struct layouts.
There are 2 ways you can access fields under these conditions. Either you
mark your rust struct as #[repr(C)] or you export extern "C" getters and
setters.
Accessing rust structs without help on the rust side is impossible. This is
not a shortcoming in the Java ffi but in the rust ffi.

Great regards
RedIODev

On Thu, Mar 28, 2024, 07:42 Zea <zea_64 at proton.me> wrote:

> Hi,
> I've been really excited to use this API for accessing Rust libraries, but
> ran into a snag.
>
> Most Rust types have undefined layout, with the only properties known
> being size and alignment via const functions that I can export as symbols
> for the Java side to know ahead of time. From my understanding of the AMD64
> ABI, you need to know the exact struct definition because it can sometimes
> pass things in registers rather than the stack, so how would I go about
> passing types by value (at least semantically)?
>
> The best I've come up with is passing in/out pointers and memcpying those,
> but is this the best way? What are the JIT implications of this, if any?
>
> Thanks for helping!
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20240328/c977fcec/attachment.htm>


More information about the panama-dev mailing list