Don’t recommend empty enums for opaque types

Fix https://github.com/rust-lang-nursery/nomicon/issues/29
pull/44/head
Simon Sapin 7 years ago committed by GitHub
parent cfb1f2d7e5
commit 36a7633208
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -737,11 +737,11 @@ void foo(struct Foo *arg);
void bar(struct Bar *arg);
```
To do this in Rust, lets create our own opaque types with `enum`:
To do this in Rust, lets create our own opaque types:
```rust
pub enum Foo {}
pub enum Bar {}
#[repr(C)] pub struct Foo { private: [u8; 0] }
#[repr(C)] pub struct Bar { private: [u8; 0] }
extern "C" {
pub fn foo(arg: *mut Foo);
@ -750,7 +750,9 @@ extern "C" {
# fn main() {}
```
By using an `enum` with no variants, we create an opaque type that we cant
instantiate, as it has no variants. But because our `Foo` and `Bar` types are
By including a private field and no constructor,
we create an opaque type that we cant instantiate outside of this module.
An empty array is both zero-size and compatible with `#[repr(C)]`.
But because our `Foo` and `Bar` types are
different, well get type safety between the two of them, so we cannot
accidentally pass a pointer to `Foo` to `bar()`.

Loading…
Cancel
Save