From cf645f6f21010a0d007b444a32b2b5ad0f9219fc Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Wed, 9 Mar 2022 12:16:00 -0800 Subject: [PATCH] Provide hex output for `Debug` impl of `HRESULT` and `NTSTATUS` (#1590) --- crates/libs/bindgen/src/replacements/ntstatus.rs | 2 +- crates/libs/windows/src/Windows/Win32/Foundation/mod.rs | 2 +- crates/libs/windows/src/core/hresult.rs | 8 +++++++- crates/tests/error/tests/test.rs | 9 +++++++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/crates/libs/bindgen/src/replacements/ntstatus.rs b/crates/libs/bindgen/src/replacements/ntstatus.rs index 4f9b58a20a..6f029d53a3 100644 --- a/crates/libs/bindgen/src/replacements/ntstatus.rs +++ b/crates/libs/bindgen/src/replacements/ntstatus.rs @@ -55,7 +55,7 @@ pub fn gen() -> TokenStream { impl ::core::cmp::Eq for NTSTATUS {} impl ::core::fmt::Debug for NTSTATUS { fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { - f.debug_tuple("NTSTATUS").field(&self.0).finish() + f.write_fmt(format_args!("NTSTATUS(0x{:08X})", self.0)) } } unsafe impl ::windows::core::Abi for NTSTATUS { diff --git a/crates/libs/windows/src/Windows/Win32/Foundation/mod.rs b/crates/libs/windows/src/Windows/Win32/Foundation/mod.rs index a70ce74676..2da0892bb0 100644 --- a/crates/libs/windows/src/Windows/Win32/Foundation/mod.rs +++ b/crates/libs/windows/src/Windows/Win32/Foundation/mod.rs @@ -5155,7 +5155,7 @@ impl ::core::cmp::PartialEq for NTSTATUS { impl ::core::cmp::Eq for NTSTATUS {} impl ::core::fmt::Debug for NTSTATUS { fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { - f.debug_tuple("NTSTATUS").field(&self.0).finish() + f.write_fmt(format_args!("NTSTATUS(0x{:08X})", self.0)) } } unsafe impl ::windows::core::Abi for NTSTATUS { diff --git a/crates/libs/windows/src/core/hresult.rs b/crates/libs/windows/src/core/hresult.rs index 6528c2c42e..7097211f88 100644 --- a/crates/libs/windows/src/core/hresult.rs +++ b/crates/libs/windows/src/core/hresult.rs @@ -3,7 +3,7 @@ use bindings::*; /// An error code value returned by most COM functions. #[repr(transparent)] -#[derive(Copy, Clone, Default, Debug, Eq, PartialEq)] +#[derive(Copy, Clone, Default, Eq, PartialEq)] #[must_use] #[allow(non_camel_case_types)] pub struct HRESULT(pub i32); @@ -110,6 +110,12 @@ impl core::convert::From> for HRESULT { } } +impl core::fmt::Debug for HRESULT { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.write_fmt(format_args!("HRESULT(0x{:08X})", self.0)) + } +} + struct HeapString(*mut u16); impl Drop for HeapString { diff --git a/crates/tests/error/tests/test.rs b/crates/tests/error/tests/test.rs index 4dc2b0f990..ab65b5edab 100644 --- a/crates/tests/error/tests/test.rs +++ b/crates/tests/error/tests/test.rs @@ -12,6 +12,9 @@ fn hresult() -> Result<()> { assert_eq!(S_OK.is_ok(), true); assert_eq!(S_OK.is_err(), false); + assert_eq!(format!("{:?}", S_OK), "HRESULT(0x00000000)"); + assert_eq!(format!("{:?}", E_INVALIDARG), "HRESULT(0x80070057)"); + S_OK.ok() } @@ -29,6 +32,9 @@ fn win32_error() -> Result<()> { assert_eq!(ERROR_SUCCESS.is_ok(), true); assert_eq!(ERROR_SUCCESS.is_err(), false); + assert_eq!(format!("{:?}", ERROR_SUCCESS), "WIN32_ERROR(0)"); + assert_eq!(format!("{:?}", ERROR_BAD_ARGUMENTS), "WIN32_ERROR(160)"); + ERROR_SUCCESS.ok() } @@ -45,5 +51,8 @@ fn ntstatus() -> Result<()> { assert_eq!(STATUS_SUCCESS.is_ok(), true); assert_eq!(STATUS_SUCCESS.is_err(), false); + assert_eq!(format!("{:?}", STATUS_SUCCESS), "NTSTATUS(0x00000000)"); + assert_eq!(format!("{:?}", STATUS_NOT_FOUND), "NTSTATUS(0xC0000225)"); + STATUS_SUCCESS.ok() }