-
Notifications
You must be signed in to change notification settings - Fork 318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for #[repr(transparent)]
#185
Conversation
87444fc
to
9774fb5
Compare
One problem with that is that newtypes should be usually treated as different types, while with this approach they become interchangeable due to C/C++ side being just aliases, which can lead to mistakes... Not sure what would be a stable way to fix this. |
Oh, looks like I missed …
… and kinda misunderstood the semantics. What would be the appropriate/expected output for |
Well you can't coerce between them in Rust, but you can on C(++) side, and that's kinda the problem - you don't want This brings us to a problem of strong typedefs in C(++) which don't exist, and hence I'm not sure what would be the proper way to simulate them. Perhaps we should generate wrappers for each function instead of exposing it as-is, that would accept proper structures and unwrap them before passing to Rust? This would be somewhat dirty, but safe. Otherwise, we could accept primitives where they're passed by value, but pointers to proper structs where they're passed by reference, so at least some of the calls would be checked (and this would be safe since C(++) guarantee that pointer to struct is same as pointer to first member). Another alternative, albeit limited to only C++ and only primitives, would be to generate I'm not sure which option would be the best, so let's wait for feedback from others. |
Had a chat with @gankro and @emilio at this week's Rust Berlin Meetup about cbindgen and The consensus —as I understood it— was that the proposed implementation —albeit being inherently unsafe— is the only viable one given the lack of language support from C/C++. @nox, you seem to have been the main author of rust-lang/rfcs#1758, could you weight in on what cbindgen should do for cc @eddyb |
You can use the GCC extension |
@eddyb Ohh I like this idea if it works in Clang and MSVC too. |
@RReverser At least clang seems to support it. |
This doesn't sound good though, after all, the entire point is to make these types explicit wrappers. |
eb97aee
to
67f6110
Compare
It looks like this will need to be rebased. It's a bit sad that C/C++ doesn't support strong typedefs to fully get the benefit here. That being the case though, the approach this PR takes seems to be the best we'll be able to do. I'll do a review pass once it's been rebased. |
67f6110
to
0e2d977
Compare
Rebased. |
Thanks! |
#[repr(transparent)]
was stabilized today: