dep-protobuf/rust/cpp_kernel
Joshua Haberman 7c958618b6 Added infrastructure for Rust Extensions support, but left it disabled for now.
This code makes use of some Rust features that are not supported in Rust 1.85 (our currently supported version), so we cannot enable the codegen for this feature yet.

This CL introduces support for [Protobuf Extensions](https://engdoc.corp.google.com/eng/doc/devguide/proto/programming-guides/proto2.html#extensions) to the Rust Protobuf library, a key feature for full Proto2 API parity.

Key changes include:

1.  **Code Generation:**
    *   Added `compiler/rust/extension.cc` and `extension.h` to handle the generation of Rust code for extension definitions.
    *   Updated `generator.cc` and `message.cc` to invoke extension generation for both top-level and nested extensions.
    *   Generates `ExtensionId` constants for each extension.

2.  **Runtime API (`rust/extension.rs`):**
    *   Introduced the generic `ExtensionId<Extendee, T>` type to identify and type-check extensions.
    *   Defined traits for extension operations: `ExtHas`, `ExtGet`, `ExtSet`, `ExtClear`, and `ExtGetMut`.
    *   Implemented methods on `ExtensionId` that dispatch to the trait implementations provided by the active kernel.

3.  **Kernel Implementations:**
    *   **C++ Kernel (`rust/cpp_kernel/extension.rs`):** Implemented extension traits by calling into new C++ FFI functions (defined in `rust/cpp_kernel/extension.cc`) that interact with the C++ `ExtensionSet`.
    *   **UPB Kernel (`rust/upb_kernel/extension.rs`):** Implemented extension traits using UPB's MiniTable-based extension API. Extension MiniTables are registered at static initialization time using the `linkme` crate.

4.  **Build System:**
    *   Updated `protobuf/rust/BUILD` and `protobuf/compiler/rust/BUILD` to include new files.
    *   Added a dependency on `//third_party/rust/linkme/v0_3` for the UPB kernel's extension registration.

5.  **Testing:**
    *   Added `test/extensions.proto` and `test/extensions_separate_file.proto` to define various extension types.
    *   Added `test/shared/extensions_test.rs` with comprehensive tests covering presence, getters, setters, defaults, nested extensions, submessage extensions, repeated extensions, serialization, and `MergeFrom`.

This change enables Rust users to define and interact with Protobuf extensions in a type-safe manner, compatible with both the C++ and UPB runtimes.

PiperOrigin-RevId: 896506594
2026-04-08 08:08:46 -07:00
..
compare.cc Suppress failure to consume parse/serialize return values. 2025-12-05 08:01:34 -08:00
compare.h Set up for Rust-cpp Protobuf implementation message matcher. 2024-08-01 07:52:17 -07:00
debug.cc Enable MessageLite::DebugString to use Message::DebugString where possible. 2024-07-03 09:37:37 -07:00
debug.h Enable MessageLite::DebugString to use Message::DebugString where possible. 2024-07-03 09:37:37 -07:00
extension.cc Added infrastructure for Rust Extensions support, but left it disabled for now. 2026-04-08 08:08:46 -07:00
extension.rs Added infrastructure for Rust Extensions support, but left it disabled for now. 2026-04-08 08:08:46 -07:00
interop.rs Refactor: Move interop traits to kernel-specific modules. 2026-03-31 12:04:34 -07:00
map.cc Refactor: Split upb and cpp backends into submodules 2026-03-20 11:13:43 -07:00
map.rs Refactor: Split upb and cpp backends into submodules 2026-03-20 11:13:43 -07:00
message.cc Automated Code Change 2025-10-30 02:55:48 -07:00
message.rs Added infrastructure for Rust Extensions support, but left it disabled for now. 2026-04-08 08:08:46 -07:00
mod.rs Added infrastructure for Rust Extensions support, but left it disabled for now. 2026-04-08 08:08:46 -07:00
raw.rs Added infrastructure for Rust Extensions support, but left it disabled for now. 2026-04-08 08:08:46 -07:00
repeated.cc Pass down Arena* to RepeatedPtrFieldBase::AddInternal. 2025-09-15 17:27:17 -07:00
repeated.rs Added infrastructure for Rust Extensions support, but left it disabled for now. 2026-04-08 08:08:46 -07:00
rust_alloc_for_cpp_api.h Change to proto2_rust C prefix and proto2::rust C++ namespace 2024-07-02 11:44:17 -07:00
rust_alloc_for_cpp_api.rs Start testing against Rust edition 2024 2025-10-07 09:21:51 -07:00
serialized_data.h Make SerializedData into a POD struct. 2024-09-18 14:17:14 -07:00
string.rs Refactor: Split upb and cpp backends into submodules 2026-03-20 11:13:43 -07:00
strings.cc Make conversion functions for into c++ string types from PtrAndLen. 2024-12-10 11:15:00 -08:00
strings.h Make conversion functions for into c++ string types from PtrAndLen. 2024-12-10 11:15:00 -08:00