2

bindgen has an option which enables the generation Rust enums (instead of constants) from C enums. However, the documentation states that it is capable of generating code that invokes undefined behavior under certain circumstances:

Use this with caution, creating this in unsafe code (including FFI) with an invalid value will invoke undefined behaviour. You may want to use the newtype enum style instead.

What exactly should I look out for to avoid this? Does "creating this in unsafe code" imply that a build.rs with a no unsafe code will catch these situations? The meaning of the above quote is unclear to me.

QuaternionsRock
  • 832
  • 7
  • 14
  • 1
    Pretty sure its just warning you that Rust enums *must* have a valid value, but since enums over FFI are just integral values, it could be invalid and thus cause undefined behavior. Using constants or a newtype pattern doesn't have that problem. – kmdreko Dec 08 '21 at 22:50

0 Answers0