You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: text/0000-result_ffi_guarantees.md
+12-1Lines changed: 12 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -28,7 +28,7 @@ I'm not sure how to write a "guide" portion of this that's any simpler than the
28
28
When either of these two `core` types:
29
29
30
30
*`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.
32
32
33
33
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.
34
34
@@ -53,6 +53,17 @@ Is combined with a non-zero or non-null type (see the chart), the combination ha
53
53
54
54
* While `fn()` is listed just once in the above table, this rule applies to all `fn` types (regardless of ABI, arguments, and return type).
55
55
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.
0 commit comments