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

Simplify parameter bindings #2360

Merged
merged 10 commits into from Mar 2, 2023
Merged

Simplify parameter bindings #2360

merged 10 commits into from Mar 2, 2023

Conversation

kennykerr
Copy link
Collaborator

@kennykerr kennykerr commented Mar 2, 2023

This update builds on #2343 and dramatically simplifies input parameter bindings, somethings that's always been rather complicated as we've attempted to model convertibility in Rust. Rather than depend on thousands of From and TryFrom implementations, types now implement the marker traits called CanInto and CanTryInto that do nothing but indicate to the windows crate that such types are convertible. The various Windows APIs can then rely on these traits to transform input arguments in the most efficient way possible, either casting directly to a parent interface - in a COM hierarchy - or calling QueryInterface when a conversion is expected - in a WinRT hierarchy.

The end result is a simpler abstraction with far less code. Specifically, it avoids generating around 6,000 TryFrom implementations and many thousands of From implementations. That amounts to a lot of functions that the Rust compiler doesn't have to parse, saving on build time.

For most applications, this change won't be noticeable as parameter bindings should just work as before. There are however two main changes that you might run into.

  • The Interface trait has been renamed to ComInterface to more accurately reflect what it represents. So, if you're relying on the cast method for explicit conversions then you might have to use ComInterface rather than use Interface.

  • If you previously relied on the TryFrom implementations for explicit WinRT conversions, simply use the cast method instead. The older try_from implementations simply called cast under the hood. This just saves having to generate thousands of wrapper functions and should improve your build time. In addition, the From implementations for COM hierarchies are no longer generated. You shouldn't need them often, but if you don't want to call cast (which injects a call to QueryInterface) then you can use the can_into or can_clone_into helpers for optimal performance.

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

1 participant