Skip to content

Commit

Permalink
Merge pull request #388 from Niki4tap/ffi-dont-use-transmute
Browse files Browse the repository at this point in the history
Discourage the use of transmute to construct invalid values
  • Loading branch information
ehuss authored Dec 30, 2022
2 parents dd37e21 + 734ca5e commit 8ca2612
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,8 @@ Certain Rust types are defined to never 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, which would seem to
require some messy `transmute`s and/or unsafe code to handle conversions to/from Rust types.
However, the language provides a workaround.
However, trying to construct/work with these invalid values **is undefined behavior**,
so you should use the following workaround instead.

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 field of one of the
Expand Down

0 comments on commit 8ca2612

Please sign in to comment.