Skip to content

Commit b883eca

Browse files
committed
Write up the upstream FFI issue a little.
1 parent cf26f37 commit b883eca

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

active/0000-undefined-struct-layout.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,31 @@ cause surprising bugs and crashes. There is already a lint for FFI use
7272
of `enum`s without a `#[repr(...)]` attribute, so this can be extended
7373
to include structs.
7474

75+
Having an unspecified (or otherwise non-C-compatible) layout by
76+
default makes interfacing with C slightly harder. A particularly bad
77+
case is passing to C a struct from an upstream library that doesn't
78+
have a `repr(C)` attribute. This situation seems relatively similar to
79+
one where an upstream library type is missing an implementation of a
80+
core trait e.g. `Hash` if one wishes to use it as a hashmap key.
81+
82+
It is slightly better if structs had a specified-but-C-incompatible
83+
layout, *and* one has control over the C interface, because then one
84+
can manually arrange the fields in the C definition to match the Rust
85+
order.
86+
87+
That said, this scenario requires:
88+
89+
- Needing to pass a Rust struct into C/FFI code, where that FFI code
90+
actually needs to use things from the struct, rather than just pass
91+
it through, e.g., back into a Rust callback.
92+
- The Rust struct is defined upstream & out of your control, and not
93+
intended for use with C code.
94+
- The C/FFI code is designed by someone other than that vendor, or
95+
otherwise not designed for use with the Rust struct (or else it is a
96+
bug in the vendor's library that the Rust struct can't be sanely
97+
passed to C).
98+
99+
75100
# Detailed design
76101

77102
A struct declaration like

0 commit comments

Comments
 (0)