-
Notifications
You must be signed in to change notification settings - Fork 568
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
Print HRESULT description when formatting Error::Hr #718
Conversation
…ription is available.
9aa9ca7
to
31fe87a
Compare
The FormatMessageW API expects a LPWSTR* disguising as a LPWSTR, in the case that the FORMAT_MESSAGE_ALLOCATE_BUFFER flag is present.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! Just one tiny change please.
| FORMAT_MESSAGE_IGNORE_INSERTS | ||
| FORMAT_MESSAGE_MAX_WIDTH_MASK, | ||
null(), | ||
hr as u32, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's change this u32
to DWORD
(from winapi::shared::minwindef
). I know functionally they're the same and the u32
looks nicer but matching the types used in Microsoft docs when calling a winapi function will help whomever is reading this code in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Completely agree, changed it
…tch windows API types.
null(), | ||
hr as u32, | ||
0, | ||
transmute(&mut message_buffer as *const LPWSTR), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can remove the as *const LPWSTR
part. Otherwise we might as well just write transmute(&message_buffer)
.
Probably best to have this instead:
transmute(&mut message_buffer)
The mut
part doesn't seem required for the code to work, but it communicates the fact that the function will modify the pointer so we can keep the mut
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I was a bit slow with this comment. I wasn't waiting until you push the DWORD
change, I swear! 😬
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to work without the as *const LPWSTR
, but then clippy complains:
warning: transmute from a reference to a pointer
--> druid-shell\src\platform\windows\error.rs:55:13
|
55 | transmute(&mut message_buffer),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try:&mut message_buffer as *mut *mut u16 as *mut u16
|
= note:#[warn(clippy::useless_transmute)]
on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_transmute
Is that another false positive? I already turned off clippy::crosspointer_transmute for this function in an earlier change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clippy seems unhappy indeed. However as *mut LPWSTR
seems to satisfy clippy and is more to the truth, so maybe use that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah just listening to clippy apparantly this just works: &mut message_buffer as *const LPWSTR as LPWSTR
, didn't know you could do that without transmute. Then I can remove the clippy ignore flags. Does that sound good?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great find! I was trying to get rid of transmute
earlier but didn't know how. It looks like the following works as well:
&mut message_buffer as *mut LPWSTR as LPWSTR
Let's go with that if possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good! I just pushed this change :)
3faa134
to
5771906
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work, this will be very useful.
I was going to complain about the obviously wrong casting, but that's winapi being winapi, and I agree this is the best way to write that in Rust. |
When implementing error handling for #712 I thought it'd be nice to add some details to the error
HRESULT
error messages instead of just error codes.This change adds a short description from the FormatMessage windows API when printing an
Error::Hr
, when one is available.As an example the following messages:
are now displayed as: