Skip to content

Implement core::ops::Deref and DerefMut for Crubit cpp_std::unique_ptr and virtual_unique_ptr, and Deref for shared_ptr. - #1931

Open
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_974639553
Open

copybara-service[bot] wants to merge 1 commit into
mainfrom
test_974639553

Conversation

@copybara-service

Copy link
Copy Markdown

Implement core::ops::Deref and DerefMut for Crubit cpp_std::unique_ptr and virtual_unique_ptr, and Deref for shared_ptr.

While C++ std::unique_ptr is only shallow-const, it is conventionally treated as deep-const. In order for &unique_ptr<T> to be usable from Rust, Crubit treats unique_ptr as deep-const. Mutating T in C++ while Rust has obtained a &T derived from &unique_ptr<T> will result in undefined behavior.

Additionally, Deref and DerefMut will panic if the pointer is null, mirroring the behavior of these types in C++. Non-nullable variants of these types were considered and rejected due to additional complexity (new variant of every type) as well as the poor compatibility of the resulting types with C++ (e.g. non-null variants would be immovable in C++).

…ue_ptr` and `virtual_unique_ptr`, and `Deref` for `shared_ptr`.

While C++ `std::unique_ptr` is only shallow-const, it is conventionally treated as deep-const. In order for `&unique_ptr<T>` to be usable from Rust, Crubit treats `unique_ptr` as deep-const. Mutating `T` in C++ while Rust has obtained a `&T` derived from `&unique_ptr<T>` will result in undefined behavior.

Additionally, Deref and DerefMut will panic if the pointer is null, mirroring the behavior of these types in C++. Non-nullable variants of these types were considered and rejected due to additional complexity (new variant of every type) as well as the poor compatibility of the resulting types with C++ (e.g. non-null variants would be immovable in C++).

PiperOrigin-RevId: 974639553
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.

1 participant