Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upDocument the correct FFI equivalent to a C opaque struct #27303
Comments
This comment has been minimized.
This comment has been minimized.
|
It looks like the canonical answer here is a But @crumblingstatue wants to newtype this for type safety, and it isn't clear whether newtyping this structure ( A few places in the rust repo, some external docs, and some projects (including Servo) use I can see few options here:
Whatever the answer, it should be documented in the book's FFI section as well as in the doc comment on |
This comment has been minimized.
This comment has been minimized.
|
Note that the only reason I personally would not recommend |
This comment has been minimized.
This comment has been minimized.
|
The reason we don't accept |
This comment has been minimized.
This comment has been minimized.
This is exactly what I've done in my projects, although I haven't tried with the newly rewritten improper-ctypes lint. |
nukep
referenced this issue
Jul 28, 2015
Closed
Replace zero-size structs in FFI with empty enums #442
steveklabnik
added
the
A-docs
label
Jul 29, 2015
This comment has been minimized.
This comment has been minimized.
|
Servo uses |
This comment has been minimized.
This comment has been minimized.
|
@Ms2ger Doesn't the ffi-safe lint trigger on that too? |
This comment has been minimized.
This comment has been minimized.
|
@bluss raw pointers to Sized types are always allowed in FFI signatures, AFAIK. |
steveklabnik
added a commit
to steveklabnik/rust
that referenced
this issue
Aug 5, 2015
steveklabnik
added a commit
to steveklabnik/rust
that referenced
this issue
Aug 6, 2015
vhbit
added a commit
to vhbit/lmdb-rs
that referenced
this issue
Aug 9, 2015
Manishearth
added a commit
to Manishearth/rust
that referenced
this issue
Aug 11, 2015
Manishearth
added a commit
to Manishearth/rust
that referenced
this issue
Aug 11, 2015
Manishearth
added a commit
to Manishearth/rust
that referenced
this issue
Aug 11, 2015
bors
closed this
in
#27542
Aug 11, 2015
crumblingstatue
added a commit
to jeremyletang/rust-sfml
that referenced
this issue
Aug 16, 2015
This comment has been minimized.
This comment has been minimized.
goertzenator
commented
Aug 18, 2015
|
A consequence of using enums for opaque structs is that the generated documentation will move from the Structs section to the Enums section. This makes the documentation confusing and misleading especially when there are also "normal" structs and enums. |
goertzenator
added a commit
to rusterlium/erlang_nif-sys
that referenced
this issue
Aug 18, 2015
felixc
added a commit
to felixc/gexiv2-sys
that referenced
this issue
Sep 14, 2015
This comment has been minimized.
This comment has been minimized.
|
Wouldn't |
This comment has been minimized.
This comment has been minimized.
|
Oh yeah, I guess given the advice above that It might be nice to have |
This comment has been minimized.
This comment has been minimized.
|
Maybe not Void, but an empty enum named Opaque or something like that. There is a void crate with a Void type that implements various traits already. We don't want such a thing. |
This comment has been minimized.
This comment has been minimized.
|
What about this to leave it named a "struct" in rustdoc:
or perhaps
Note that I'm avoiding |
This comment has been minimized.
This comment has been minimized.
Nah, this isn't a better idea, as this type is actually constructible (although it takes a smidge more work). |
crumblingstatue commentedJul 26, 2015
With recent changes to the
improper_ctypeslint, it now rejects#[repr(C)] struct Foo;for representing a C opaque struct.On the #rust IRC channel, I have asked what is the proper way to represent a C opaque struct, but no definitive answer was reached.
Some of the answers were:
struct Foo;is incorrect.struct Foo;is fine, but it can break certain LLVM optimizations.c_voidinstead. This isn't satisfactory when it is desirable to represent a C opaque struct as a distinct type in your API.c_void. Someone said this might not be correct either.#repr(C) enum Foo {}. It doesn't currently allow this.This should be properly researched and then documented in the official documentation, so users know what to use for representing C opaque struct types in Rust when interfacing with C code.
Some relevant issues: