Skip to content

Commit f647e37

Browse files
committed
restrict the ZST rules, provide further explanation.
1 parent 4b5a891 commit f647e37

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

text/0000-result_ffi_guarantees.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ I'm not sure how to write a "guide" portion of this that's any simpler than the
2828
When either of these two `core` types:
2929

3030
* `Option<T>`
31-
* `Result<T, E>` where either `T` or `E` are a zero-sized type with alignment 1 (a "1-ZST").
31+
* `Result<T, E>` where either `T` or `E` are a zero-sized type with alignment 1 (a "1-ZST") and either no fields (eg: `()` or `struct Foo;`) or with `repr(transparent)` if there are fields.
3232

3333
Is combined with a non-zero or non-null type (see the chart), the combination has the same layout (size and alignment) and the same ABI as the primitive form of the data.
3434

@@ -53,6 +53,17 @@ Is combined with a non-zero or non-null type (see the chart), the combination ha
5353

5454
* While `fn()` is listed just once in the above table, this rule applies to all `fn` types (regardless of ABI, arguments, and return type).
5555

56+
For simplicity the table listing only uses `Result<_, ()>`, but swapping the `T` and `E` types, or using `Option<T>` is also valid.
57+
What changes are the implied semantics:
58+
* `Result<NonZeroI32, ()>` is "a non-zero success value"
59+
* `Result<(), NonZeroI32>` is "a non-zero error value"
60+
* `Option<NonZeroI32>` is "a non-zero value is present"
61+
* they all pass over FFI as if they were an `i32`.
62+
63+
Which type you should use with a particular FFI function signature still depends on the function.
64+
Rust can't solve that part for you.
65+
However, once you've decided on the type you want to use, the compiler's normal type checks can guide you everywhere else in the code.
66+
5667
# Drawbacks
5768
[drawbacks]: #drawbacks
5869

0 commit comments

Comments
 (0)