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

Simpler error interop #1462

Merged
merged 2 commits into from Jan 26, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/extensions/win32_error.rs
Expand Up @@ -4,7 +4,7 @@ pub fn gen() -> TokenStream {
quote! {
impl ::core::convert::From<WIN32_ERROR> for ::windows::core::HRESULT {
fn from(value: WIN32_ERROR) -> Self {
Self::from_win32(value.0)
Self(if value.0 as i32 <= 0 { value.0 } else { (value.0 & 0x0000_FFFF) | (7 << 16) | 0x8000_0000 } as _)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/windows/src/Windows/Win32/Foundation/mod.rs
Expand Up @@ -20844,7 +20844,7 @@ impl ::core::fmt::Debug for WIN32_ERROR {
}
impl ::core::convert::From<WIN32_ERROR> for ::windows::core::HRESULT {
fn from(value: WIN32_ERROR) -> Self {
Self::from_win32(value.0)
Self(if value.0 as i32 <= 0 { value.0 } else { (value.0 & 0x0000_FFFF) | (7 << 16) | 0x8000_0000 } as _)
}
}
#[doc = "*Required features: 'Win32_Foundation'*"]
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/windows/src/core/bindings.rs
Expand Up @@ -1588,7 +1588,7 @@ impl ::core::fmt::Debug for WIN32_ERROR {
}
impl ::core::convert::From<WIN32_ERROR> for ::windows::core::HRESULT {
fn from(value: WIN32_ERROR) -> Self {
Self::from_win32(value.0)
Self(if value.0 as i32 <= 0 { value.0 } else { (value.0 & 0x0000_FFFF) | (7 << 16) | 0x8000_0000 } as _)
}
}
#[repr(C)]
Expand Down
11 changes: 4 additions & 7 deletions crates/libs/windows/src/core/error.rs
Expand Up @@ -71,10 +71,11 @@ impl Error {
}

/// Returns the win32 error code if the underlying HRESULT's facility is win32
pub fn win32_error(&self) -> Option<u32> {
#[cfg(feature = "Win32_Foundation")]
pub fn win32_error(&self) -> Option<crate::Win32::Foundation::WIN32_ERROR> {
let hresult = self.code.0 as u32;
if ((hresult >> 16) & 0x7FF) == 7 {
Some(hresult & 0xFFFF)
Some(crate::Win32::Foundation::WIN32_ERROR(hresult & 0xFFFF))
} else {
None
}
Expand Down Expand Up @@ -129,11 +130,7 @@ impl core::convert::From<HRESULT> for Error {
impl core::fmt::Debug for Error {
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let mut debug = fmt.debug_struct("Error");
debug.field("code", &format_args!("{:#010X}", self.code.0)).field("message", &self.message());
if let Some(win32) = self.win32_error() {
debug.field("win32_error", &format_args!("{}", win32));
}
debug.finish()
debug.field("code", &format_args!("{:#010X}", self.code.0)).field("message", &self.message()).finish()
}
}

Expand Down
4 changes: 0 additions & 4 deletions crates/libs/windows/src/core/hresult.rs
Expand Up @@ -9,10 +9,6 @@ use bindings::*;
pub struct HRESULT(pub i32);

impl HRESULT {
pub fn from_win32(value: u32) -> HRESULT {
Self(if value as i32 <= 0 { value } else { (value & 0x0000_FFFF) | (7 << 16) | 0x8000_0000 } as _)
}

/// Returns [`true`] if `self` is a success code.
#[inline]
pub const fn is_ok(self) -> bool {
Expand Down
5 changes: 5 additions & 0 deletions crates/tests/enums/tests/win.rs
Expand Up @@ -34,6 +34,11 @@ fn win32_error() {
let h: HRESULT = ERROR_ACCESS_DENIED.into();
assert!(h.is_err());
assert!("WIN32_ERROR(5)" == format!("{:?}", e));

let e: Error = h.into();
assert_eq!("Error { code: 0x80070005, message: Access is denied.\r\n }", format!("{:?}", e));
let e: WIN32_ERROR = e.win32_error().unwrap();
assert!(e == ERROR_ACCESS_DENIED);
}

#[test]
Expand Down
3 changes: 0 additions & 3 deletions crates/tests/handles/tests/legacy.rs
@@ -1,4 +1,3 @@
use windows::core::HRESULT;
use windows::Win32::Foundation::*;
use windows::Win32::System::Registry::*;

Expand All @@ -15,11 +14,9 @@ fn handle() {
assert!(HANDLE(1).ok().unwrap() == HANDLE(1));

unsafe { SetLastError(ERROR_INVALID_WINDOW_HANDLE) };
assert!(HANDLE(0).ok().err().unwrap().code() == HRESULT::from_win32(ERROR_INVALID_WINDOW_HANDLE.0));
assert!(HANDLE(0).ok().err().unwrap().code() == ERROR_INVALID_WINDOW_HANDLE.into());

unsafe { SetLastError(ERROR_FILE_NOT_FOUND) };
assert!(HANDLE(-1).ok().err().unwrap().code() == HRESULT::from_win32(ERROR_FILE_NOT_FOUND.0));
assert!(HANDLE(-1).ok().err().unwrap().code() == ERROR_FILE_NOT_FOUND.into());

assert!(core::mem::size_of::<HANDLE>() == core::mem::size_of::<usize>());
Expand Down