generics-agnostic description
This commit is contained in:
parent
032ea41e99
commit
48ce20653a
1 changed files with 6 additions and 8 deletions
|
@ -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.
|
require some messy `transmute`s and/or unsafe code to handle conversions to/from Rust types.
|
||||||
However, the language provides a workaround.
|
However, the language provides a workaround.
|
||||||
|
|
||||||
As a special case, an `enum` that contains exactly two variants, one of
|
As a special case, an `enum` is eligible for the "nullable pointer optimization" if it
|
||||||
which contains no data and the other containing a single field, is eligible
|
contains exactly two variants, one of which contains no data and the other contains
|
||||||
for the "nullable pointer optimization". When such an enum is instantiated
|
a single field of one of the non-nullable types listed above. This means it is represented
|
||||||
with one of the non-nullable types listed above, it is represented as a single pointer,
|
as a single pointer, and the non-data variant is represented as the null pointer. This is
|
||||||
and the non-data variant is represented as the null pointer. This is called an
|
called an "optimization", but unlike other optimizations it is guaranteed to apply to
|
||||||
"optimization", but unlike other optimizations it is guaranteed to apply to
|
|
||||||
eligible types.
|
eligible types.
|
||||||
|
|
||||||
The most common type that takes advantage of the nullable pointer optimization is `Option<T>`,
|
The most common type that takes advantage of the nullable pointer optimization is `Option<T>`,
|
||||||
where `None` corresponds to `null`. So `Option<extern "C" fn(c_int) -> c_int>` is a correct way
|
where `None` corresponds to `null`. So `Option<extern "C" fn(c_int) -> c_int>` is a correct way
|
||||||
to represent a nullable function pointer using the C ABI (corresponding to the C type
|
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
|
`int (*)(int)`).
|
||||||
`enum NullableIntRef { Int(Box<i32>), NotInt }` is also represented as a single pointer.)
|
|
||||||
|
|
||||||
Here is an example:
|
Here is an example:
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue