Skip to content
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

Introduce DiplomatChar #366

Merged
merged 4 commits into from
Nov 21, 2023
Merged

Conversation

robertbastian
Copy link
Collaborator

@robertbastian robertbastian commented Nov 21, 2023

Fixes #318

@robertbastian robertbastian merged commit f545acf into rust-diplomat:main Nov 21, 2023
5 checks passed
@sffc
Copy link
Contributor

sffc commented Nov 22, 2023

Why did you do type DiplomatChar = u32 instead of struct DiplomatChar(pub u32) ?

@robertbastian
Copy link
Collaborator Author

This type goes on the ABI, and struct DiplomatChar(pub u32) is not generally ABI compatible with u32

@sffc
Copy link
Contributor

sffc commented Nov 22, 2023

Ok. It just struck me as slightly weird because type means that the things are 100% equivalent and can be swapped out at will, but in this case DiplomatChar actually has different semantics than char because we read it in a proc macro in a layer above Rust code compilation.

@robertbastian
Copy link
Collaborator Author

The typedef is basically a signal to the macro to treat this as a char (i.e. generate uchar32_t instead of uint32_t in C), and a signal to the Rust implementation that this is meant to be a char.

I could have actually done pub struct DiplomatChar(pub u32) and have the macro wrap and unwrap, but:

  • that's extra complexity in the macro
  • it's nice that Rust code can just pass this type to ...32 methods without having .0 everywhere
  • it's nice that Rust code can use primitive casts with it, i.e. a suffixed as DiplomatChar instead of DiplomatChar( at the beginning of the expression

@sffc
Copy link
Contributor

sffc commented Nov 23, 2023

Okay on point 1. Neutral on point 2. On point 3, I don't generally like as casts in Rust because they hide away footguns like converting signed to unsigned and converting larger ints to smaller ints. I find DiplomatChar( less error-prone (and it is even a few characters shorter).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide something other than char for FFI
3 participants