Commit graph

44 commits

Author SHA1 Message Date
Protobuf Team Bot
7f7b974fce Allow ProtoStr to be used in const contexts.
PiperOrigin-RevId: 886834080
2026-03-20 09:34:26 -07:00
Samuel Benzaquen
6ced6127bc Change _opt() accessors to return an std::Option instead of protobuf::Optional
protob::Optional was designed to be able to carry a default value in the Unset case, as in:

```message M { int32 x = 1 [default=42]; }```

Before this change: m.x_opt() would return Unset(42) if the field was unset.

After this change: m.x_opt() will return None instead. If they want to know the default value they will need to look at the accessor again, like:

if !msg.x_opt().is_none() {   // or !msg.has_x()
  let default_val = msg.x();
}

PiperOrigin-RevId: 883178303
2026-03-13 08:31:00 -07:00
Protobuf Team Bot
f8daf2fabc Add some common methods to ProtoStr to make it more ergonomic to use without dropping down to &[u8]
Also make ProtoString deref to &ProtoStr to get the same methods there.

PiperOrigin-RevId: 882142314
2026-03-11 12:36:35 -07:00
Protobuf Team Bot
b3cd288ce4 Have from_utf8_unchecked on both ProtoString and &ProtoStr not be considered 'unsafe' since we guarantee it won't reach UB if it is used.
In both cases, support only try_into() not into() when going from [u8]: its intended that `string` fields maintain utf8ness invariant, so even though it is not UB if the invariant is violated, people should avoid it where feasible.

PiperOrigin-RevId: 878704268
2026-03-04 15:51:59 -08:00
Protobuf Team Bot
a8daa95057 Trait clarity improvement on Map traits
Make a new trait "MapKey" to represent the types that are allowed as a Map Key. Without this, right now our Map<> bounds is over generalized.

Rename "ProxiedInMapValue" to "MapValue" to correspondingly represent the types that are allowed as a Map Value.

PiperOrigin-RevId: 860118222
2026-01-23 08:26:31 -08:00
Joshua Haberman
ced5509045 Use the standard library's implementation of Utf8Chunks.
Also removed the `chars()` and `utf8_chunks()` methods, as it's equally easy for the user to call `proto_str.as_bytes().utf8_chunks()`.

PiperOrigin-RevId: 850439621
2025-12-30 09:44:27 -08:00
Adam Cozzette
94360aacfb Remove the Proxy, ViewProxy, and MutProxy traits
These traits don't seem to be doing a whole lot, so it seems worthwhile to
remove them and simplify things a bit.

PiperOrigin-RevId: 816759211
2025-10-08 10:20:05 -07:00
Adam Cozzette
ecb3113cda Continue separating Rust gencode from the upb API
This CL does a few different things:
 - Moves the implementations of `OwnedMessageInterop`, `MessgageMutInterop`,
   and `MessageViewInterop` out of the gencode and into the runtime. To make
   this possible, I tweaked the gencode so that `FooView<'msg>` implements
   `From<MessageViewInner<'msg, Foo>>`, analogous to what we already do on mut
   proxies for `MessageMutInner`.
 - Removes remaining references to the unstable upb API from generated field
   accessors. This required adding a bit of missing functionality on
   `MessagePtr<T>`.
 - Removes `MiniTableFieldPtr<T>`, since we ended up not using this struct.
 - Removes the `raw_msg()` inherent method from upb owned messages and proxy
   types, allowing us to hide `upb::RawMessage` from the gencode. I tried
   removing it from the C++ kernel as well, but that turned out to be impractical
   because many things still depend on it.

This gets us to the point where the only major part of the unstable upb API
still used by gencode is the stuff related to building mini-tables, which I
hope to address next.

