Skip to content

Conversation

ahayzen-kdab
Copy link
Contributor

This allows for developers and crates that are generating both C++ and Rust code to have a C++ method implemented in Rust without having to use a free method and passing through the C++ "this" as an argument.

This allows for developers and crates that are generating both C++ and Rust
code to have a C++ method implemented in Rust without having to use a
free method and passing through the C++ "this" as an argument.
@ahayzen-kdab
Copy link
Contributor Author

While trying to simplify our free functions and wrapper methods going between generated C++ classes, Rust structs, and methods implemented in a C++ context in Rust. We found that it was possible to implement a Rust method onto a C++ type. As CXX only generates the implementation for it and we are generating the C++ class we can specify the right header match. Then this avoids us having to have a free function to proxy C++ calls to the Rust calls as we can then go directly between.

As this was slightly non-obvious we wanted to submit a test to ensure that the behaviour doesn't regress and to ensure that it is accepted behaviour that we can depend on.

Copy link
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Thank you!

Comment on lines +52 to +53
// The header should be implemented in the C++ class definition and the Rust
// implementation in the usual way.
Copy link
Owner

Choose a reason for hiding this comment

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

I don't understand what this middle paragraph means.

I will make a followup PR to reword this.

Copy link
Owner

Choose a reason for hiding this comment

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

@dtolnay dtolnay merged commit b77f320 into dtolnay:master Aug 10, 2025
@dtolnay dtolnay mentioned this pull request Aug 10, 2025
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.

2 participants