dep-protobuf/rust/test/extensions.proto
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

68 lines
1.6 KiB
Protocol Buffer

// Protocol Buffers - Google's data interchange format
// Copyright 2026 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
edition = "2024";
package third_party_protobuf_rust_test;
message TestExtensions {
extensions 1 to max;
extend TestExtensions {
int32 nested_extension = 500;
}
}
message SimpleSubmessage {
int32 i32_field = 1 [default = 123];
}
extend TestExtensions {
int32 i32_extension = 1;
string str_extension = 2;
SimpleSubmessage submessage_extension = 20;
int32 i32_extension_with_default = 100 [default = 100];
repeated int32 repeated_i32_extension = 801;
repeated string repeated_str_extension = 802;
repeated SimpleSubmessage repeated_submessage_extension = 803;
int64 i64_extension = 11;
uint32 u32_extension = 12;
uint64 u64_extension = 13;
float f32_extension = 14;
double f64_extension = 15;
bool bool_extension = 16;
repeated int64 repeated_i64_extension = 811;
repeated uint32 repeated_u32_extension = 812;
repeated uint64 repeated_u64_extension = 813;
repeated float repeated_f32_extension = 814;
repeated double repeated_f64_extension = 815;
repeated bool repeated_bool_extension = 816;
TestEnum enum_extension = 30;
repeated TestEnum repeated_enum_extension = 830;
}
enum TestEnum {
FOO = 0;
BAR = 1;
}
enum ClosedEnum {
option features.enum_type = CLOSED;
CLOSED_ZERO = 0;
CLOSED_ONE = 1;
}
extend TestExtensions {
ClosedEnum closed_enum_extension = 31;
}