Best way to use FFI-unsafe types in functions?

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Fri Apr 5 08:58:39 UTC 2024


Related:

https://jornvernee.github.io/java/panama/rust/panama-ffi/2021/09/03/rust-panama-helloworld.html

Cheers
Maurizio

On 28/03/2024 07:37, Red IO wrote:
> 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/20240405/5a50a811/attachment.htm>


More information about the panama-dev mailing list