Replace UpbTypeConversions::empty_message_value with MaybeUninit/null

The API doesn't require the pointees be initialized first, and the writes are predictable.

PiperOrigin-RevId: 609540475
pull/15910/head
Alyssa Haroldsen 11 months ago committed by Copybara-Service
parent dd27b6b877
commit c6af7f76b7
  1. 23
      rust/upb.rs
  2. 16
      src/google/protobuf/compiler/rust/message.cc

@ -752,7 +752,6 @@ impl<'msg> InnerMapMut<'msg> {
pub trait UpbTypeConversions: Proxied {
fn upb_type() -> UpbCType;
fn to_message_value(val: View<'_, Self>) -> upb_MessageValue;
fn empty_message_value() -> upb_MessageValue;
/// # Safety
/// - `raw_arena` must point to a valid upb arena.
@ -781,11 +780,6 @@ macro_rules! impl_upb_type_conversions_for_scalars {
upb_MessageValue { $ufield: val }
}
#[inline(always)]
fn empty_message_value() -> upb_MessageValue {
Self::to_message_value($zero_val)
}
#[inline(always)]
unsafe fn to_message_value_copy_if_required(_: RawArena, val: View<'_, $t>) -> upb_MessageValue {
Self::to_message_value(val)
@ -819,10 +813,6 @@ impl UpbTypeConversions for [u8] {
upb_MessageValue { str_val: val.into() }
}
fn empty_message_value() -> upb_MessageValue {
Self::to_message_value(b"")
}
unsafe fn to_message_value_copy_if_required(
raw_arena: RawArena,
val: View<'_, [u8]>,
@ -848,10 +838,6 @@ impl UpbTypeConversions for ProtoStr {
upb_MessageValue { str_val: val.as_bytes().into() }
}
fn empty_message_value() -> upb_MessageValue {
Self::to_message_value("".into())
}
unsafe fn to_message_value_copy_if_required(
raw_arena: RawArena,
val: View<'_, ProtoStr>,
@ -955,23 +941,22 @@ macro_rules! impl_ProxiedInMapValue_for_non_generated_value_types {
}
fn map_get<'a>(map: View<'a, Map<$key_t, Self>>, key: View<'_, $key_t>) -> Option<View<'a, Self>> {
let mut val = <$t as UpbTypeConversions>::empty_message_value();
let mut val = MaybeUninit::uninit();
let found = unsafe {
upb_Map_Get(map.as_raw(Private), <$key_t as UpbTypeConversions>::to_message_value(key),
&mut val)
val.as_mut_ptr())
};
if !found {
return None;
}
Some(unsafe { <$t as UpbTypeConversions>::from_message_value(val) })
Some(unsafe { <$t as UpbTypeConversions>::from_message_value(val.assume_init()) })
}
fn map_remove(mut map: Mut<'_, Map<$key_t, Self>>, key: View<'_, $key_t>) -> bool {
let mut val = <$t as UpbTypeConversions>::empty_message_value();
unsafe {
upb_Map_Delete(map.as_raw(Private),
<$key_t as UpbTypeConversions>::to_message_value(key),
&mut val)
ptr::null_mut())
}
}

@ -535,13 +535,6 @@ void MessageProxiedInMapValue(Context& ctx, const Descriptor& msg) {
$pbr$::upb_MessageValue { msg_val: Some(val.raw_msg()) }
}
fn empty_message_value() -> $pbr$::upb_MessageValue {
Self::to_message_value(
$Msg$View::new(
$pbi$::Private,
$pbr$::ScratchSpace::zeroed_block($pbi$::Private)))
}
unsafe fn to_message_value_copy_if_required(
arena: $pbi$::RawArena,
val: $pb$::View<'_, Self>) -> $pbr$::upb_MessageValue {
@ -620,26 +613,25 @@ void MessageProxiedInMapValue(Context& ctx, const Descriptor& msg) {
}
fn map_get<'a>(map: $pb$::View<'a, $pb$::Map<$key_t$, Self>>, key: $pb$::View<'_, $key_t$>) -> Option<$pb$::View<'a, Self>> {
let mut val = <Self as $pbr$::UpbTypeConversions>::empty_message_value();
let mut val = $std$::mem::MaybeUninit::uninit();
let found = unsafe {
$pbr$::upb_Map_Get(
map.as_raw($pbi$::Private),
<$key_t$ as $pbr$::UpbTypeConversions>::to_message_value(key),
&mut val)
val.as_mut_ptr())
};
if !found {
return None;
}
Some(unsafe { <Self as $pbr$::UpbTypeConversions>::from_message_value(val) })
Some(unsafe { <Self as $pbr$::UpbTypeConversions>::from_message_value(val.assume_init()) })
}
fn map_remove(mut map: $pb$::Mut<'_, $pb$::Map<$key_t$, Self>>, key: $pb$::View<'_, $key_t$>) -> bool {
let mut val = <Self as $pbr$::UpbTypeConversions>::empty_message_value();
unsafe {
$pbr$::upb_Map_Delete(
map.as_raw($pbi$::Private),
<$key_t$ as $pbr$::UpbTypeConversions>::to_message_value(key),
&mut val)
$std$::ptr::null_mut())
}
}
fn map_iter(map: $pb$::View<'_, $pb$::Map<$key_t$, Self>>) -> $pb$::MapIter<'_, $key_t$, Self> {

Loading…
Cancel
Save