File tree 1 file changed +7
-5
lines changed 1 file changed +7
-5
lines changed Original file line number Diff line number Diff line change @@ -737,11 +737,11 @@ void foo(struct Foo *arg);
737
737
void bar(struct Bar * arg);
738
738
```
739
739
740
- To do this in Rust, let’s create our own opaque types with `enum` :
740
+ To do this in Rust, let’s create our own opaque types:
741
741
742
742
```rust
743
- pub enum Foo {}
744
- pub enum Bar {}
743
+ #[repr(C)] pub struct Foo { private: [u8; 0] }
744
+ #[repr(C)] pub struct Bar { private: [u8; 0] }
745
745
746
746
extern "C" {
747
747
pub fn foo(arg: *mut Foo);
@@ -750,7 +750,9 @@ extern "C" {
750
750
# fn main() {}
751
751
```
752
752
753
- By using an ` enum ` with no variants, we create an opaque type that we can’t
754
- instantiate, as it has no variants. But because our ` Foo ` and ` Bar ` types are
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)] ` .
756
+ But because our ` Foo ` and ` Bar ` types are
755
757
different, we’ll get type safety between the two of them, so we cannot
756
758
accidentally pass a pointer to ` Foo ` to ` bar() ` .
You can’t perform that action at this time.
0 commit comments