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.
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.
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.
This looks like great work. Worth checking out the
crabiABI 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 ofcrates.io.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
alignasattribute to force stricter alignment; not sure about C.C has
_Alignassince C11: https://en.cppreference.com/w/c/language/_AlignasThank you! Fixed and updated. Gave you a thanks in the article too =)
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 Tto&mut T, just make it an argument on the C-exported function.