Clean up a few things in upb.rs
This CL does a few different things:
- Mark `link_mini_table()` as unsafe (this was an oversight from my previous CL)
- Remove some dead code, especially code related to repeated enum fields that
is no longer needed
- Remove `upb_Map_InsertAndReturnIfInserted()` and move its logic into its one
call site
PiperOrigin-RevId: 805852130
diff --git a/rust/upb.rs b/rust/upb.rs
index 76515da..2bd86f8 100644
--- a/rust/upb.rs
+++ b/rust/upb.rs
@@ -7,7 +7,7 @@
//! UPB FFI wrapper code for use by Rust Protobuf.
-use crate::__internal::{Enum, MatcherEq, Private, SealedInternal};
+use crate::__internal::{MatcherEq, Private, SealedInternal};
use crate::{
AsMut, AsView, Clear, ClearAndParse, CopyFrom, IntoProxied, Map, MapIter, MapMut, MapView,
MergeFrom, Message, MessageMut, MessageMutInterop, MessageView, MessageViewInterop, Mut,
@@ -86,7 +86,7 @@
/// # Safety
/// - All arguments must point to valid MiniTables.
-pub fn link_mini_table(
+pub unsafe fn link_mini_table(
mini_table: RawMiniTable,
submessages: &[RawMiniTable],
subenums: &[RawMiniTableEnum],
@@ -135,17 +135,6 @@
pub static THREAD_LOCAL_ARENA: ManuallyDrop<Arena> = ManuallyDrop::new(Arena::new());
}
-#[doc(hidden)]
-pub type SerializedData = upb::OwnedArenaBox<[u8]>;
-
-impl SealedInternal for SerializedData {}
-
-impl IntoProxied<ProtoBytes> for SerializedData {
- fn into_proxied(self, _private: Private) -> ProtoBytes {
- ProtoBytes { inner: InnerProtoString(self) }
- }
-}
-
#[derive(Debug)]
#[doc(hidden)]
pub struct OwnedMessageInner<T> {
@@ -534,55 +523,6 @@
}
}
-/// Cast a `RepeatedView<SomeEnum>` to `RepeatedView<i32>`.
-pub fn cast_enum_repeated_view<E: Enum + ProxiedInRepeated>(
- repeated: RepeatedView<E>,
-) -> RepeatedView<i32> {
- // SAFETY: Reading an enum array as an i32 array is sound.
- unsafe { RepeatedView::from_raw(Private, repeated.as_raw(Private)) }
-}
-
-/// Cast a `RepeatedMut<SomeEnum>` to `RepeatedMut<i32>`.
-///
-/// Writing an unknown value is sound because all enums
-/// are representationally open.
-pub fn cast_enum_repeated_mut<E: Enum + ProxiedInRepeated>(
- repeated: RepeatedMut<E>,
-) -> RepeatedMut<i32> {
- // SAFETY:
- // - Reading an enum array as an i32 array is sound.
- // - No shared mutation is possible through the output.
- unsafe {
- let InnerRepeatedMut { arena, raw, .. } = repeated.inner;
- RepeatedMut::from_inner(Private, InnerRepeatedMut { arena, raw })
- }
-}
-
-/// Cast a `RepeatedMut<SomeEnum>` to `RepeatedMut<i32>` and call
-/// repeated_reserve.
-pub fn reserve_enum_repeated_mut<E: Enum + ProxiedInRepeated>(
- repeated: RepeatedMut<E>,
- additional: usize,
-) {
- let int_repeated = cast_enum_repeated_mut(repeated);
- ProxiedInRepeated::repeated_reserve(int_repeated, additional);
-}
-
-pub fn new_enum_repeated<E: Enum + ProxiedInRepeated>() -> Repeated<E> {
- let arena = Arena::new();
- // SAFETY:
- // - `upb_Array_New` is unsafe but assumed to be sound when called on a valid
- // arena.
- unsafe {
- let raw = upb_Array_New(arena.raw(), upb::CType::Int32);
- Repeated::from_inner(Private, InnerRepeated::from_raw_parts(raw, arena))
- }
-}
-
-pub fn free_enum_repeated<E: Enum + ProxiedInRepeated>(_repeated: &mut Repeated<E>) {
- // No-op: the memory will be dropped by the arena.
-}
-
/// Returns a static empty RepeatedView.
pub fn empty_array<T: ProxiedInRepeated>() -> RepeatedView<'static, T> {
// TODO: Consider creating a static empty array in C.
@@ -1063,13 +1003,20 @@
value: impl IntoProxied<Self>,
) -> bool {
let arena = map.inner(Private).raw_arena();
- unsafe {
- upb_Map_InsertAndReturnIfInserted(
+ let insert_status = unsafe {
+ upb_Map_Insert(
map.as_raw(Private),
Key::to_message_value(key),
Self::into_message_value_fuse_if_required(arena, value.into_proxied(Private)),
arena,
)
+ };
+ match insert_status {
+ upb::MapInsertStatus::Inserted => true,
+ upb::MapInsertStatus::Replaced => false,
+ upb::MapInsertStatus::OutOfMemory => {
+ panic!("map insert failed (alloc should be infallible)")
+ }
}
}
@@ -1113,32 +1060,6 @@
}
}
-/// `upb_Map_Insert`, but returns a `bool` for whether insert occurred.
-///
-/// Returns `true` if the entry was newly inserted.
-///
-/// # Panics
-/// Panics if the arena is out of memory.
-///
-/// # Safety
-/// The same as `upb_Map_Insert`:
-/// - `map` must be a valid map.
-/// - The `arena` must be valid and outlive the map.
-/// - The inserted value must outlive the map.
-#[allow(non_snake_case)]
-pub unsafe fn upb_Map_InsertAndReturnIfInserted(
- map: RawMap,
- key: upb_MessageValue,
- value: upb_MessageValue,
- arena: RawArena,
-) -> bool {
- match unsafe { upb_Map_Insert(map, key, value, arena) } {
- upb::MapInsertStatus::Inserted => true,
- upb::MapInsertStatus::Replaced => false,
- upb::MapInsertStatus::OutOfMemory => panic!("map arena is out of memory"),
- }
-}
-
/// Internal-only trait to support blanket impls that need const access to raw messages
/// on codegen. Should never be used by application code.
#[doc(hidden)]