Skip to content

Commit

Permalink
Provide hex output for Debug impl of HRESULT and NTSTATUS (#1590)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Mar 9, 2022
1 parent 2765470 commit cf645f6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/replacements/ntstatus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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 @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion crates/libs/windows/src/core/hresult.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -110,6 +110,12 @@ impl<T> core::convert::From<Result<T>> 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 {
Expand Down
9 changes: 9 additions & 0 deletions crates/tests/error/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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()
}

Expand All @@ -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()
}

0 comments on commit cf645f6

Please sign in to comment.