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

windows-bindgen should generate more specific Result<T, E> types #3294

Open
kennykerr opened this issue Sep 25, 2024 · 4 comments
Open

windows-bindgen should generate more specific Result<T, E> types #3294

kennykerr opened this issue Sep 25, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@kennykerr
Copy link
Collaborator

Rather than folding all error types into the windows_result::Error type, APIs can more specifically return Result<T, HRESULT>, Result<T, NTSTATUS>, and so on. This would avoid the overhead that necessitated #3126 and allow simple pattern matching for error handling. The reasoning around the Windows-specific Result<T, Error> is that it additionally carries any IErrorInfo that may have been associated with the calling thread. This turns out to be used by very few APIs but can still be supported as needed through conversion.

@kennykerr kennykerr added the enhancement New feature or request label Sep 25, 2024
@kennykerr
Copy link
Collaborator Author

One thing to keep in mind is that Result only really works well when there is one, or mostly one, success code. So it works fine for BOOL and it mostly works fine for HRESULT if you don't rely on nasty codes like S_FALSE but it doesn't work well for NTSTATUS in many cases because NTSTATUS has numerous codes, like STATUS_TIMEOUT, that are technically successful but in practice represent some kind of soft failure that would be lost if transformed into a Result.

@kennykerr
Copy link
Collaborator Author

Along these lines, I've been slow to add WIN32_ERROR to the windows-result crate partly because its not a "real" type in the Windows SDK but it now shows up quite prominently in the Win32 metadata and moving it formally in to the windows-result crate will simplify and formalize some typeless conversions that already exist inside windows-result and helps to enable this issue's goal of allowing windows-bindgen to generate more specific Result types such as Result<T, WIN32_ERROR>.

@ChrisDenton
Copy link
Collaborator

My only concern would be how accurate the metadata for WIN32_ERROR is if it's being inferred rather than coming directly from the SDK.

@kennykerr
Copy link
Collaborator Author

Yes, the flip side is to avoid transforming these signatures automatically and let the caller use foo().ok() if they really want the transformation to Result. And this works today. 😊

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

No branches or pull requests

2 participants