1. 26
    1. 4

      This looks like great work. Worth checking out the crabi ABI experiments in the compiler for a stable ABI that’s a bit more Rusty than the C ABI (if you haven’t already). I think it would be opt-in for FFI though and not the default, so might not help with the goal of gaining access to all of crates.io.

      1. 4

        This generally looks sound, but you could have alignment problems. The C struct contains an array of bytes so it has 1-byte alignment, but the Rust vector struct it holds presumably contains pointers so it would be 8-byte aligned. Not all CPUs care about unaligned access anymore, but some do, and it can sometimes hurt performance even if it doesn’t crash.

        C++ has an alignas attribute to force stricter alignment; not sure about C.

          1. 2

            Thank you! Fixed and updated. Gave you a thanks in the article too =)

          2. 3

            Maybe it’s not that useful for a code generator, but for manually written bindings it’s handy that Rust promises C-compatible ABI for a bunch of things, like references and even Box, so for example you don’t need transmute from *mut T to &mut T, just make it an argument on the C-exported function.