PiperOrigin-RevId: 802234798
2025-09-02 11:56:19 -07:00
Adam Cozzette
115d905a1e Reduce the amount of upb internals exposed to Rust gencode
PiperOrigin-RevId: 795479672
2025-08-15 08:12:22 -07:00
Protobuf Team Bot
84ed87e1a0 Upgrade to googletest 0.13 crate.
PiperOrigin-RevId: 705994414
2024-12-13 14:03:59 -08:00
Protobuf Team Bot
b025398f9c Move __runtime into __internal::runtime
Clarify the rustdoc on __internal

PiperOrigin-RevId: 703541883
2024-12-06 10:33:43 -08:00
Protobuf Team Bot
0325132bf4 Use the latest googletest version number in our generated Cargo.toml for release.
Crates.io does not allow publishing of Cargo.toml files which point to git.

PiperOrigin-RevId: 698801255
2024-11-21 09:04:17 -08:00
Dmitri Gribenko
744c9ddfc5 Rename #[googletest::test] to #[gtest]
PiperOrigin-RevId: 661993029
2024-08-12 01:48:49 -07:00
Protobuf Team Bot
c7d4697481 Add an protobuf::__internal::SealedInternal trait
The purpose of this trait is that it is declared as a supertrait of the traits that we don't want application code to implement (like "Proxied" and "MessageView"); application code can see those traits but not the supertrait, which enables them to use them but not implement them.

PiperOrigin-RevId: 657555025
2024-07-30 05:46:11 -07:00
Jakob Buchgraber
5854e2b6a8 Implement std::error::Error for String
This enables the use of `proto_str.to_str()?` with anyhow::Result.

PiperOrigin-RevId: 657122601
2024-07-29 02:42:25 -07:00
Adam Cozzette
7141c30425 Rust: update map setters to take IntoProxied
I also added a blanket implementation of `IntoProxied<T> for T` so that we
don't have to duplicate this no-op implementation for all our types.

PiperOrigin-RevId: 656465755
2024-07-26 11:16:01 -07:00
Derek Benson
6f58085577 Adds an initial Cargo.toml for the protobuf crate.
Adds a new 'bzl' feature that is used to adjust import paths that need to change
in the Cargo build vs Blaze build.
This protobuf rust crate is a single crate that merges all of our current crates (protobuf, rust/upb, and utf8).

PiperOrigin-RevId: 656405153
2024-07-26 08:17:41 -07:00
Protobuf Team Bot
e6304eb3a2 Add AsView + AsMut as supertraits of Proxied and MutProxied.
PiperOrigin-RevId: 655660782
2024-07-24 12:30:50 -07:00
Jakob Buchgraber
8cdc700b5b Implement ctype=CORD for singular string & byte fields
- We introduce two new view types ProtoStringCow and ProtoBytesCow.
 - In UPB, for cord field accessors we always return a Cow::Borrowed.
 - In C++, for coed field accessors we check if the underlying absl::Cord is flat (contigous) and if so return a Cow::Borrowed. If it's not flat we copy the data to a ProtoString and return a Cow::Owned.
   - We expect the absl::Cord to be flat almost all the time. We have experimentally verified that for small strings (<4 KiB) and less than 6 appends the cord is in fact flat [1].
 - This change lifts the requirement of all ViewProxy types to be Copy. Our Cow types cannot be Copy because the owned types aren't copy.

[1] https://source.corp.google.com/piper///depot/google3/experimental/users/buchgr/cords/cords.cc

PiperOrigin-RevId: 655485943
2024-07-24 02:32:16 -07:00
Protobuf Team Bot
3c95fc8b76 Create AsView/IntoView/AsMut/IntoMut traits
These 'verb' traits are more in line with our plans for the common message operations.

PiperOrigin-RevId: 655163895
2024-07-23 07:58:59 -07:00
Dmitri Gribenko
0a917b95f2 Use #[googletest::test] in Protobuf Rust
PiperOrigin-RevId: 654726974
2024-07-22 06:59:28 -07:00
Protobuf Team Bot
cf948e4a81 Restructure the ViewProxy versus MutProxy trait setup.
Rather than two traits (MutProxy subtrait of ViewProxy), instead have three traits (MutProxy subtrait of Proxy, and ViewProxy subtrait of Proxy).

This makes things more consistent, including that (MutProxied subtraits Proxied) is now more parallel to (MutProxy subtraits Proxy).

ViewProxy is largely a marker trait here but leaves a spot for methods that should be on ViewProxies but not MutProxies if those do show up later.

PiperOrigin-RevId: 653661953
2024-07-18 10:04:54 -07:00
Jakob Buchgraber
08da921314 Remove the defensive copy on singular string setter
Calling into_proxied() already does a copy and before this change we were doing a second one.

I am not using set_allocated_<field(std::string* s) because the method is not generated when [features.(pb.cpp).string_type = VIEW] is specified.

PiperOrigin-RevId: 650612909
2024-07-09 07:01:58 -07:00
Jakob Buchgraber
0d6e9794d1 Migrate Repeated::{push, set} and Map::insert to use the IntoProxied trait.
* The public Repeated::{push, set} and Map::insert methods now accept any value that implements IntoProxied<T>, allowing us to move owned values instead of copying them.
  * This change also updates the FFI layer for strings/bytes in the repeated and maps thunks to accept a std::string* that can be moved rather than a PtrAndLen type that needs to be copied.
  * Tests are updated to no longer .as_view() when setting a message / string on a repeated / map field. The IntoProxied trait makes calling .as_view() obsolete.

PiperOrigin-RevId: 650580788
2024-07-09 04:47:00 -07:00
Marcel Hlopko
6b7e814347 Add ProtoString/Bytes owned types
PiperOrigin-RevId: 644999527
2024-06-20 06:07:35 -07:00
Jakob Buchgraber
959903d199 Remove the now unused vtable types. In particular,
* SettableValue
 * ProxiedWithPresence
 * FieldEntry

PiperOrigin-RevId: 629363417
2024-04-30 04:03:50 -07:00
Jakob Buchgraber
f9ed22055e Remove MutProxied for bytes/string fields.
This change then also makes the BytesMut/ProtoStrMut types unused. It removes them and related code.

PiperOrigin-RevId: 629023886
2024-04-29 04:16:00 -07:00
Protobuf Team Bot
1d0028ddda Fix unused warnings in Rust Proto
PiperOrigin-RevId: 628973552
2024-04-29 00:02:43 -07:00
Jakob Buchgraber
849b975e5f Temporarily use AsRef<{ProtoStr, [u8]}> for string/bytes accessors
This change removes the only remaining instance of SettableValue in a generated accessor. The principled fix is to implement IntoProxied for ProtoStr/[u8], but this will have to wait until we have agreed on & implemented the 1.0 string types. So we'll use AsRef in the meantime in order to not break any user code while allowing us to make progress on IntoProxied and Proxied changes.

PiperOrigin-RevId: 627735404
2024-04-24 08:22:33 -07:00
Jakob Buchgraber
1a7ce61061 Split Proxied into Proxied and MutProxied traits.
Proxied is not marked as Sized yet, because ProtoStr is still dynamically sized. We will wait for clarity for the string types before marking Proxied Sized.

PiperOrigin-RevId: 627707544
2024-04-24 06:23:49 -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
Protobuf Team Bot
434d109d9c Make utf8chars.chars iter return impl Iterator + fmt::Debug.
PiperOrigin-RevId: 596599987
2024-01-08 08:23:41 -08:00
Alyssa Haroldsen
1dd6a7d06e Clean up some trivial lifetime usage
- Rename most usage of `'a` to `'msg`
- Remove a no-op unused lifetime param for `remove` in maps
- Elide lifetimes recommended by clippy

PiperOrigin-RevId: 589878364
2023-12-11 11:04:23 -08:00
Jakob Buchgraber
ab11a0d1a8 Name the lifetime in the signature of SettableValue.set_on()
This change names the lifetime of Mut<'a, T> and requires that T outlives 'a. The motivation for this change came up while implementing `Map<K, ProtoStr>`. The Map implementation makes it so that `V` needs to implement the `MapWithKeyOps` trait which has an associated type with a lifetime (`Value<'a>`. The lifetime bound on `T` ensures that e.g. for `MapWithKeyOps<Value<'b>=&'b ProtoStr>` `'a` outlives `'b`.

PiperOrigin-RevId: 585657154
2023-11-27 08:18:41 -08:00
Sandy Zhang
81068e8e8c Internal change
PiperOrigin-RevId: 566426899
2023-09-18 15:17:56 -07:00
Joshua Haberman
4a513035cf Shorten our license headers into an abbreviated form that references LICENSE instead of including it in full.
PiperOrigin-RevId: 563888892
2023-09-08 17:24:23 -07:00
Alyssa Haroldsen
614e29f946 Implement v0.6 string fields
This is predominantly a wrapper around `BytesMut`, for simplicity.
Bytes and string fields are mostly the same, except for possible UTF-8 handling.

This also implements some minor parts of `ProtoStr` that were missed.

PiperOrigin-RevId: 561422951
2023-08-30 12:57:54 -07:00
Alyssa Haroldsen
d5d207f099 Move common macros to a shared location
PiperOrigin-RevId: 559855079
2023-08-24 13:40:56 -07:00
Alyssa Haroldsen
9a0bc392b3 Implement v0.6 Optional Bytes
This makes a few changes:
 - It changes generated messages to reference message innards as a type in `__runtime` instead of branching on what fields should be there. That results in much less bifurcation in gencode and lets runtime-agnostic code reference raw message innards.
- It adds a generic mechanism for creating vtable-based mutators. These vtables point to thunks generated for interacting with C++ or upb fields. Right now, the design results in 2-word (msg+vtable) mutators for C++ and 3-word mutators (msg+arena+vtable) for UPB. See upb.rs for an explanation of the design options. I chose the `RawMessage+&Arena` design for mutator data as opposed to a `&MessageInner` design because it did not result in extra-indirection layout changes for message mutators. We could revisit this in the future with performance data, since this results in all field mutators being 3 words large instead of the register-friendly 2 words.
- And lastly, as a nearby change that touches on many of the same topics, it adds some extra SAFETY comments for Send/Sync in message gencode.

PiperOrigin-RevId: 559483437
2023-08-23 11:08:21 -07:00
Protobuf Team Bot
4b0e76370b Add UTF-8 handling to ProtoStr
This mostly uses a copied `Utf8Chunks` utility from nightly Rust.

PiperOrigin-RevId: 551224417
2023-07-26 09:17:38 -07:00
Protobuf Team Bot
49d3bca39f Improve overall unsafe hygiene
This adds `#![deny(unsafe_op_in_unsafe_fn)]` which removes the
implicit `unsafe` block that `unsafe fn` does.

It also adds many more `SAFETY` docs, corrects some incomplete
ones, and catches a null pointer returned by `upb_Arena_New`.

PiperOrigin-RevId: 549067106
2023-07-18 11:53:09 -07:00
Protobuf Team Bot
8fe9d8e7c1 Define the shape of ProtoStr as a DST
PiperOrigin-RevId: 547871451
2023-07-13 11:49:19 -07:00
Protobuf Team Bot
230a9a751f Remove stubs for in-place mutating BytesMut operations
These are invalid for UPB which uses immutable string views.

This also adjusts some docs:
- removes a promise in the language of `truncate`
that no reallocation will occur.
- removes language about DerefMut and replaces the `&mut Vec<u8>` analogy

PiperOrigin-RevId: 547603918
2023-07-12 14:39:06 -07:00
Protobuf Team Bot
a75def66cd Define the shape of BytesMut with stubbed methods
This is the primary mutator for `bytes` fields, which proxy `[u8]`.

PiperOrigin-RevId: 547242972
2023-07-11 11:13:41 -07:00