From 734ca5e3f7baf356c3e01ad9e6032317d85772de Mon Sep 17 00:00:00 2001 From: Niki4tap Date: Sat, 17 Dec 2022 14:26:28 +0300 Subject: [PATCH] Don't try to work with invalid values on non-null types, always do what nomicon says --- src/ffi.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ffi.md b/src/ffi.md index 9b0ff98..8d1a882 100644 --- a/src/ffi.md +++ b/src/ffi.md @@ -659,7 +659,8 @@ Certain Rust types are defined to never be `null`. This includes references (`&T `&mut T`), boxes (`Box`), 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