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

Replace boilerplate COM interface code gen with macro_rules #2079

Merged
merged 5 commits into from Oct 3, 2022

Conversation

kennykerr
Copy link
Collaborator

@kennykerr kennykerr commented Sep 30, 2022

The windows and windows-sys crates avoid derive macros as much as possible as they tend to be very slow at scale. There is however a lot of boilerplate code that could be avoided with selective use of macros, so I am going to start making conservative use of macro_rules to replace large chunks of boilerplate code. I tested the change in this update and could not measure any difference in build time, so I think this is a good first step in reducing code gen and overall crate size.

The new interface_hierarchy macro stamps out most of the From implementations for COM and WinRT types that can often have large interface hierarchies. For example, ID3D12Device9 derives from 11 other interfaces leading to 33 From implementations that can be generated by the interface_hierarchy macro far more concisely.

macro_rules! interface_hierarchy {
($child:ty, $parent:ty) => {
impl ::std::convert::From<$child> for $parent {
fn from(value: $child) -> Self {
unsafe { ::std::mem::transmute(value) }
}
}
impl ::std::convert::From<&$child> for &$parent {
fn from(value: &$child) -> Self {
unsafe { ::std::mem::transmute(value) }
}
}
impl ::std::convert::From<&$child> for $parent {
fn from(value: &$child) -> Self {
unsafe { ::std::mem::transmute(::std::clone::Clone::clone(value)) }
}
}
};
($child:ty, $first:ty, $($rest:ty),+) => {
$crate::core::interface_hierarchy!($child, $first);
$crate::core::interface_hierarchy!($child, $($rest),+);
};
}

@kennykerr kennykerr merged commit ffb92c8 into master Oct 3, 2022
@kennykerr kennykerr deleted the interface-hierarchy branch October 3, 2022 14:40
Copy link
Contributor

@rylev rylev left a comment

Choose a reason for hiding this comment

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

Looks great! ❤️

Comment on lines +125 to +126
#[doc(hidden)]
pub use interface_hierarchy;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't believe this is necessary given the #[macro_export].

@@ -95,3 +95,32 @@ fn wide_trim_end(mut wide: &[u16]) -> &[u16] {
}
wide
}

#[doc(hidden)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Despite this being not shown to end users, it could be good to have some documentation here for those working on the internals of windows-rs.

@kennykerr
Copy link
Collaborator Author

Will do - thanks @rylev!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants