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

Improve the Interface trait #1738

Merged
merged 6 commits into from
May 6, 2022
Merged

Conversation

rylev
Copy link
Contributor

@rylev rylev commented May 5, 2022

Note: since this involves changes to generated code, it's easiest to review this commit by commit. The codegen was run in a separate commit from the other changes.

This improves the safety auditability of the Interface trait:

  • Adds safety docs to the Interface trait. Even though this trait is not meant for public consumption, the docs can really help those reviewing the safety of generated code.
  • Adds an as_raw method to the trait which simply wraps std::mem::transmute_copy. transmute_copy is very powerful and easy to use wrong. Having a bespoke method for converting an interface to a raw pointer which is documented as such makes the code easier to read. We've also had several users ask for this type of functionality before.
  • Unmarks Interface::vtable as unsafe since this method is in fact safe.

Finally, we use the Interface::as_raw method instead of std::mem::transmute_copy in the generated code.

cc @eholk

@rylev rylev requested a review from kennykerr May 5, 2022 11:23
@kennykerr
Copy link
Collaborator

Adds safety docs to the Interface trait. Even though this trait is not meant for public consumption, the docs can really help those reviewing the safety of generated code.

Note that parts of this trait are meant for public consumption. That's why some members were hidden. Notably, IID and cast are meant to be used by developers. The rest should remain hidden.

@rylev
Copy link
Contributor Author

rylev commented May 5, 2022

@kennykerr that sounds fine. I would argue for the newly added as_raw to also be publicly accessible as I think it's a better API (with many parallels in the standard library) than having to use std::mem::transmute_copy.

@rylev rylev requested a review from kennykerr May 5, 2022 16:04
kennykerr
kennykerr previously approved these changes May 5, 2022
eholk
eholk previously approved these changes May 5, 2022
@rylev rylev dismissed stale reviews from eholk and kennykerr via 0756da9 May 6, 2022 07:46
@rylev rylev force-pushed the improve-interface branch 2 times, most recently from af440bf to ac88754 Compare May 6, 2022 08:09
@rylev
Copy link
Contributor Author

rylev commented May 6, 2022

@kennykerr the latest nightly of clippy brought some new things to fix which I did, but it made the reviews stale. Can you re-review (only the last commit is new), and merge?

@kennykerr kennykerr merged commit 46b18ea into microsoft:master May 6, 2022
@rylev rylev deleted the improve-interface branch May 6, 2022 13:11
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

3 participants