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

Fix Debug impls for COM interfaces #3066

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:?}");
Comment on lines +423 to +425
Copy link
Collaborator

@kennykerr kennykerr Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not tests. 😉 #3068

}

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