Skip to content

Commit

Permalink
HRESULT_FROM_WIN32
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Sep 6, 2023
1 parent 2ab3844 commit eab3194
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
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);
}
}

0 comments on commit eab3194

Please sign in to comment.