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

Tweak Win32 error code conversion to handle HRESULT input #2646

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl WIN32_ERROR {
}
#[inline]
pub const fn to_hresult(self) -> ::windows_core::HRESULT {
::windows_core::HRESULT(if self.0 == 0 { self.0 } else { (self.0 & 0x0000_FFFF) | (7 << 16) | 0x8000_0000 } as i32)
::windows_core::HRESULT::from_win32(self.0)
}
#[inline]
pub fn from_error(error: &::windows_core::Error) -> ::core::option::Option<Self> {
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/core/src/hresult.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ impl HRESULT {
}

/// Maps a Win32 error code to an HRESULT value.
pub(crate) fn from_win32(error: u32) -> Self {
Self(if error == 0 { 0 } else { (error & 0x0000_FFFF) | (7 << 16) | 0x8000_0000 } as i32)
pub const fn from_win32(error: u32) -> Self {
Self(if error as i32 <= 0 { error } else { (error & 0x0000_FFFF) | (7 << 16) | 0x8000_0000 } as i32)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/libs/windows/src/Windows/Win32/Foundation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21793,7 +21793,7 @@ impl WIN32_ERROR {
}
#[inline]
pub const fn to_hresult(self) -> ::windows_core::HRESULT {
::windows_core::HRESULT(if self.0 == 0 { self.0 } else { (self.0 & 0x0000_FFFF) | (7 << 16) | 0x8000_0000 } as i32)
::windows_core::HRESULT::from_win32(self.0)
}
#[inline]
pub fn from_error(error: &::windows_core::Error) -> ::core::option::Option<Self> {
Expand Down
18 changes: 15 additions & 3 deletions crates/tests/core/tests/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use windows::{core::*, Win32::Foundation::*, Win32::Media::Audio::*};

#[test]
fn test() {
let e = windows::core::Error::from(windows::Win32::Foundation::ERROR_NO_UNICODE_TRANSLATION);
fn display_debug() {
let e = Error::from(ERROR_NO_UNICODE_TRANSLATION);
let display = format!("{e}");
let debug = format!("{e:?}");
assert_eq!(display, "No mapping for the Unicode character exists in the target multi-byte code page. (0x80070459)");
Expand All @@ -9,9 +11,19 @@ fn test() {
r#"Error { code: HRESULT(0x80070459), message: "No mapping for the Unicode character exists in the target multi-byte code page." }"#
);

let e = windows::core::Error::from(windows::Win32::Media::Audio::AUDCLNT_E_UNSUPPORTED_FORMAT);
let e = Error::from(AUDCLNT_E_UNSUPPORTED_FORMAT);
let display = format!("{e}");
let debug = format!("{e:?}");
assert_eq!(display, "0x88890008");
assert_eq!(debug, r#"Error { code: HRESULT(0x88890008), message: "" }"#);
}

#[test]
fn hresult_last_error() {
unsafe {
assert_eq!(CRYPT_E_NOT_FOUND.0, 0x80092004u32 as i32);
SetLastError(WIN32_ERROR(CRYPT_E_NOT_FOUND.0 as u32));
let e = GetLastError().unwrap_err();
assert_eq!(e.code(), CRYPT_E_NOT_FOUND);
}
}