Does anyone know what kind of assembly is generated when using std::option in Rust when make a similar function call?
void f(std::variant<int, char>)
would be f(x: IntOrChar)
roughly in Rust, I'm too lazy to make the types _exactly_ the same, but https://godbolt.org/g/pZde2A is roughly it.Actually, https://godbolt.org/g/1vcMeG might be even better to compare; I'm not an expert, but looks like the only difference is in the size.
last one, adding inline(never) so you can see the calls in main: https://godbolt.org/g/cVDjQH
movabs rdi, 21474836480
call example::f1@PLT
vs mov edi, 5
pop rbp
jmp example::f2@PLT
so, yup.In other words, the compiler can turn an Option into a null-pointer convention by reasoning from first principles.
(I think this optimization works for enums in general, and is somehow related to the `nonzero::NonZero` type, but I could be wrong about that.)
Example (look at the calls to f1 and f2 in example::main): https://godbolt.org/g/SSs6X2