Skip to content

Implement Rust bindings for C++ operator[] overloads#722

Open
copybara-service[bot] wants to merge 1 commit intomainfrom
test_752439079
Open

Implement Rust bindings for C++ operator[] overloads#722
copybara-service[bot] wants to merge 1 commit intomainfrom
test_752439079

Conversation

@copybara-service
Copy link
Copy Markdown

Implement Rust bindings for C++ operator[] overloads

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.

@google-cla
Copy link
Copy Markdown

google-cla bot commented Mar 20, 2026

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
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.

0 participants