Zig / C++ Interop

7 hours ago (tuple.app)

> When you want to embed a type, you need its definition, but you don’t actually need the full definition. You just need the size/alignment.

Aren't there ABI cases where e.g.

    struct foo { float X, Y; }

would be passed in e.g. fp registers whereas

    struct { char[8]; }

would not?

  • Yeah this is correct. You don't want to pass these values around "by value" but, you should be able to "embed them" and pass "pointers to them". It's a middle-ground between a completely opaque type which you would also pass around by address, but, with the added benefit that you allocate your own storage for it.

    I sort of mentioned this in the blog but this is good clarification.

    > if you want to pass a shared_ptr to Zig, you need to pass a pointer to the shared pointer

    For lore, I believe this GitHub thread is where I first learned about the how types of the same size/alignment can still have different ABIs :) https://github.com/microsoft/win32metadata/issues/623#issuec...

This idea about communicating size/alignment is actually something we're doing on the port of RediSearch to Rust [0]. We have an "opaque sized type" which is declared on the Rust-side, and has its size & alignment communicated to the C-side via cbindgen. The C-side has no visibility into the fields, but it can still allocate it on the stack.

It's a bit ugly due to cbindgen not supporting const-generic expressions and macro-expansion being nightly-only. It seems like this will be a generally useful mechanism to be able to use values which are not traditionally FFI-safe across FFI boundaries.

[0]: https://github.com/RediSearch/RediSearch/blob/cfd364fa2a47eb...