Skip to content

Commit b8586ad

Browse files
authored
Fix Debug impls for COM interfaces (#3066)
* 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]>
1 parent dbc3932 commit b8586ad

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

crates/libs/core/src/imp/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,32 @@ pub use required_hierarchy;
7777
macro_rules! define_interface {
7878
($name:ident, $vtbl:ident, $iid:literal) => {
7979
#[repr(transparent)]
80-
#[derive(::core::cmp::PartialEq, ::core::cmp::Eq, ::core::fmt::Debug, ::core::clone::Clone)]
80+
#[derive(::core::cmp::PartialEq, ::core::cmp::Eq, ::core::clone::Clone)]
8181
pub struct $name(::windows_core::IUnknown);
8282
unsafe impl ::windows_core::Interface for $name {
8383
type Vtable = $vtbl;
8484
const IID: ::windows_core::GUID = ::windows_core::GUID::from_u128($iid);
8585
}
86+
impl ::core::fmt::Debug for $name {
87+
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> core::fmt::Result {
88+
f.debug_tuple(stringify!($name)).field(&::windows_core::Interface::as_raw(self)).finish()
89+
}
90+
}
8691
};
8792
($name:ident, $vtbl:ident) => {
8893
#[repr(transparent)]
89-
#[derive(::core::cmp::PartialEq, ::core::cmp::Eq, ::core::fmt::Debug, ::core::clone::Clone)]
94+
#[derive(::core::cmp::PartialEq, ::core::cmp::Eq, ::core::clone::Clone)]
9095
pub struct $name(::std::ptr::NonNull<::std::ffi::c_void>);
9196
unsafe impl ::windows_core::Interface for $name {
9297
type Vtable = $vtbl;
9398
const IID: ::windows_core::GUID = ::windows_core::GUID::zeroed();
9499
const UNKNOWN: bool = false;
95100
}
101+
impl ::core::fmt::Debug for $name {
102+
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> core::fmt::Result {
103+
f.debug_tuple(stringify!($name)).field(&self.0).finish()
104+
}
105+
}
96106
};
97107
}
98108

crates/libs/core/src/unknown.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl Eq for IUnknown {}
5555

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

crates/libs/interface/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ impl Interface {
361361
impl ::core::cmp::Eq for #name {}
362362
impl ::core::fmt::Debug for #name {
363363
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
364-
f.debug_tuple(#name_string).field(&self.0).finish()
364+
f.debug_tuple(#name_string).field(&::windows_core::Interface::as_raw(self)).finish()
365365
}
366366
}
367367
}

crates/tests/agile_reference/tests/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ fn agile_debug() -> Result<()> {
2020
assert!(format!("{uri:?}").starts_with("Uri(IUnknown(0x"));
2121

2222
let reference = AgileReference::new(&uri)?;
23-
assert!(format!("{reference:?}").starts_with("AgileReference(IAgileReference(IUnknown(0x"));
23+
assert!(format!("{reference:?}").starts_with("AgileReference(IAgileReference(0x"));
2424
Ok(())
2525
}

crates/tests/implement_core/src/com_object.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,15 @@ fn common_method_name() {
416416
assert_eq!(unsafe { ibar2.common() }, std::f32::consts::PI);
417417
}
418418

419+
#[test]
420+
fn debug_fmt() {
421+
let app = MyApp::new(42);
422+
let iunknown: IUnknown = app.to_interface();
423+
println!("IUnknown = {iunknown:?}");
424+
let ifoo: IFoo = app.to_interface();
425+
println!("IFoo = {ifoo:?}");
426+
}
427+
419428
// This tests that we can place a type that is not Send in a ComObject.
420429
// Compilation is sufficient to test.
421430
#[implement(IBar)]

0 commit comments

Comments
 (0)