0

I'm making a Dart library that uses FFI to interact with Rust code and I have some questions.

I couldn't find any official information about native interop behavior and I've been steering only by other examples and Dart SDK code.

For instance, Dart FFI doesn't map bool types directly, instead, it uses Int8. I also don't know what happens if the sizes of the types don't match. Is there any documentation that explains these "rules"?

pak3nuh
  • 11
  • 4
  • From what I recall, FFI uses the standard calling convention for C, which are the "rules". If the sizes of the types don't match, you could be accessing memory you're not supposed to, which will lead to a segmentation fault. – Christopher Moore Jun 17 '21 at 16:51
  • Is that documented somewhere? – pak3nuh Jun 17 '21 at 17:48
  • Which part of my comment? – Christopher Moore Jun 17 '21 at 17:53
  • All. The calling convention is C and the sizes. Don't get me wrong, I just want to read more about this, but I can't find that much official/verified information. – pak3nuh Jun 17 '21 at 18:13
  • There's plenty of resources about the C standards for calling convention with a quick google search. Microsoft uses the [following](https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-160) for x64. Once you somewhat understand the calling convention, it should be clear how using incorrectly sized arguments can create issues. – Christopher Moore Jun 17 '21 at 18:44
  • In rust, `bool`s are simply a `u8` that is guaranteed to be 0 or 1. Treating them as a single byte value is perfectly fine, so long as you don't break that invariant. – Aiden4 Jun 17 '21 at 20:09

1 Answers1

0

The boolean type is supported on the master branch. It is available on dev from 2.15.0-233.0.dev, in the 2.15 beta, and will be part of the 2.15 stable release.

As for sizes. You could try generating a C header file from your Rust API and then using package:ffigen to generate the Dart bindings.

Daco Harkes
  • 296
  • 3
  • 13