blob: 0df37b9248960c75ed7fba3e55b54833b89276fc [file] [log] [blame]
Marcel Hlopkoab9f1ab2023-03-16 04:57:32 -07001// Protocol Buffers - Google's data interchange format
Hong Shin4f20efb2023-07-10 07:35:36 -07002// Copyright 2023 Google LLC. All rights reserved.
Marcel Hlopkoab9f1ab2023-03-16 04:57:32 -07003//
Joshua Haberman4a513032023-09-08 17:12:50 -07004// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file or at
6// https://developers.google.com/open-source/licenses/bsd
Marcel Hlopkoab9f1ab2023-03-16 04:57:32 -07007
Miguel Young de la Sota8e530462023-04-20 09:54:41 -07008//! UPB FFI wrapper code for use by Rust Protobuf.
Marcel Hlopkoab9f1ab2023-03-16 04:57:32 -07009
Protobuf Team Botc7d46972024-07-30 05:39:53 -070010use crate::__internal::{Enum, Private, SealedInternal};
Jakob Buchgraberb539fa92023-12-20 02:41:41 -080011use crate::{
Adam Cozzette11428382024-09-12 11:22:07 -070012 IntoProxied, Map, MapIter, MapMut, MapView, Mut, ProtoBytes, ProtoStr, ProtoString, Proxied,
13 ProxiedInMapValue, ProxiedInRepeated, Repeated, RepeatedMut, RepeatedView, View,
Jakob Buchgraberb539fa92023-12-20 02:41:41 -080014};
Jakob Buchgraber95073f82023-11-21 04:27:56 -080015use core::fmt::Debug;
Protobuf Team Bote26ef862024-08-26 05:14:52 -070016use std::mem::{size_of, ManuallyDrop, MaybeUninit};
Adam Cozzettec3df3592024-09-05 11:09:02 -070017use std::ptr::{self, NonNull};
18use std::slice;
19use std::sync::OnceLock;
Marcel Hlopko5814f6c2023-04-11 12:57:07 -070020
Derek Benson6f580852024-07-26 08:09:33 -070021#[cfg(bzl)]
Protobuf Team Bot2678e102024-04-12 13:50:28 -070022extern crate upb;
Derek Benson6f580852024-07-26 08:09:33 -070023#[cfg(not(bzl))]
24use crate::upb;
Protobuf Team Bot2678e102024-04-12 13:50:28 -070025
26// Temporarily 'pub' since a lot of gencode is directly calling any of the ffi
27// fns.
28pub use upb::*;
29
30pub type RawArena = upb::RawArena;
31pub type RawMessage = upb::RawMessage;
32pub type RawRepeatedField = upb::RawArray;
33pub type RawMap = upb::RawMap;
34pub type PtrAndLen = upb::StringView;
35
36impl From<&ProtoStr> for PtrAndLen {
37 fn from(s: &ProtoStr) -> Self {
38 let bytes = s.as_bytes();
39 Self { ptr: bytes.as_ptr(), len: bytes.len() }
40 }
41}
42
Alyssa Haroldsen4395d972024-02-23 00:15:08 -080043/// The scratch size of 64 KiB matches the maximum supported size that a
44/// upb_Message can possibly be.
Protobuf Team Bot9473beb2024-02-12 11:57:19 -080045const UPB_SCRATCH_SPACE_BYTES: usize = 65_536;
Hong Shin6d4ea612023-10-16 09:45:35 -070046
47/// Holds a zero-initialized block of memory for use by upb.
Alyssa Haroldsen4395d972024-02-23 00:15:08 -080048///
Hong Shin6d4ea612023-10-16 09:45:35 -070049/// By default, if a message is not set in cpp, a default message is created.
50/// upb departs from this and returns a null ptr. However, since contiguous
51/// chunks of memory filled with zeroes are legit messages from upb's point of
52/// view, we can allocate a large block and refer to that when dealing
53/// with readonly access.
Alyssa Haroldsen4395d972024-02-23 00:15:08 -080054#[repr(C, align(8))] // align to UPB_MALLOC_ALIGN = 8
Protobuf Team Bote26ef862024-08-26 05:14:52 -070055#[doc(hidden)]
Alyssa Haroldsen4395d972024-02-23 00:15:08 -080056pub struct ScratchSpace([u8; UPB_SCRATCH_SPACE_BYTES]);
Hong Shin6d4ea612023-10-16 09:45:35 -070057impl ScratchSpace {
Protobuf Team Bote26ef862024-08-26 05:14:52 -070058 pub fn zeroed_block() -> RawMessage {
Alyssa Haroldsen4395d972024-02-23 00:15:08 -080059 static ZEROED_BLOCK: ScratchSpace = ScratchSpace([0; UPB_SCRATCH_SPACE_BYTES]);
60 NonNull::from(&ZEROED_BLOCK).cast()
Hong Shin6d4ea612023-10-16 09:45:35 -070061 }
62}
63
Protobuf Team Bot9d2dafe2024-07-22 11:30:36 -070064#[doc(hidden)]
Protobuf Team Bot734729a2024-04-24 12:25:37 -070065pub type SerializedData = upb::OwnedArenaBox<[u8]>;
Protobuf Team Botc2883432023-12-20 06:52:48 -080066
Protobuf Team Botc7d46972024-07-30 05:39:53 -070067impl SealedInternal for SerializedData {}
68
Marcel Hlopko6b7e8142024-06-20 05:54:09 -070069impl IntoProxied<ProtoBytes> for SerializedData {
70 fn into_proxied(self, _private: Private) -> ProtoBytes {
71 ProtoBytes { inner: InnerProtoString(self) }
72 }
73}
74
Alyssa Haroldsen9a0bc392023-08-23 11:05:31 -070075/// The raw contents of every generated message.
76#[derive(Debug)]
Protobuf Team Bote26ef862024-08-26 05:14:52 -070077#[doc(hidden)]
Alyssa Haroldsen9a0bc392023-08-23 11:05:31 -070078pub struct MessageInner {
79 pub msg: RawMessage,
80 pub arena: Arena,
81}
82
83/// Mutators that point to their original message use this to do so.
84///
85/// Since UPB expects runtimes to manage their own arenas, this needs to have
86/// access to an `Arena`.
87///
88/// This has two possible designs:
89/// - Store two pointers here, `RawMessage` and `&'msg Arena`. This doesn't
90/// place any restriction on the layout of generated messages and their
91/// mutators. This makes a vtable-based mutator three pointers, which can no
92/// longer be returned in registers on most platforms.
93/// - Store one pointer here, `&'msg MessageInner`, where `MessageInner` stores
94/// a `RawMessage` and an `Arena`. This would require all generated messages
95/// to store `MessageInner`, and since their mutators need to be able to
96/// generate `BytesMut`, would also require `BytesMut` to store a `&'msg
97/// MessageInner` since they can't store an owned `Arena`.
98///
99/// Note: even though this type is `Copy`, it should only be copied by
Alyssa Haroldsen9fe3ac62023-08-28 03:23:39 -0700100/// protobuf internals that can maintain mutation invariants:
101///
102/// - No concurrent mutation for any two fields in a message: this means
103/// mutators cannot be `Send` but are `Sync`.
104/// - If there are multiple accessible `Mut` to a single message at a time, they
105/// must be different fields, and not be in the same oneof. As such, a `Mut`
106/// cannot be `Clone` but *can* reborrow itself with `.as_mut()`, which
107/// converts `&'b mut Mut<'a, T>` to `Mut<'b, T>`.
Alyssa Haroldsen9a0bc392023-08-23 11:05:31 -0700108#[derive(Clone, Copy, Debug)]
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700109#[doc(hidden)]
Alyssa Haroldsen9a0bc392023-08-23 11:05:31 -0700110pub struct MutatorMessageRef<'msg> {
111 msg: RawMessage,
112 arena: &'msg Arena,
113}
114
115impl<'msg> MutatorMessageRef<'msg> {
116 #[doc(hidden)]
117 #[allow(clippy::needless_pass_by_ref_mut)] // Sound construction requires mutable access.
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700118 pub fn new(msg: &'msg mut MessageInner) -> Self {
Alyssa Haroldsen9a0bc392023-08-23 11:05:31 -0700119 MutatorMessageRef { msg: msg.msg, arena: &msg.arena }
120 }
121
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700122 pub fn from_parent(parent_msg: MutatorMessageRef<'msg>, message_field_ptr: RawMessage) -> Self {
Protobuf Team Botd716c2e2024-01-16 11:20:45 -0800123 MutatorMessageRef { msg: message_field_ptr, arena: parent_msg.arena }
Hong Shinb3cfff32023-11-13 09:08:02 -0800124 }
125
Alyssa Haroldsen9a0bc392023-08-23 11:05:31 -0700126 pub fn msg(&self) -> RawMessage {
127 self.msg
128 }
Kevin King8876b102024-01-02 11:19:58 -0800129
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700130 pub fn arena(&self) -> &Arena {
Protobuf Team Botdeb2dff2024-01-16 09:43:44 -0800131 self.arena
Kevin King8876b102024-01-02 11:19:58 -0800132 }
Alyssa Haroldsen9a0bc392023-08-23 11:05:31 -0700133}
134
Marcel Hlopko6b7e8142024-06-20 05:54:09 -0700135/// Kernel-specific owned `string` and `bytes` field type.
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700136#[doc(hidden)]
Marcel Hlopko6b7e8142024-06-20 05:54:09 -0700137pub struct InnerProtoString(OwnedArenaBox<[u8]>);
138
139impl InnerProtoString {
140 pub(crate) fn as_bytes(&self) -> &[u8] {
141 &self.0
142 }
Jakob Buchgraber08da9212024-07-09 06:59:28 -0700143
144 #[doc(hidden)]
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700145 pub fn into_raw_parts(self) -> (PtrAndLen, Arena) {
Jakob Buchgraber08da9212024-07-09 06:59:28 -0700146 let (data_ptr, arena) = self.0.into_parts();
147 (unsafe { data_ptr.as_ref().into() }, arena)
148 }
Marcel Hlopko6b7e8142024-06-20 05:54:09 -0700149}
150
151impl From<&[u8]> for InnerProtoString {
152 fn from(val: &[u8]) -> InnerProtoString {
153 let arena = Arena::new();
Protobuf Team Botb35d4ba2024-09-06 13:04:52 -0700154 let in_arena_copy = arena.copy_slice_in(val).unwrap();
Marcel Hlopko6b7e8142024-06-20 05:54:09 -0700155 // SAFETY:
156 // - `in_arena_copy` is valid slice that will live for `arena`'s lifetime and
157 // this is the only reference in the program to it.
158 // - `in_arena_copy` is a pointer into an allocation on `arena`
159 InnerProtoString(unsafe { OwnedArenaBox::new(Into::into(in_arena_copy), arena) })
160 }
161}
162
Protobuf Team Bot9ff062c2024-03-12 04:48:15 -0700163/// The raw type-erased version of an owned `Repeated`.
164#[derive(Debug)]
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700165#[doc(hidden)]
Protobuf Team Bot9ff062c2024-03-12 04:48:15 -0700166pub struct InnerRepeated {
167 raw: RawRepeatedField,
168 arena: Arena,
169}
170
171impl InnerRepeated {
172 pub fn as_mut(&mut self) -> InnerRepeatedMut<'_> {
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700173 InnerRepeatedMut::new(self.raw, &self.arena)
Protobuf Team Bot9ff062c2024-03-12 04:48:15 -0700174 }
Jakob Buchgraberb6e0a482024-05-06 05:16:05 -0700175
176 pub fn raw(&self) -> RawRepeatedField {
177 self.raw
178 }
179
180 pub fn arena(&self) -> &Arena {
181 &self.arena
182 }
Derek Benson9c994242024-07-15 12:36:48 -0700183
184 /// # Safety
185 /// - `raw` must be a valid `RawRepeatedField`
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700186 pub unsafe fn from_raw_parts(raw: RawRepeatedField, arena: Arena) -> Self {
Derek Benson9c994242024-07-15 12:36:48 -0700187 Self { raw, arena }
188 }
Protobuf Team Bot9ff062c2024-03-12 04:48:15 -0700189}
190
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800191/// The raw type-erased pointer version of `RepeatedMut`.
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -0700192#[derive(Clone, Copy, Debug)]
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700193#[doc(hidden)]
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800194pub struct InnerRepeatedMut<'msg> {
195 pub(crate) raw: RawRepeatedField,
Protobuf Team Bot9ff062c2024-03-12 04:48:15 -0700196 arena: &'msg Arena,
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -0700197}
198
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800199impl<'msg> InnerRepeatedMut<'msg> {
200 #[doc(hidden)]
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700201 pub fn new(raw: RawRepeatedField, arena: &'msg Arena) -> Self {
Protobuf Team Bot9ff062c2024-03-12 04:48:15 -0700202 InnerRepeatedMut { raw, arena }
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -0700203 }
204}
205
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800206macro_rules! impl_repeated_base {
207 ($t:ty, $elem_t:ty, $ufield:ident, $upb_tag:expr) => {
208 #[allow(dead_code)]
Derek Benson904266d2024-05-13 07:16:57 -0700209 #[inline]
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800210 fn repeated_new(_: Private) -> Repeated<$t> {
Protobuf Team Bot9ff062c2024-03-12 04:48:15 -0700211 let arena = Arena::new();
Derek Benson9c994242024-07-15 12:36:48 -0700212 Repeated::from_inner(
213 Private,
214 InnerRepeated { raw: unsafe { upb_Array_New(arena.raw(), $upb_tag) }, arena },
215 )
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800216 }
217 #[allow(dead_code)]
Protobuf Team Bot9ff062c2024-03-12 04:48:15 -0700218 unsafe fn repeated_free(_: Private, _f: &mut Repeated<$t>) {
219 // No-op: the memory will be dropped by the arena.
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800220 }
Derek Benson904266d2024-05-13 07:16:57 -0700221 #[inline]
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800222 fn repeated_len(f: View<Repeated<$t>>) -> usize {
223 unsafe { upb_Array_Size(f.as_raw(Private)) }
224 }
Derek Benson904266d2024-05-13 07:16:57 -0700225 #[inline]
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700226 fn repeated_push(mut f: Mut<Repeated<$t>>, v: impl IntoProxied<$t>) {
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800227 let arena = f.raw_arena(Private);
228 unsafe {
Protobuf Team Bot2678e102024-04-12 13:50:28 -0700229 assert!(upb_Array_Append(
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800230 f.as_raw(Private),
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700231 <$t as UpbTypeConversions>::into_message_value_fuse_if_required(
232 arena,
233 v.into_proxied(Private)
234 ),
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800235 arena,
Protobuf Team Bot2678e102024-04-12 13:50:28 -0700236 ));
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800237 }
238 }
Derek Benson904266d2024-05-13 07:16:57 -0700239 #[inline]
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800240 fn repeated_clear(mut f: Mut<Repeated<$t>>) {
241 unsafe {
242 upb_Array_Resize(f.as_raw(Private), 0, f.raw_arena(Private));
243 }
244 }
Derek Benson904266d2024-05-13 07:16:57 -0700245 #[inline]
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800246 unsafe fn repeated_get_unchecked(f: View<Repeated<$t>>, i: usize) -> View<$t> {
247 unsafe {
248 <$t as UpbTypeConversions>::from_message_value(upb_Array_Get(f.as_raw(Private), i))
249 }
250 }
Derek Benson904266d2024-05-13 07:16:57 -0700251 #[inline]
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700252 unsafe fn repeated_set_unchecked(
253 mut f: Mut<Repeated<$t>>,
254 i: usize,
255 v: impl IntoProxied<$t>,
256 ) {
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800257 let arena = f.raw_arena(Private);
258 unsafe {
259 upb_Array_Set(
260 f.as_raw(Private),
261 i,
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700262 <$t as UpbTypeConversions>::into_message_value_fuse_if_required(
263 arena,
264 v.into_proxied(Private),
265 ),
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800266 )
267 }
268 }
Derek Benson904266d2024-05-13 07:16:57 -0700269 #[inline]
Derek Bensonf2d8c2b2024-05-13 05:48:10 -0700270 fn repeated_reserve(mut f: Mut<Repeated<$t>>, additional: usize) {
271 // SAFETY:
272 // - `upb_Array_Reserve` is unsafe but assumed to be sound when called on a
273 // valid array.
274 unsafe {
275 let arena = f.raw_arena(Private);
276 let size = upb_Array_Size(f.as_raw(Private));
277 assert!(upb_Array_Reserve(f.as_raw(Private), size + additional, arena));
278 }
279 }
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800280 };
281}
282
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -0700283macro_rules! impl_repeated_primitives {
Marcel Hlopko39e8ca72024-01-19 07:00:32 -0800284 ($(($t:ty, $elem_t:ty, $ufield:ident, $upb_tag:expr)),* $(,)?) => {
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -0700285 $(
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800286 unsafe impl ProxiedInRepeated for $t {
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800287 impl_repeated_base!($t, $elem_t, $ufield, $upb_tag);
288
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800289 fn repeated_copy_from(src: View<Repeated<$t>>, mut dest: Mut<Repeated<$t>>) {
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800290 let arena = dest.raw_arena(Private);
Protobuf Team Bot0a13fde2023-11-15 17:04:27 -0800291 // SAFETY:
292 // - `upb_Array_Resize` is unsafe but assumed to be always sound to call.
293 // - `copy_nonoverlapping` is unsafe but here we guarantee that both pointers
294 // are valid, the pointers are `#[repr(u8)]`, and the size is correct.
295 unsafe {
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800296 if (!upb_Array_Resize(dest.as_raw(Private), src.len(), arena)) {
Protobuf Team Bot0a13fde2023-11-15 17:04:27 -0800297 panic!("upb_Array_Resize failed.");
298 }
299 ptr::copy_nonoverlapping(
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800300 upb_Array_DataPtr(src.as_raw(Private)).cast::<u8>(),
301 upb_Array_MutableDataPtr(dest.as_raw(Private)).cast::<u8>(),
Marcel Hlopko39e8ca72024-01-19 07:00:32 -0800302 size_of::<$elem_t>() * src.len());
Kevin King65cdac42023-10-23 14:50:42 -0700303 }
304 }
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -0700305 }
306 )*
307 }
308}
309
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800310macro_rules! impl_repeated_bytes {
311 ($(($t:ty, $upb_tag:expr)),* $(,)?) => {
312 $(
313 unsafe impl ProxiedInRepeated for $t {
314 impl_repeated_base!($t, PtrAndLen, str_val, $upb_tag);
315
Derek Benson904266d2024-05-13 07:16:57 -0700316 #[inline]
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800317 fn repeated_copy_from(src: View<Repeated<$t>>, mut dest: Mut<Repeated<$t>>) {
318 let len = src.len();
319 // SAFETY:
320 // - `upb_Array_Resize` is unsafe but assumed to be always sound to call.
321 // - `upb_Array` ensures its elements are never uninitialized memory.
322 // - The `DataPtr` and `MutableDataPtr` functions return pointers to spans
323 // of memory that are valid for at least `len` elements of PtrAndLen.
324 // - `copy_nonoverlapping` is unsafe but here we guarantee that both pointers
325 // are valid, the pointers are `#[repr(u8)]`, and the size is correct.
326 // - The bytes held within a valid array are valid.
327 unsafe {
328 let arena = ManuallyDrop::new(Arena::from_raw(dest.raw_arena(Private)));
329 if (!upb_Array_Resize(dest.as_raw(Private), src.len(), arena.raw())) {
330 panic!("upb_Array_Resize failed.");
331 }
332 let src_ptrs: &[PtrAndLen] = slice::from_raw_parts(
333 upb_Array_DataPtr(src.as_raw(Private)).cast(),
334 len
335 );
336 let dest_ptrs: &mut [PtrAndLen] = slice::from_raw_parts_mut(
337 upb_Array_MutableDataPtr(dest.as_raw(Private)).cast(),
338 len
339 );
340 for (src_ptr, dest_ptr) in src_ptrs.iter().zip(dest_ptrs) {
Protobuf Team Botb35d4ba2024-09-06 13:04:52 -0700341 *dest_ptr = arena.copy_slice_in(src_ptr.as_ref()).unwrap().into();
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800342 }
343 }
344 }
345 }
346 )*
347 }
348}
349
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700350impl<'msg, T> RepeatedMut<'msg, T> {
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800351 // Returns a `RawArena` which is live for at least `'msg`
352 #[doc(hidden)]
Alyssa Haroldsen3657e052024-02-05 12:51:41 -0800353 pub fn raw_arena(&mut self, _private: Private) -> RawArena {
Protobuf Team Bot9ff062c2024-03-12 04:48:15 -0700354 self.inner.arena.raw()
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800355 }
356}
357
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -0700358impl_repeated_primitives!(
Protobuf Team Bot2678e102024-04-12 13:50:28 -0700359 // proxied type, element type, upb_MessageValue field name, upb::CType variant
360 (bool, bool, bool_val, upb::CType::Bool),
361 (f32, f32, float_val, upb::CType::Float),
362 (f64, f64, double_val, upb::CType::Double),
363 (i32, i32, int32_val, upb::CType::Int32),
364 (u32, u32, uint32_val, upb::CType::UInt32),
365 (i64, i64, int64_val, upb::CType::Int64),
366 (u64, u64, uint64_val, upb::CType::UInt64),
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -0700367);
368
Marcel Hlopko6b7e8142024-06-20 05:54:09 -0700369impl_repeated_bytes!((ProtoString, upb::CType::String), (ProtoBytes, upb::CType::Bytes),);
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800370
Kevin Kingdf376c82024-01-17 11:41:53 -0800371/// Copy the contents of `src` into `dest`.
372///
373/// # Safety
374/// - `minitable` must be a pointer to the minitable for message `T`.
375pub unsafe fn repeated_message_copy_from<T: ProxiedInRepeated>(
376 src: View<Repeated<T>>,
377 mut dest: Mut<Repeated<T>>,
Protobuf Team Bot2678e102024-04-12 13:50:28 -0700378 minitable: *const upb_MiniTable,
Kevin Kingdf376c82024-01-17 11:41:53 -0800379) {
380 // SAFETY:
381 // - `src.as_raw()` is a valid `const upb_Array*`.
382 // - `dest.as_raw()` is a valid `upb_Array*`.
383 // - Elements of `src` and have message minitable `$minitable$`.
384 unsafe {
385 let size = upb_Array_Size(src.as_raw(Private));
386 if !upb_Array_Resize(dest.as_raw(Private), size, dest.raw_arena(Private)) {
387 panic!("upb_Array_Resize failed.");
388 }
389 for i in 0..size {
390 let src_msg = upb_Array_Get(src.as_raw(Private), i)
391 .msg_val
392 .expect("upb_Array* element should not be NULL");
393 // Avoid the use of `upb_Array_DeepClone` as it creates an
394 // entirely new `upb_Array*` at a new memory address.
395 let cloned_msg = upb_Message_DeepClone(src_msg, minitable, dest.raw_arena(Private))
396 .expect("upb_Message_DeepClone failed.");
397 upb_Array_Set(dest.as_raw(Private), i, upb_MessageValue { msg_val: Some(cloned_msg) });
398 }
399 }
400}
401
Alyssa Haroldsen0ce51da2023-12-20 10:08:11 -0800402/// Cast a `RepeatedView<SomeEnum>` to `RepeatedView<i32>`.
403pub fn cast_enum_repeated_view<E: Enum + ProxiedInRepeated>(
Alyssa Haroldsen0ce51da2023-12-20 10:08:11 -0800404 repeated: RepeatedView<E>,
405) -> RepeatedView<i32> {
406 // SAFETY: Reading an enum array as an i32 array is sound.
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700407 unsafe { RepeatedView::from_raw(Private, repeated.as_raw(Private)) }
Alyssa Haroldsen0ce51da2023-12-20 10:08:11 -0800408}
409
410/// Cast a `RepeatedMut<SomeEnum>` to `RepeatedMut<i32>`.
411///
412/// Writing an unknown value is sound because all enums
413/// are representationally open.
414pub fn cast_enum_repeated_mut<E: Enum + ProxiedInRepeated>(
Alyssa Haroldsen0ce51da2023-12-20 10:08:11 -0800415 repeated: RepeatedMut<E>,
416) -> RepeatedMut<i32> {
417 // SAFETY:
418 // - Reading an enum array as an i32 array is sound.
419 // - No shared mutation is possible through the output.
420 unsafe {
Alyssa Haroldsen3657e052024-02-05 12:51:41 -0800421 let InnerRepeatedMut { arena, raw, .. } = repeated.inner;
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700422 RepeatedMut::from_inner(Private, InnerRepeatedMut { arena, raw })
Alyssa Haroldsen0ce51da2023-12-20 10:08:11 -0800423 }
424}
425
Derek Bensonf2d8c2b2024-05-13 05:48:10 -0700426/// Cast a `RepeatedMut<SomeEnum>` to `RepeatedMut<i32>` and call
427/// repeated_reserve.
428pub fn reserve_enum_repeated_mut<E: Enum + ProxiedInRepeated>(
Derek Bensonf2d8c2b2024-05-13 05:48:10 -0700429 repeated: RepeatedMut<E>,
430 additional: usize,
431) {
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700432 let int_repeated = cast_enum_repeated_mut(repeated);
Derek Bensonf2d8c2b2024-05-13 05:48:10 -0700433 ProxiedInRepeated::repeated_reserve(int_repeated, additional);
434}
435
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700436pub fn new_enum_repeated<E: Enum + ProxiedInRepeated>() -> Repeated<E> {
Derek Benson9c994242024-07-15 12:36:48 -0700437 let arena = Arena::new();
438 // SAFETY:
439 // - `upb_Array_New` is unsafe but assumed to be sound when called on a valid
440 // arena.
441 unsafe {
442 let raw = upb_Array_New(arena.raw(), upb::CType::Int32);
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700443 Repeated::from_inner(Private, InnerRepeated::from_raw_parts(raw, arena))
Derek Benson9c994242024-07-15 12:36:48 -0700444 }
445}
446
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700447pub fn free_enum_repeated<E: Enum + ProxiedInRepeated>(_repeated: &mut Repeated<E>) {
Derek Benson9c994242024-07-15 12:36:48 -0700448 // No-op: the memory will be dropped by the arena.
449}
450
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800451/// Returns a static empty RepeatedView.
Protobuf Team Bota1c53e42024-07-22 11:35:04 -0700452pub fn empty_array<T: ProxiedInRepeated>() -> RepeatedView<'static, T> {
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800453 // TODO: Consider creating a static empty array in C.
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -0700454
Alyssa Haroldsen406fa602024-01-11 11:32:53 -0800455 // Use `i32` for a shared empty repeated for all repeated types in the program.
Adam Cozzettef712ca52024-07-16 12:57:33 -0700456 static EMPTY_REPEATED_VIEW: OnceLock<Repeated<i32>> = OnceLock::new();
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800457
458 // SAFETY:
459 // - Because the repeated is never mutated, the repeated type is unused and
460 // therefore valid for `T`.
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800461 unsafe {
462 RepeatedView::from_raw(
463 Private,
Adam Cozzettef712ca52024-07-16 12:57:33 -0700464 EMPTY_REPEATED_VIEW.get_or_init(Repeated::new).as_view().as_raw(Private),
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800465 )
466 }
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -0700467}
468
Jakob Buchgraber8d9e3e92024-01-12 00:39:31 -0800469/// Returns a static empty MapView.
470pub fn empty_map<K, V>() -> MapView<'static, K, V>
Jakob Buchgraber991a2f42024-01-08 05:25:58 -0800471where
Protobuf Team Bota1c53e42024-07-22 11:35:04 -0700472 K: Proxied,
473 V: ProxiedInMapValue<K>,
Jakob Buchgraber991a2f42024-01-08 05:25:58 -0800474{
Jakob Buchgraber8d9e3e92024-01-12 00:39:31 -0800475 // TODO: Consider creating a static empty map in C.
476
Marcel Hlopko73769a02024-02-12 00:46:04 -0800477 // Use `<bool, bool>` for a shared empty map for all map types.
478 //
479 // This relies on an implicit contract with UPB that it is OK to use an empty
480 // Map<bool, bool> as an empty map of all other types. The only const
481 // function on `upb_Map` that will care about the size of key or value is
482 // `get()` where it will hash the appropriate number of bytes of the
483 // provided `upb_MessageValue`, and that bool being the smallest type in the
484 // union means it will happen to work for all possible key types.
485 //
486 // If we used a larger key, then UPB would hash more bytes of the key than Rust
487 // initialized.
Adam Cozzettef712ca52024-07-16 12:57:33 -0700488 static EMPTY_MAP_VIEW: OnceLock<Map<bool, bool>> = OnceLock::new();
Jakob Buchgraber8d9e3e92024-01-12 00:39:31 -0800489
490 // SAFETY:
Marcel Hlopko73769a02024-02-12 00:46:04 -0800491 // - The map is empty and never mutated.
492 // - The value type is never used.
493 // - The size of the key type is used when `get()` computes the hash of the key.
494 // The map is empty, therefore it doesn't matter what hash is computed, but we
495 // have to use `bool` type as the smallest key possible (otherwise UPB would
496 // read more bytes than Rust allocated).
Jakob Buchgraber8d9e3e92024-01-12 00:39:31 -0800497 unsafe {
Adam Cozzettef712ca52024-07-16 12:57:33 -0700498 MapView::from_raw(Private, EMPTY_MAP_VIEW.get_or_init(Map::new).as_view().as_raw(Private))
Jakob Buchgraber991a2f42024-01-08 05:25:58 -0800499 }
Jakob Buchgraber991a2f42024-01-08 05:25:58 -0800500}
Jakob Buchgraber001be3c2023-11-16 01:36:28 -0800501
Alyssa Haroldsen3657e052024-02-05 12:51:41 -0800502impl<'msg, K: ?Sized, V: ?Sized> MapMut<'msg, K, V> {
503 // Returns a `RawArena` which is live for at least `'msg`
504 #[doc(hidden)]
505 pub fn raw_arena(&mut self, _private: Private) -> RawArena {
Protobuf Team Bot9ff062c2024-03-12 04:48:15 -0700506 self.inner.arena.raw()
507 }
508}
509
510#[derive(Debug)]
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700511#[doc(hidden)]
Protobuf Team Bot9ff062c2024-03-12 04:48:15 -0700512pub struct InnerMap {
513 pub(crate) raw: RawMap,
514 arena: Arena,
515}
516
517impl InnerMap {
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700518 pub fn new(raw: RawMap, arena: Arena) -> Self {
Protobuf Team Bot9ff062c2024-03-12 04:48:15 -0700519 Self { raw, arena }
520 }
521
522 pub fn as_mut(&mut self) -> InnerMapMut<'_> {
523 InnerMapMut { raw: self.raw, arena: &self.arena }
Alyssa Haroldsen3657e052024-02-05 12:51:41 -0800524 }
525}
526
Jakob Buchgraber8d9e3e92024-01-12 00:39:31 -0800527#[derive(Clone, Copy, Debug)]
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700528#[doc(hidden)]
Jakob Buchgraber8d9e3e92024-01-12 00:39:31 -0800529pub struct InnerMapMut<'msg> {
530 pub(crate) raw: RawMap,
Protobuf Team Bot9ff062c2024-03-12 04:48:15 -0700531 arena: &'msg Arena,
Jakob Buchgraber8d9e3e92024-01-12 00:39:31 -0800532}
Jakob Buchgraber991a2f42024-01-08 05:25:58 -0800533
Marcel Hlopko5842cc92024-02-09 03:25:00 -0800534#[doc(hidden)]
Jakob Buchgraber8d9e3e92024-01-12 00:39:31 -0800535impl<'msg> InnerMapMut<'msg> {
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700536 pub fn new(raw: RawMap, arena: &'msg Arena) -> Self {
Protobuf Team Bot9ff062c2024-03-12 04:48:15 -0700537 InnerMapMut { raw, arena }
Jakob Buchgraber991a2f42024-01-08 05:25:58 -0800538 }
Marcel Hlopko5842cc92024-02-09 03:25:00 -0800539
540 #[doc(hidden)]
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700541 pub fn as_raw(&self) -> RawMap {
Marcel Hlopko5842cc92024-02-09 03:25:00 -0800542 self.raw
543 }
544
Adam Cozzette7d619e82024-10-18 09:44:13 -0700545 pub fn arena(&mut self) -> &Arena {
546 self.arena
547 }
548
Marcel Hlopko5842cc92024-02-09 03:25:00 -0800549 #[doc(hidden)]
Adam Cozzette7d619e82024-10-18 09:44:13 -0700550 pub fn raw_arena(&mut self) -> RawArena {
Protobuf Team Bot9ff062c2024-03-12 04:48:15 -0700551 self.arena.raw()
Marcel Hlopko5842cc92024-02-09 03:25:00 -0800552 }
Jakob Buchgraber001be3c2023-11-16 01:36:28 -0800553}
554
Marcel Hlopko5842cc92024-02-09 03:25:00 -0800555pub trait UpbTypeConversions: Proxied {
Protobuf Team Bot2678e102024-04-12 13:50:28 -0700556 fn upb_type() -> upb::CType;
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700557
Jakob Buchgraber85972e52024-01-17 03:32:53 -0800558 fn to_message_value(val: View<'_, Self>) -> upb_MessageValue;
Jakob Buchgraber85972e52024-01-17 03:32:53 -0800559
560 /// # Safety
561 /// - `raw_arena` must point to a valid upb arena.
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700562 unsafe fn into_message_value_fuse_if_required(
Jakob Buchgraber85972e52024-01-17 03:32:53 -0800563 raw_arena: RawArena,
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700564 val: Self,
Jakob Buchgraber85972e52024-01-17 03:32:53 -0800565 ) -> upb_MessageValue;
566
567 /// # Safety
568 /// - `msg` must be the correct variant for `Self`.
569 /// - `msg` pointers must point to memory valid for `'msg` lifetime.
570 unsafe fn from_message_value<'msg>(msg: upb_MessageValue) -> View<'msg, Self>;
571}
572
573macro_rules! impl_upb_type_conversions_for_scalars {
574 ($($t:ty, $ufield:ident, $upb_tag:expr, $zero_val:literal;)*) => {
575 $(
576 impl UpbTypeConversions for $t {
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800577 #[inline(always)]
Protobuf Team Bot2678e102024-04-12 13:50:28 -0700578 fn upb_type() -> upb::CType {
Jakob Buchgraber85972e52024-01-17 03:32:53 -0800579 $upb_tag
580 }
581
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800582 #[inline(always)]
Jakob Buchgraber85972e52024-01-17 03:32:53 -0800583 fn to_message_value(val: View<'_, $t>) -> upb_MessageValue {
584 upb_MessageValue { $ufield: val }
585 }
586
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800587 #[inline(always)]
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700588 unsafe fn into_message_value_fuse_if_required(_: RawArena, val: $t) -> upb_MessageValue {
Jakob Buchgraber85972e52024-01-17 03:32:53 -0800589 Self::to_message_value(val)
590 }
591
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800592 #[inline(always)]
Jakob Buchgraber85972e52024-01-17 03:32:53 -0800593 unsafe fn from_message_value<'msg>(msg: upb_MessageValue) -> View<'msg, $t> {
594 unsafe { msg.$ufield }
595 }
596 }
597 )*
598 };
599}
600
601impl_upb_type_conversions_for_scalars!(
Protobuf Team Bot2678e102024-04-12 13:50:28 -0700602 f32, float_val, upb::CType::Float, 0f32;
603 f64, double_val, upb::CType::Double, 0f64;
604 i32, int32_val, upb::CType::Int32, 0i32;
605 u32, uint32_val, upb::CType::UInt32, 0u32;
606 i64, int64_val, upb::CType::Int64, 0i64;
607 u64, uint64_val, upb::CType::UInt64, 0u64;
608 bool, bool_val, upb::CType::Bool, false;
Jakob Buchgraber85972e52024-01-17 03:32:53 -0800609);
610
Marcel Hlopko6b7e8142024-06-20 05:54:09 -0700611impl UpbTypeConversions for ProtoBytes {
Protobuf Team Bot2678e102024-04-12 13:50:28 -0700612 fn upb_type() -> upb::CType {
613 upb::CType::Bytes
Jakob Buchgraber85972e52024-01-17 03:32:53 -0800614 }
615
Marcel Hlopko6b7e8142024-06-20 05:54:09 -0700616 fn to_message_value(val: View<'_, ProtoBytes>) -> upb_MessageValue {
Jakob Buchgraber85972e52024-01-17 03:32:53 -0800617 upb_MessageValue { str_val: val.into() }
618 }
619
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700620 unsafe fn into_message_value_fuse_if_required(
621 raw_parent_arena: RawArena,
622 val: ProtoBytes,
Jakob Buchgraber85972e52024-01-17 03:32:53 -0800623 ) -> upb_MessageValue {
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800624 // SAFETY: The arena memory is not freed due to `ManuallyDrop`.
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700625 let parent_arena = ManuallyDrop::new(unsafe { Arena::from_raw(raw_parent_arena) });
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700626
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700627 let (view, arena) = val.inner.into_raw_parts();
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700628 parent_arena.fuse(&arena);
629
Jakob Buchgraber08da9212024-07-09 06:59:28 -0700630 upb_MessageValue { str_val: view }
Jakob Buchgraber85972e52024-01-17 03:32:53 -0800631 }
632
Marcel Hlopko6b7e8142024-06-20 05:54:09 -0700633 unsafe fn from_message_value<'msg>(msg: upb_MessageValue) -> View<'msg, ProtoBytes> {
Jakob Buchgraber85972e52024-01-17 03:32:53 -0800634 unsafe { msg.str_val.as_ref() }
635 }
636}
637
Marcel Hlopko6b7e8142024-06-20 05:54:09 -0700638impl UpbTypeConversions for ProtoString {
Protobuf Team Bot2678e102024-04-12 13:50:28 -0700639 fn upb_type() -> upb::CType {
640 upb::CType::String
Jakob Buchgraber85972e52024-01-17 03:32:53 -0800641 }
642
Marcel Hlopko6b7e8142024-06-20 05:54:09 -0700643 fn to_message_value(val: View<'_, ProtoString>) -> upb_MessageValue {
Jakob Buchgraber85972e52024-01-17 03:32:53 -0800644 upb_MessageValue { str_val: val.as_bytes().into() }
645 }
646
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700647 unsafe fn into_message_value_fuse_if_required(
Jakob Buchgraber85972e52024-01-17 03:32:53 -0800648 raw_arena: RawArena,
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700649 val: ProtoString,
Jakob Buchgraber85972e52024-01-17 03:32:53 -0800650 ) -> upb_MessageValue {
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800651 // SAFETY: `raw_arena` is valid as promised by the caller
652 unsafe {
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700653 <ProtoBytes as UpbTypeConversions>::into_message_value_fuse_if_required(
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800654 raw_arena,
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700655 val.into(),
Alyssa Haroldsen3ccccdb2024-02-06 10:22:46 -0800656 )
657 }
Jakob Buchgraber85972e52024-01-17 03:32:53 -0800658 }
659
Marcel Hlopko6b7e8142024-06-20 05:54:09 -0700660 unsafe fn from_message_value<'msg>(msg: upb_MessageValue) -> View<'msg, ProtoString> {
Jakob Buchgraber85972e52024-01-17 03:32:53 -0800661 unsafe { ProtoStr::from_utf8_unchecked(msg.str_val.as_ref()) }
662 }
663}
664
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700665#[doc(hidden)]
Alyssa Haroldsen035d6ec2024-02-05 15:17:56 -0800666pub struct RawMapIter {
667 // TODO: Replace this `RawMap` with the const type.
668 map: RawMap,
669 iter: usize,
670}
671
672impl RawMapIter {
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700673 pub fn new(map: RawMap) -> Self {
Derek Benson1e1bf0d2024-07-26 11:07:22 -0700674 RawMapIter { map, iter: UPB_MAP_BEGIN }
Alyssa Haroldsen035d6ec2024-02-05 15:17:56 -0800675 }
676
677 /// # Safety
678 /// - `self.map` must be valid, and remain valid while the return value is
679 /// in use.
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700680 pub unsafe fn next_unchecked(&mut self) -> Option<(upb_MessageValue, upb_MessageValue)> {
Alyssa Haroldsen035d6ec2024-02-05 15:17:56 -0800681 let mut key = MaybeUninit::uninit();
682 let mut value = MaybeUninit::uninit();
683 // SAFETY: the `map` is valid as promised by the caller
684 unsafe { upb_Map_Next(self.map, key.as_mut_ptr(), value.as_mut_ptr(), &mut self.iter) }
685 // SAFETY: if upb_Map_Next returns true, then key and value have been populated.
686 .then(|| unsafe { (key.assume_init(), value.assume_init()) })
687 }
688}
689
Adam Cozzettec3df3592024-09-05 11:09:02 -0700690impl<Key, MessageType> ProxiedInMapValue<Key> for MessageType
691where
692 Key: Proxied + UpbTypeConversions,
Adam Cozzette11428382024-09-12 11:22:07 -0700693 MessageType: Proxied + UpbTypeConversions,
Adam Cozzettec3df3592024-09-05 11:09:02 -0700694{
695 fn map_new(_private: Private) -> Map<Key, Self> {
696 let arena = Arena::new();
697 let raw = unsafe {
698 upb_Map_New(
699 arena.raw(),
700 <Key as UpbTypeConversions>::upb_type(),
701 <Self as UpbTypeConversions>::upb_type(),
702 )
703 };
704
705 Map::from_inner(Private, InnerMap::new(raw, arena))
706 }
707
708 unsafe fn map_free(_private: Private, _map: &mut Map<Key, Self>) {
709 // No-op: the memory will be dropped by the arena.
710 }
711
712 fn map_clear(mut map: MapMut<Key, Self>) {
713 unsafe {
714 upb_Map_Clear(map.as_raw(Private));
715 }
716 }
717
718 fn map_len(map: MapView<Key, Self>) -> usize {
719 unsafe { upb_Map_Size(map.as_raw(Private)) }
720 }
721
722 fn map_insert(
723 mut map: MapMut<Key, Self>,
724 key: View<'_, Key>,
725 value: impl IntoProxied<Self>,
726 ) -> bool {
727 let arena = map.inner(Private).raw_arena();
728 unsafe {
729 upb_Map_InsertAndReturnIfInserted(
730 map.as_raw(Private),
731 <Key as UpbTypeConversions>::to_message_value(key),
732 <Self as UpbTypeConversions>::into_message_value_fuse_if_required(
733 arena,
734 value.into_proxied(Private),
735 ),
736 arena,
737 )
738 }
739 }
740
741 fn map_get<'a>(map: MapView<'a, Key, Self>, key: View<'_, Key>) -> Option<View<'a, Self>> {
742 let mut val = MaybeUninit::uninit();
743 let found = unsafe {
744 upb_Map_Get(
745 map.as_raw(Private),
746 <Key as UpbTypeConversions>::to_message_value(key),
747 val.as_mut_ptr(),
748 )
749 };
750 if !found {
751 return None;
752 }
753 Some(unsafe { <Self as UpbTypeConversions>::from_message_value(val.assume_init()) })
754 }
755
756 fn map_remove(mut map: MapMut<Key, Self>, key: View<'_, Key>) -> bool {
757 unsafe {
758 upb_Map_Delete(
759 map.as_raw(Private),
760 <Key as UpbTypeConversions>::to_message_value(key),
761 ptr::null_mut(),
762 )
763 }
764 }
765 fn map_iter(map: MapView<Key, Self>) -> MapIter<Key, Self> {
766 // SAFETY: MapView<'_,..>> guarantees its RawMap outlives '_.
767 unsafe { MapIter::from_raw(Private, RawMapIter::new(map.as_raw(Private))) }
768 }
769
770 fn map_iter_next<'a>(
771 iter: &mut MapIter<'a, Key, Self>,
772 ) -> Option<(View<'a, Key>, View<'a, Self>)> {
773 // SAFETY: MapIter<'a, ..> guarantees its RawMapIter outlives 'a.
774 unsafe { iter.as_raw_mut(Private).next_unchecked() }
775 // SAFETY: MapIter<K, V> returns key and values message values
776 // with the variants for K and V active.
777 .map(|(k, v)| unsafe {
778 (
779 <Key as UpbTypeConversions>::from_message_value(k),
780 <Self as UpbTypeConversions>::from_message_value(v),
781 )
782 })
783 }
784}
785
Alyssa Haroldsena48090e2024-02-01 14:21:41 -0800786/// `upb_Map_Insert`, but returns a `bool` for whether insert occurred.
787///
788/// Returns `true` if the entry was newly inserted.
789///
790/// # Panics
791/// Panics if the arena is out of memory.
792///
793/// # Safety
794/// The same as `upb_Map_Insert`:
795/// - `map` must be a valid map.
796/// - The `arena` must be valid and outlive the map.
797/// - The inserted value must outlive the map.
798#[allow(non_snake_case)]
799pub unsafe fn upb_Map_InsertAndReturnIfInserted(
800 map: RawMap,
801 key: upb_MessageValue,
802 value: upb_MessageValue,
803 arena: RawArena,
804) -> bool {
805 match unsafe { upb_Map_Insert(map, key, value, arena) } {
Protobuf Team Bot2678e102024-04-12 13:50:28 -0700806 upb::MapInsertStatus::Inserted => true,
807 upb::MapInsertStatus::Replaced => false,
808 upb::MapInsertStatus::OutOfMemory => panic!("map arena is out of memory"),
Alyssa Haroldsena48090e2024-02-01 14:21:41 -0800809 }
810}