File tree Expand file tree Collapse file tree 1 file changed +11
-2
lines changed Expand file tree Collapse file tree 1 file changed +11
-2
lines changed Original file line number Diff line number Diff line change @@ -751,8 +751,17 @@ extern "C" {
751
751
```
752
752
753
753
By including a private field and no constructor,
754
- we create an opaque type that we can’t instantiate outside of this module.
755
- An empty array is both zero-size and compatible with ` #[repr(C)] ` .
754
+ we create an opaque type that we can't instantiate outside of this module.
755
+ (A struct with no field could be instantiated by anyone.)
756
+ We also want to use this type in FFI, so we have to add ` #[repr(C)] ` .
757
+ And to avoid warning around using ` () ` in FFI, we instead use an empty array,
758
+ which works just as well as an empty type but is FFI-compatible.
759
+
756
760
But because our ` Foo ` and ` Bar ` types are
757
761
different, we’ll get type safety between the two of them, so we cannot
758
762
accidentally pass a pointer to ` Foo ` to ` bar() ` .
763
+
764
+ Notice that it is a really bad idea to use an empty enum as FFI type.
765
+ The compiler relies on empty enums being uninhabited, so handling values of type
766
+ ` &Empty ` is a huge footgun and can lead to buggy program behavior (by triggering
767
+ undefined behavior).
You can’t perform that action at this time.
0 commit comments