Commit graph

14 commits

Author SHA1 Message Date
Alyssa Haroldsen
5983f7033c Transpose the map thunk macros, relocate to header file
PiperOrigin-RevId: 609766889
2024-02-23 10:26:25 -08:00
Alyssa Haroldsen
dd27b6b877 Use consistent name for the "get item from untyped map iterator" thunk
It now uses the same prefix as other thunks needed for the proxied type,
so the RawMapThunk helper can be used for enums.

Calling it a "iter next" thunk is misleading.
It does not increment the iterator as "next" implies,
it only gets the current key/value the iterator points to.

PiperOrigin-RevId: 609527442
2024-02-22 15:56:54 -08:00
Alyssa Haroldsen
035d6ec2cb Implement map iteration
PiperOrigin-RevId: 604447996
2024-02-05 15:19:51 -08:00
Alyssa Haroldsen
a48090e329 Make map insert return if the value was newly inserted
PiperOrigin-RevId: 603481207
2024-02-01 14:26:41 -08:00
Marcel Hlopko
d21425d334 Use consistent thunk prefix between generator and cpp.rs
PiperOrigin-RevId: 602644203
2024-01-30 01:34:05 -08:00
Marcel Hlopko
39e8ca7faf Add support for repeated strings/bytes
PiperOrigin-RevId: 599822292
2024-01-19 07:13:56 -08:00
Jakob Buchgraber
85972e505a #rust #protobuf Implement bytes as map values
We now support fields with bytes as map values e.g. map<i32, bytes>. The implementation for the C++ runtime was straightforward. The majority of the changes in this CL are about the UPB runtime. In UPB, when we insert Rust bytes/string into the map we need to first copy the bytes onto the maps arena. To support this I have rewritten the macro that implements the ProxiedInMapValue types. I refactored the functionality to convert between UPB and Rust types into the 'UpbTypeConversions' trait. This trait has a function 'to_message_value_if_required' which does the copying for bytes and strings.

PiperOrigin-RevId: 599118416
2024-01-17 03:37:45 -08:00
Jakob Buchgraber
8d9e3e9a1c #rust #protobuf Refactor maps to make ProxiedInMapValue independent of the runtime
- ProxiedInMapValue is defined in maps.rs, and no longer in the runtime files {upb, cpp}.rs.
 - ProxiedInMapValue's methods accept and return Proxied types.
 - InnerMapMut no longer has any generic type parameters.
 - Through this refactoring the Map type is no longer a ZST. Creating a new map is now as simple as `Map::new()`.

PiperOrigin-RevId: 597765165
2024-01-12 00:42:35 -08:00
Protobuf Team Bot
9d749cd5b0 Change Thunks that take by ref to take by pointer.
PiperOrigin-RevId: 592525870
2023-12-20 05:02:08 -08:00
Alyssa Haroldsen
f51182b543 Add RepeatedMut::clear, free for owned Repeated
PiperOrigin-RevId: 591046913
2023-12-14 14:11:56 -08:00
Jakob Buchgraber
976029283f Implement Maps for strings
This change implements maps with keys and values of type string e.g. Map<ProtoStr, i32> and Map<ProtoStr, ProtoStr>.

Implementing the Map type for ProtoStr has been different from scalar types because ProtoStr is an unsized type i.e. its size is not known at compile time. The existing Map implementation assumed sized types in many places. To make unsized types fit into the existing code architecture I have added an associated type 'Value' to the MapWith*KeyOps traits. The associated type needs to be sized and is the type returned by the Map::get(self, key) method e.g. for aProtoStr, the `type Value = &ProtoStr`.

PiperOrigin-RevId: 588783751
2023-12-07 07:25:00 -08:00
Jakob Buchgraber
001be3c655 Implement Maps for scalar types for the C++ kernel
This CL implements Maps for scalar types for the C++ runtime. It's orthogonal to cl/580453646. This CL is constrained by having to force template instantiation of proto2::Map<K, V>. Put differently, a Rust protobuf::Map<K, V> implementation needs to call 'extern "C"' functions with both key and value type in the function name (e.g. __pb_rust_Map_i32_f64_get()). We use macros to generate a Map implementation for every (K,V)-pair. An alternative would have been to use vtables.

Luckily a key in a protobuf map can only be integer types, bool and string. So the number of key types is bounded by the specification, while the number of value types is not i.e. any protobuf message can be a value in a map. Given these constraints we introduce one 'MapKeyOps' trait per key type e.g. MapKeyBOOLOps or MapKeyI32Ops. These traits need to be implemented for every value type e.g. 'impl MapKeyBOOLOps for i32' will implement 'Map::<bool, i32>'. In particular the MapKeyOps traits can also be implemented for generated messages without violating the orphan rule.

This CL also contains significant changes to the UPB runtime so that both upb.rs and cpp.rs export a similar interface to simplify the implementation in  map.rs and the generated code.

This CL does not yet implement the Proxied trait.

PiperOrigin-RevId: 582951914
2023-11-16 01:38:42 -08:00
Kevin King
65cdac4ac5 Finish Proxied impls for rust repeated scalars
PiperOrigin-RevId: 575935874
2023-10-23 14:54:44 -07:00
Protobuf Team Bot
e1bb7d65a8 Implement rust repeated scalars for cpp and upb
PiperOrigin-RevId: 574261929
2023-10-17 14:18:54 -07:00