-
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
Many Win32-style enums would be better promoted to module-scope constants #727
Comments
Yes, I think that's a good idea. Enums from WinRT APIs tend to already provide scoped names, but Win32 enums are based on C-style enums, as you know. I could make them module-scoped but still preserve their type. |
Blocked on microsoft/win32metadata#442 and microsoft/win32metadata#443 |
If let barrier_type = D3D12_RESOURCE_BARRIER_TYPE::TRANSITION; That then allows use of Module-scope constants could simply reference those variants I guess? |
I was going to propose something like this originally, but there's a couple of drawbacks that it'd be interesting to get your take on. Firstly, it diverges from what these things are called in other tools or languages. For example, being able to search for D3D12_RESOURCE_BARRIER_TYPE_TRANSITION and get hits in C++ and Rust is pretty valuable. Tools such as PIX or the debug layer display the Win32 ABI names for these types, so as it diverges we'll be asking developers to make the translation in their head. The second issue, which is more of an implementation detail, is that there isn't always a mechanical way to reliably generate the simplified names. This can be addressed with enough determination :) |
Yes, searchability and portability is very valuable. Having already tried it, there are definitely a lot of edge cases that don't work well when you try to shorten the names. The metadata also doesn't deal with these constants consistently. Some are enums, some are constants, so in the end I'm also looking for more consistency in how this is presented in Rust. Scoping these also means that all of them must be generated as bindings. This is very problematic if you refer to an API that say uses |
At least in Rust there's
We're constantly fighting with that in the Vulkan bindings as well. Theoretically this should be solved on a metadata level, ie. enum variants/constants are not replicating the name of the surrounding type. Then the projection is free to make it an associated variant (concatenated with
That would be tricky. Logic has it that
I'm not sure about that. Consider the following in
OTOH having an enum with incomplete variants might feel inconsistent and confusing.
This is already the case in Perhaps on that note it's a good idea to split up generated code across files. Referencing Ash again, we have a separate file dedicated to displaying enums/bitflags. All in all I'm just grossed out at reading |
Yes, renaming stuff is hard and I ultimately went with preserving (#646) the original names everywhere (the win32 metadata project has taken the same originalist approach). The challenge is to make the bindings as rich/useful as possible while letting the developer limit/filter as much as possible to keep build time/complexity under control. That's why I'd like fine-grained filtering and not force huge chunks of code to be generated. The flip side is having pre-generated bindings that include everything (#432) but we're a ways from getting that to work. |
@kennykerr That's only casing, not stripping/deduplicating enum variant names, right? I agree on keeping the names similar, using original casing wherever possible. Tools like Unless I misunderstood you can still have fine-grained control over generated code if enum variants have the redundant part of the name stripped off? Again, I'm okay with module-level |
@damyanp All done - let me know what you think. 😉 |
Thanks! That makes the code so much nicer to read! It'd be nice if generating bindings for an enum also generated the bindings for all of the enum fields. For example, if I build "WNDCLASS_STYLES", I'd want to get "CS_HREDRAW" without having to list them manually. |
Yes, I considered that. The drawback is that (1) there's reduced symmetry with the Rust language between the |
@kennykerr Would it be possible for the generator to distinguish between However that probably goes straight into what you want to achieve here with keeping it simple and concise, for little gain. |
@kennykerr What qualifies as "scoped enum"? Just updated a local project to this and it seems // Windows.Win32.System.Kernel.EXCEPTION_DISPOSITION
public enum EXCEPTION_DISPOSITION
{
ExceptionContinueExecution,
ExceptionContinueSearch,
ExceptionNestedException,
ExceptionCollidedUnwind
} |
@MarijnS95 The |
@kennykerr Sure, just asking whether |
It's somewhat subjective. The constant names are unlikely to collide with anything so I'm hesitant to recommend it since that would make their use more verbose. I generally recommend |
@kennykerr Supposedly this enum lives in a Any comment on associated constants in #727 (comment) though? |
Yes, its an odd case hence subjective. Feel free to raise it as an issue on the win32metadata repo. I don't really mind either way, but I'm not in favor of removing prefixes as it deviates too much from existing docs and samples in other languages. Do you mean use |
I was thinking about having it both ways: a module-level constant, and a prefix-stripped variant aliasing that module-level constant. It's extra lines in
Yeah that wouldn't work in Rust - better not do that then especially if we're not generating associated constants as aliases anyway. |
One downside of this is that when generating I think when generating a scoped enum, all its module-scope constants should be generated as well. |
@roblabla yes that's a tradeoff I'm still wrestling with. @mikebattista are there plans to reduce the size of the
The trouble is that it is often the case that the particular error code isn't needed where the value is simply checked for success/failure and passed on to some |
@kennykerr we could move WIN32_ERROR and GetLastError to the Foundation namespace. Would that work? |
Hmm. While this is true for Another avenue that could fix it would be giving the ability to have a prefix on wildcard, e.g. |
In the latest metadata, VER_FLAGS is currently in a much more scoped Windows.Win32.System.SystemInformation namespace. The SystemServices and WindowsProgramming namespaces won't exist anymore once we're done refactoring. |
Another option is to treat the scoped enums as follows: if the build macro only includes specific constants like |
Thanks @mikebattista, I'm not sure whether moving it would help. I am curious what the difference is between |
There's nothing in Windows.Win32.System so I'm not sure what you're referring to there. |
I mean how do you decide whether something is in |
There is a need to put common shared types like HANDLE, HWND, string types, etc. in a shared namespace. Foundation is currently the plan for that to align with Windows.Foundation. The expectation would be that Foundation would always be included like Windows.Foundation for WinRT or System for .NET. Given that WIN32_ERROR and GetLastError are used with many APIs, it's a possible candidate for that Foundation namespace. The goal is to improve discovery of the basic things you need and minimize needing to add seemingly obscure namespace references just to use APIs in some other completely unrelated namespace. System.* is for system-level APIs that are mostly self-contained and not as commonly required as Foundation APIs would be: System.Memory, System.Power, System.Threading, etc. If you're using System.Memory you're not necessarily also using System.Power. If you think APIs like CreateFileMapping and VirtualAlloc should be in Foundation, that would be a separate discussion. |
Ah yes, I remember us chatting about a common namespace that others could depend on to keep circular references to a minimum. |
So yes, I'd err on the side of keeping it to a minimum and GetLastError doesn't seem like it needs to be there to reduce dependencies. |
Thanks. It's a work in progress so happy to iterate based on feedback on where APIs belong. For sure, though, I want to get rid of SystemServices and WindowsProgramming. |
Compare the usability of
WM_QUIT
withD3D12_RESOURCE_BARRIER_TYPE_TRANSITION
:The barrier type one is much more awkward to use. IMO it would be much more convenient to be able to write:
As all the naming conventions were picked with the assumption that no further qualification would be required.
Alternatively, the projection could try and figure out better names:
The text was updated successfully, but these errors were encountered: