Commit graph

34 commits

Author SHA1 Message Date
Protobuf Team Bot
e1c5f3ac47 rust protobuf matchers: Add a partially matcher to mirror C++
It is common to want to match only a subset of fields in large protobufs, and doing that in rust right now requires manually clearing out the fields in the `actual` message that we don't care about. This is a pain, and a worse experience when compared to C++.

Like in C++, when doing partial matching, only fields set in the expected protobuf message are compared, while extra fields set only in the actual message are ignored.

When using the C++ kernel we leverage `proto2::util::MessageDifferencer`, and for the UPB kernel we use the `kUpb_CompareOption_Partial` option.

PiperOrigin-RevId: 964125682
2026-08-13 09:24:36 -07:00
Protobuf Team Bot
c99b4fbc40 Replace transmute on arena with pointer cast
This is just a slightly more Rust idiomatic way of being clear that you are knowingly using unsafe to soundly extend a lifetime.

PiperOrigin-RevId: 926763735
2026-06-04 10:36:14 -07:00
Protobuf Team Bot
06afcbd65b RustProto: add serialized_len()->usize
PiperOrigin-RevId: 904638364
2026-04-23 14:34:21 -07:00
Protobuf Team Bot
0be05fd8f9 Add Private passkey to extension api that is only intended to be available for internal and gencode use.
PiperOrigin-RevId: 899554889
2026-04-14 06:48:28 -07:00
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
Joshua Haberman
f54e9a1eaf Refactor: Move interop traits to kernel-specific modules.
This change moves the `OwnedMessageInterop`, `MessageViewInterop`, and `MessageMutInterop` traits from `codegen_traits.rs` into new `interop.rs` files within both the `cpp_kernel` and `upb_kernel` directories.

The `KernelMessage`, `KernelMessageView`, and `KernelMessageMut` traits in each kernel's `message.rs` are updated to include the respective kernel-specific interop traits. This ensures that the interop functionality is tied to the specific kernel implementation.

The `shared.rs` module now re-exports the interop traits from `crate::__internal::runtime::interop`, which will resolve to the correct kernel's interop module.

It would be possible to take this farther by making the interop traits return strongly typed pointers to `upb_Message*` and `proto2::MessageLite*` instead of `c_void`, but that would be a breaking change.

PiperOrigin-RevId: 892470084
2026-03-31 12:04:34 -07:00
Protobuf Team Bot
002f830c46 Remove Copy+Clone from MessageMutInner.
MessageMutInner was Copy+Clone, which had been convenient for some internal use. But, with them being .into() SomeMessageMut this was a potential footgun, since it was very easy to into() the same one twice which would be formally unsound if it was reachable (including that you could send each of them to a different thread and concurrently modify them).

As these were implementation detail types this is only a defensive fix and no actual soundness issues were identified.

PiperOrigin-RevId: 888615057
2026-03-24 05:52:58 -07:00
Joshua Haberman
1c478d5923 Added appropriate super-traits to Enum.
The `Enum` trait now requires several other traits, including `Singular`, `Into<i32>`, `Copy`, `Proxied`, `EntityType`, and `SealedInternal`. This simplifies bounds in functions that operate on types implementing `Enum`, as they no longer need to explicitly list `+ Singular` and other common requirements.

PiperOrigin-RevId: 888286136
2026-03-23 14:40:03 -07:00
Joshua Haberman
2b3058dcff Refactor: Split upb and cpp backends into submodules
This CL contains no functional change, it is a pure splitting of the existing files.

I elected to move the C++ kernel files to `cpp_kernel/` which already contained the C++ API for the C++ kernel.  Unfortunately this required removing the existing `third_party/protobuf/rust/cpp_kernel/BUILD` file, because, the Rust parts of the cpp kernel have circular dependencies with the shared code in `third_party/protobuf/rust`.

PiperOrigin-RevId: 886884124
2026-03-20 11:13:43 -07:00
Protobuf Team Bot
2678e10c99 Create a upb rust directory.
The intent of this directory would be for a layer of Rust bindings that directly map to upb semantics; Rust Protobuf runtime would be layer on top of that Rust, instead of directly on the upb C api.

PiperOrigin-RevId: 624282429
2024-04-12 13:57:44 -07:00
Alyssa Haroldsen
035d6ec2cb Implement map iteration
PiperOrigin-RevId: 604447996
2024-02-05 15:19:51 -08:00
Kevin King
8876b1069f impl SettableValue for MsgView
For the cpp runtime, call the `Message::CopyFrom` method.

For the upb runtime, expose the message `MiniTable` and call `upb_Message_DeepCopy`.

PiperOrigin-RevId: 595166276
2024-01-02 11:22:46 -08:00
Adam Cozzette
4ec9170bcd Fix layering check for usage of gtest
To satisfy the layering check, we need to depend on :gtest for the headers, in
addition to :gtest_main which provides the main() function.

There are a bunch of formatting changes as a side effect of this, but they
should be harmless.

PiperOrigin-RevId: 594318263
2023-12-28 14:41:48 -08:00
Eric Salo
708913c05c rust: stop using deprecated upb:collections as a dep
PiperOrigin-RevId: 583932650
2023-11-20 00:47:58 -08:00
Jakob Buchgraber
ac3f553073 This CL implements msg.<field>() and msg.<field_mut>() accessors for maps with primitive-typed keys and values for the UPB kernel only.
Support for the CPP runtime and non-scalar value types will be implemented in follow up CLs.

PiperOrigin-RevId: 580453646
2023-11-08 02:16:21 -08:00
Protobuf Team Bot
e1bb7d65a8 Implement rust repeated scalars for cpp and upb
PiperOrigin-RevId: 574261929
2023-10-17 14:18:54 -07:00
Adam Cozzette
501ececd39 Reorganize upb file structure
This change moves almost everything in the `upb/` directory up one level, so
that for example `upb/upb/generated_code_support.h` becomes just
`upb/generated_code_support.h`. The only exceptions I made to this were that I
left `upb/cmake` and `upb/BUILD` where they are, mostly because that avoids
conflict with other files and the current locations seem reasonable for now.

The `python/` directory is a little bit of a challenge because we had to merge
the existing directory there with `upb/python/`. I made `upb/python/BUILD` into
the BUILD file for the merged directory, and it effectively loads the contents
of the other BUILD file via `python/build_targets.bzl`, but I plan to clean
this up soon.

PiperOrigin-RevId: 568651768
2023-09-26 14:38:35 -07:00
Adam Cozzette
87fcc79861 Internal change
PiperOrigin-RevId: 567664389
2023-09-22 10:54:50 -07:00
Adam Cozzette
7286ffceb2 Merge the protobuf and upb Bazel repos
A couple weeks ago we moved upb into the protobuf Git repo, and this change
continues the merger of the two repos by making them into a single Bazel repo.
This was mostly a matter of deleting upb's WORKSPACE file and fixing up a bunch
of references to reflect the new structure.

Most of the changes are pretty mechanical, but one thing that needed more
invasive changes was the Python script for generating CMakeLists.txt,
make_cmakelists.py. The WORKSPACE file it relied on no longer exists with this
change, so I updated it to hardcode the information it needed from that file.

PiperOrigin-RevId: 564810016
2023-09-12 13:09:54 -07:00
Joshua Haberman
638a860285 Shorten our license headers into an abbreviated form that references LICENSE instead of including it in full.
PiperOrigin-RevId: 563893662
2023-09-08 18:44:16 -07:00
Hong Shin
4f20efbb78 Internal Change
PiperOrigin-RevId: 546867607
2023-07-10 07:37:00 -07:00
Marcel Hlopko
ff750bb4c3 Put shared.rs and cpp.rs/upb.rs into the same crate.
The motivation is to make the setup simpler and more flexible.

PiperOrigin-RevId: 538189061
2023-06-06 08:25:55 -07:00
Marcel Hlopko
f92edc13c2 Automated rollback of commit fe7c4f9422.
PiperOrigin-RevId: 538128679
2023-06-06 03:21:20 -07:00
Marcel Hlopko
fe7c4f9422 Simplify Protobuf Rust runtime build setup
PiperOrigin-RevId: 537300231
2023-06-02 07:04:18 -07:00
Marcel Hlopko
c58067fa70 Enable sanitizers
PiperOrigin-RevId: 529371786
2023-05-04 04:36:38 -07:00
Miguel Young de la Sota
8e53046d94 Make protobuf_upb::Arena into a proper Rust RAII type.
PiperOrigin-RevId: 525775419
2023-04-20 09:57:25 -07:00
Marcel Hlopko
5814f6c977 Generate C++ thunks for Rust protos
In this CL I'd like to call existing C++ Protobuf API from the V0 Rust API. Since parts of the C++ API are defined inline and using (obviously) C++ name mangling, we need to create a "thunks.cc" file that:

1) Generates code for C++ API function we use from Rust
2) Exposes these functions without any name mangling (meaning using `extern "C"`)

In this CL we add Bazel logic to generate "thunks" file, compile it, and propagate its object to linking. We also add logic to protoc to generate this "thunks" file.

The protoc logic is rather rudimentary still. I hope to focus on protoc code quality in my followup work on V0 Rust API using C++ kernel.

PiperOrigin-RevId: 523479839
2023-04-11 12:58:34 -07:00
Marcel Hlopko
f7a2f4acea Add initial end-to-end test directory structure.
This turns out to be quite of a yak shave to be able to perfectly test both kernels without having to pass extra Blaze flags.

PiperOrigin-RevId: 521850709
2023-04-04 13:31:07 -07:00
Marcel Hlopko
083830d849 Update copyright year
PiperOrigin-RevId: 521800787
2023-04-04 10:29:27 -07:00
Marcel Hlopko
ef299f7af4 Disable some tests under msan
PiperOrigin-RevId: 521505590
2023-04-03 11:00:04 -07:00
Augie Fackler
648060bbcb Internal change.
PiperOrigin-RevId: 520627020
2023-03-30 06:59:13 -07:00
Jakob Buchgraber
7802b93442 Fix build errors in Protobuf Rust
PiperOrigin-RevId: 518619271
2023-03-22 11:08:58 -07:00
Protobuf Team Bot
f73c7cb74a Add license rules
PiperOrigin-RevId: 518370510
2023-03-21 13:49:49 -07:00
Marcel Hlopko
aaa338b285 Configure build for the C++ backend
PiperOrigin-RevId: 517429571
2023-03-17 09:22:54 -07:00