Skip to content

Commit eabf78c

Browse files
committed
Document the nullable pointer optimization in the FFI guide
Closes #8748
1 parent 5bcb761 commit eabf78c

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/doc/guide-ffi.md

+13
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,16 @@ NUL-terminated string for interoperability with C, you should use the `c_str::to
496496

497497
The standard library includes type aliases and function definitions for the C standard library in
498498
the `libc` module, and Rust links against `libc` and `libm` by default.
499+
500+
# The "nullable pointer optimization"
501+
502+
Certain types are defined to not be `null`. This includes references (`&T`,
503+
`&mut T`), owning pointers (`~T`), and function pointers (`extern "abi"
504+
fn()`). When interfacing with C, pointers that might be null are often used.
505+
As a special case, a generic `enum` that contains exactly two variants, one of
506+
which contains no data and the other containing a single field, is eligible
507+
for the "nullable pointer optimization". When such an enum is instantiated
508+
with one of the non-nullable types, it is represented as a single pointer,
509+
and the non-data variant is represented as the null pointer. So
510+
`Option<extern "C" fn(c_int) -> c_int>` is how one represents a nullable
511+
function pointer using the C ABI.

0 commit comments

Comments
 (0)