Skip to content

Commit

Permalink
Fix Debug impls for COM interfaces (#3066)
Browse files Browse the repository at this point in the history
* Fix Debug impls for COM interfaces

Currently, the Debug impl for COM interfaces shows the recursive
interface chain, like so:

```
IDWriteFontFace5(IDWriteFontFace4(IDWriteFontFace3(IDWriteFontFace2(IDWriteFontFace(0xNNN)))))
```

That's not very useful. This PR trims it down to just the current interface:

```
IDWriteFontFace5(0xNNN)
```

* fix build break

---------

Co-authored-by: Arlie Davis <[email protected]>
  • Loading branch information
sivadeilra and Arlie Davis authored Jun 3, 2024
1 parent dbc3932 commit b8586ad
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
14 changes: 12 additions & 2 deletions crates/libs/core/src/imp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,32 @@ pub use required_hierarchy;
macro_rules! define_interface {
($name:ident, $vtbl:ident, $iid:literal) => {
#[repr(transparent)]
#[derive(::core::cmp::PartialEq, ::core::cmp::Eq, ::core::fmt::Debug, ::core::clone::Clone)]
#[derive(::core::cmp::PartialEq, ::core::cmp::Eq, ::core::clone::Clone)]
pub struct $name(::windows_core::IUnknown);
unsafe impl ::windows_core::Interface for $name {
type Vtable = $vtbl;
const IID: ::windows_core::GUID = ::windows_core::GUID::from_u128($iid);
}
impl ::core::fmt::Debug for $name {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple(stringify!($name)).field(&::windows_core::Interface::as_raw(self)).finish()
}
}
};
($name:ident, $vtbl:ident) => {
#[repr(transparent)]
#[derive(::core::cmp::PartialEq, ::core::cmp::Eq, ::core::fmt::Debug, ::core::clone::Clone)]
#[derive(::core::cmp::PartialEq, ::core::cmp::Eq, ::core::clone::Clone)]
pub struct $name(::std::ptr::NonNull<::std::ffi::c_void>);
unsafe impl ::windows_core::Interface for $name {
type Vtable = $vtbl;
const IID: ::windows_core::GUID = ::windows_core::GUID::zeroed();
const UNKNOWN: bool = false;
}
impl ::core::fmt::Debug for $name {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple(stringify!($name)).field(&self.0).finish()
}
}
};
}

Expand Down
2 changes: 1 addition & 1 deletion crates/libs/core/src/unknown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Eq for IUnknown {}

impl core::fmt::Debug for IUnknown {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("IUnknown").field(&self.0).finish()
f.debug_tuple("IUnknown").field(&self.as_raw()).finish()
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/libs/interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ impl Interface {
impl ::core::cmp::Eq for #name {}
impl ::core::fmt::Debug for #name {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.debug_tuple(#name_string).field(&self.0).finish()
f.debug_tuple(#name_string).field(&::windows_core::Interface::as_raw(self)).finish()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/tests/agile_reference/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ fn agile_debug() -> Result<()> {
assert!(format!("{uri:?}").starts_with("Uri(IUnknown(0x"));

let reference = AgileReference::new(&uri)?;
assert!(format!("{reference:?}").starts_with("AgileReference(IAgileReference(IUnknown(0x"));
assert!(format!("{reference:?}").starts_with("AgileReference(IAgileReference(0x"));
Ok(())
}
9 changes: 9 additions & 0 deletions crates/tests/implement_core/src/com_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,15 @@ fn common_method_name() {
assert_eq!(unsafe { ibar2.common() }, std::f32::consts::PI);
}

#[test]
fn debug_fmt() {
let app = MyApp::new(42);
let iunknown: IUnknown = app.to_interface();
println!("IUnknown = {iunknown:?}");
let ifoo: IFoo = app.to_interface();
println!("IFoo = {ifoo:?}");
}

// This tests that we can place a type that is not Send in a ComObject.
// Compilation is sufficient to test.
#[implement(IBar)]
Expand Down

0 comments on commit b8586ad

Please sign in to comment.