Description
The paragraph in question (https://doc.rust-lang.org/book/ffi.html#the-nullable-pointer-optimization):
The "nullable pointer optimization"
Certain types are defined to not be
null
. This includes references (&T
,
&mut T
), boxes (Box<T>
), and function pointers (extern "abi" fn()
).
When interfacing with C, pointers that might be null are often used.
As a special case, a genericenum
that contains exactly two variants, one of
which contains no data and the other containing a single field, is eligible
for the "nullable pointer optimization". When such an enum is instantiated
with one of the non-nullable types, it is represented as a single pointer,
and the non-data variant is represented as the null pointer. So
Option<extern "C" fn(c_int) -> c_int>
is how one represents a nullable
function pointer using the C ABI.
While the paragraph does a good job at explaining when this optimizations happens (although it's missing the fact that the optimization can now look for deeply nested fields to use), it doesn't explicitly mention that None
represents the "NULL" value and that Some(function)
is how you create non-"NULL" values, nor does it have a code example.
What this can lead to is code that transmutes because it might not be entirely clear that there's a safe way to create values of that type.