Skip to content

Commit 8ca2612

Browse files
authored
Merge pull request #388 from Niki4tap/ffi-dont-use-transmute
Discourage the use of transmute to construct invalid values
2 parents dd37e21 + 734ca5e commit 8ca2612

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/ffi.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,8 @@ Certain Rust types are defined to never be `null`. This includes references (`&T
659659
`&mut T`), boxes (`Box<T>`), and function pointers (`extern "abi" fn()`). When
660660
interfacing with C, pointers that might be `null` are often used, which would seem to
661661
require some messy `transmute`s and/or unsafe code to handle conversions to/from Rust types.
662-
However, the language provides a workaround.
662+
However, trying to construct/work with these invalid values **is undefined behavior**,
663+
so you should use the following workaround instead.
663664

664665
As a special case, an `enum` is eligible for the "nullable pointer optimization" if it contains
665666
exactly two variants, one of which contains no data and the other contains a field of one of the

0 commit comments

Comments
 (0)