Implement Rust bindings for C++ operator[] overloads#722
Open
copybara-service[bot] wants to merge 1 commit intomainfrom
Open
Implement Rust bindings for C++ operator[] overloads#722copybara-service[bot] wants to merge 1 commit intomainfrom
operator[] overloads#722copybara-service[bot] wants to merge 1 commit intomainfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Instead of mapping directly to Rust's standard `std::ops::Index` and `std::ops::IndexMut` traits, this CL defines new Crubit-specific traits: `CcIndex` and `CcIndexMut`. This approach was chosen to address key interoperability challenges: 1. Return Types. C++ `operator[]` can return values, references, or proxy objects. Rust's `Index` traits are restrictive, requiring a reference (`&Self::Output`). The new traits use generic associated types (GATs) (`type Output<'a> where Self: 'a;`) to flexibly handle different return kinds and tie their lifetimes to the container. 2. `!Unpin` Containers. Many C++ types are not `Unpin` in Rust. `std::ops::IndexMut` takes `&mut self`, which is incompatible with `!Unpin` types that require `Pin<&mut Self>` for safe mutable access. `CcIndexMut::cc_index_mut` accepts `self: Pin<&mut Self>`. 3. `!Unpin` Items. The GAT-based `Output` type in `CcIndexMut` can return `Pin<&mut Item>` when necessary, preserving C++ safety invariants for non-movable items. Integration tests in `operators_index_test.rs` cover combinations of `Unpin` / `!Unpin` containers and items. PiperOrigin-RevId: 752439079
9f572da to
78d409d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implement Rust bindings for C++
operator[]overloadsInstead of mapping directly to Rust's standard
std::ops::Indexandstd::ops::IndexMuttraits, this CL defines new Crubit-specific traits:CcIndexandCcIndexMut. This approach was chosen to address key interoperability challenges:operator[]can return values, references, or proxy objects. Rust'sIndextraits are restrictive, requiring a reference (&Self::Output). The new traits use generic associated types (GATs) (type Output<'a> where Self: 'a;) to flexibly handle different return kinds and tie their lifetimes to the container.!UnpinContainers. Many C++ types are notUnpinin Rust.std::ops::IndexMuttakes&mut self, which is incompatible with!Unpintypes that requirePin<&mut Self>for safe mutable access.CcIndexMut::cc_index_mutacceptsself: Pin<&mut Self>.!UnpinItems. The GAT-basedOutputtype inCcIndexMutcan returnPin<&mut Item>when necessary, preserving C++ safety invariants for non-movable items.Integration tests in
operators_index_test.rscover combinations ofUnpin/!Unpincontainers and items.