From 48ce20653a470f2d4734fb0ee4a89905da23b15c Mon Sep 17 00:00:00 2001 From: Alex Burka Date: Mon, 13 Jun 2016 15:05:22 -0400 Subject: [PATCH] generics-agnostic description --- src/doc/book/ffi.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/doc/book/ffi.md b/src/doc/book/ffi.md index 873d078f053..590c23e8929 100644 --- a/src/doc/book/ffi.md +++ b/src/doc/book/ffi.md @@ -578,19 +578,17 @@ interfacing with C, pointers that might be `null` are often used, which would se require some messy `transmute`s and/or unsafe code to handle conversions to/from Rust types. However, the language provides a workaround. -As a special case, an `enum` 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 listed above, it is represented as a single pointer, -and the non-data variant is represented as the null pointer. This is called an -"optimization", but unlike other optimizations it is guaranteed to apply to +As a special case, an `enum` is eligible for the "nullable pointer optimization" if it +contains exactly two variants, one of which contains no data and the other contains +a single field of one of the non-nullable types listed above. This means it is represented +as a single pointer, and the non-data variant is represented as the null pointer. This is +called an "optimization", but unlike other optimizations it is guaranteed to apply to eligible types. The most common type that takes advantage of the nullable pointer optimization is `Option`, where `None` corresponds to `null`. So `Option c_int>` is a correct way to represent a nullable function pointer using the C ABI (corresponding to the C type -`int (*)(int)`). (However, generics are not required to get the optimization. A simple -`enum NullableIntRef { Int(Box), NotInt }` is also represented as a single pointer.) +`int (*)(int)`). Here is an example: