-
Notifications
You must be signed in to change notification settings - Fork 534
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
newtype vs type aliases for windows-sys? #1330
Comments
Yes, I had considered that for the native typedefs. The concern is that this would make it a little less type safe, but perhaps that is less of a concern when you're using the |
In some cases it's probably a small safety win to have the newtypes, but it may also lead to losses due to the extra transmutes or casts for pointer parameters. E.g. if a function takes The other thing I found a little dangerous is that there's no Related, it's curious that All of this would probably be addressed by staying true to the C typedefs. |
@kennykerr Thanks for looking into const-correctness! That mutability cast on string literals has discomforted me more than I'd like 😁 |
Interesting that the const attribute looks top-level but actually applies what the pointer points to. What does metadata look like for an API taking a const pointer to PCWSTR (for example, for a list of strings). |
The metadata has come a long way and is pretty rich. I'd recommend grabbing ILSpy here: https://github.com/icsharpcode/ILSpy/releases You can then load the winmd for the win32 metadata and poke around. You can find the metadata that windows-rs uses here: |
@jstarks I took that for a spin and I do like the results. The resulting code looks and feels a lot more like what it would in C/C++: https://github.com/microsoft/windows-samples-rs/compare/sys-handles?expand=1 |
Cool. What do you think about additionally changing HANDLE to |
The Win32 metadata defines it as |
Looks like there was some discussion several months ago: microsoft/win32metadata#21. I haven't had time to digest this yet, but it looks like perhaps it was changed based on feedback from .NET. |
OK, let's figure out what that's about and go from there. We can always override that for Rust but I'd like to have a better understanding first. |
Hmm, looks like Rust really really wants this to be a pointer: https://doc.rust-lang.org/std/os/windows/raw/type.HANDLE.html I may just override this for Rust and move on... It sure does make porting system code easier. |
And this is now so: #1332 |
#1550 adds first-class types for |
In windows-sys, could it make more sense to use type aliases instead of newtypes for things like
BOOL
andPWSTR
? It's kind of inconvenient to have to spell out the types at every callsite, especially without the conversion methods available from the full windows crate.The precedent in other low-level C wrapper crates (winapi, libc) seems to be to expose C typedefs as type aliases, so e.g.
BOOL
would just be an alias forc_int
ori32
, etc.The text was updated successfully, but these errors were encountered: