Stop using upb C accessor codegen for submsg accessors.

PiperOrigin-RevId: 668009483
pull/17964/head
Protobuf Team Bot 5 months ago committed by Copybara-Service
parent cdd3cf7ed4
commit e9b04b1f8f
  1. 102
      rust/upb/message.rs
  2. 26
      src/google/protobuf/compiler/rust/accessors/singular_message.cc

@ -24,7 +24,7 @@ extern "C" {
pub fn upb_Message_Clear(m: RawMessage, mini_table: *const upb_MiniTable); pub fn upb_Message_Clear(m: RawMessage, mini_table: *const upb_MiniTable);
/// # Safety /// # Safety
/// - `m` and `mini_table` must be valid to deref /// - `m` and `f` must be valid to deref
/// - `f` must be a field associated with `f` /// - `f` must be a field associated with `f`
pub fn upb_Message_ClearBaseField(m: RawMessage, f: *const upb_MiniTableField); pub fn upb_Message_ClearBaseField(m: RawMessage, f: *const upb_MiniTableField);
@ -49,17 +49,17 @@ extern "C" {
) -> Option<RawMessage>; ) -> Option<RawMessage>;
/// # Safety /// # Safety
/// - `m` and `mini_table` must be valid to deref /// - `m` and `f` must be valid to deref
/// - `f` must be a field associated with `f` /// - `f` must be a bool field associated with `m`
pub fn upb_Message_GetBool( pub fn upb_Message_GetBool(
m: RawMessage, m: RawMessage,
mini_table: *const upb_MiniTableField, f: *const upb_MiniTableField,
default_val: bool, default_val: bool,
) -> bool; ) -> bool;
/// # Safety /// # Safety
/// - `m` and `mini_table` must be valid to deref /// - `m` and `f` must be valid to deref
/// - `f` must be a field associated with `m` /// - `f` must be an i32 field associated with `m`
pub fn upb_Message_GetInt32( pub fn upb_Message_GetInt32(
m: RawMessage, m: RawMessage,
f: *const upb_MiniTableField, f: *const upb_MiniTableField,
@ -67,8 +67,8 @@ extern "C" {
) -> i32; ) -> i32;
/// # Safety /// # Safety
/// - `m` and `mini_table` must be valid to deref /// - `m` and `f` must be valid to deref
/// - `f` must be a field associated with `m` /// - `f` must be an i64 field associated with `m`
pub fn upb_Message_GetInt64( pub fn upb_Message_GetInt64(
m: RawMessage, m: RawMessage,
f: *const upb_MiniTableField, f: *const upb_MiniTableField,
@ -76,8 +76,8 @@ extern "C" {
) -> i64; ) -> i64;
/// # Safety /// # Safety
/// - `m` and `mini_table` must be valid to deref /// - `m` and `f` must be valid to deref
/// - `f` must be a field associated with `m` /// - `f` must be a u32 field associated with `m`
pub fn upb_Message_GetUInt32( pub fn upb_Message_GetUInt32(
m: RawMessage, m: RawMessage,
f: *const upb_MiniTableField, f: *const upb_MiniTableField,
@ -85,8 +85,8 @@ extern "C" {
) -> u32; ) -> u32;
/// # Safety /// # Safety
/// - `m` and `mini_table` must be valid to deref /// - `m` and `f` must be valid to deref
/// - `f` must be a field associated with `m` /// - `f` must be a u64 field associated with `m`
pub fn upb_Message_GetUInt64( pub fn upb_Message_GetUInt64(
m: RawMessage, m: RawMessage,
f: *const upb_MiniTableField, f: *const upb_MiniTableField,
@ -94,8 +94,8 @@ extern "C" {
) -> u64; ) -> u64;
/// # Safety /// # Safety
/// - `m` and `mini_table` must be valid to deref /// - `m` and `f` must be valid to deref
/// - `f` must be a field associated with `m` /// - `f` must be a f32 field associated with `m`
pub fn upb_Message_GetFloat( pub fn upb_Message_GetFloat(
m: RawMessage, m: RawMessage,
f: *const upb_MiniTableField, f: *const upb_MiniTableField,
@ -103,8 +103,8 @@ extern "C" {
) -> f32; ) -> f32;
/// # Safety /// # Safety
/// - `m` and `mini_table` must be valid to deref /// - `m` and `f` must be valid to deref
/// - `f` must be a field associated with `m` /// - `f` must be a f64 field associated with `m`
pub fn upb_Message_GetDouble( pub fn upb_Message_GetDouble(
m: RawMessage, m: RawMessage,
f: *const upb_MiniTableField, f: *const upb_MiniTableField,
@ -112,15 +112,34 @@ extern "C" {
) -> f64; ) -> f64;
/// # Safety /// # Safety
/// - `m` and `mini_table` must be valid to deref /// - `m` and `f` must be valid to deref
/// - `f` must be a message-typed field associated with `m`
pub fn upb_Message_GetMessage(
m: RawMessage,
f: *const upb_MiniTableField,
) -> Option<RawMessage>;
/// # Safety
/// - All arguments must be valid to deref
/// - `mini_table` must be the MiniTable associated with `m`
/// - `f` must be a message-typed field associated with `m`
pub fn upb_Message_GetOrCreateMutableMessage(
m: RawMessage,
mini_table: *const upb_MiniTable,
f: *const upb_MiniTableField,
arena: RawArena,
) -> Option<RawMessage>;
/// # Safety
/// - `m` and `f` must be valid to deref
/// - `mini_table` must be the MiniTable associated with `m` /// - `mini_table` must be the MiniTable associated with `m`
pub fn upb_Message_HasBaseField(m: RawMessage, mini_table: *const upb_MiniTableField) -> bool; pub fn upb_Message_HasBaseField(m: RawMessage, f: *const upb_MiniTableField) -> bool;
/// # Safety /// # Safety
/// - `m` and `mini_table` must be valid to deref /// - `m` and `f` must be valid to deref
/// - `f` must be a field associated with `m` /// - `f` must be a field associated with `m`
/// - `val` must be a pointer to legally readable memory of the correct type /// - `val` must be a pointer to legally readable memory of the correct type
/// for the field described by `mini_table` /// for the field described by `f`
pub fn upb_Message_SetBaseField( pub fn upb_Message_SetBaseField(
m: RawMessage, m: RawMessage,
f: *const upb_MiniTableField, f: *const upb_MiniTableField,
@ -151,41 +170,46 @@ extern "C" {
) -> bool; ) -> bool;
/// # Safety /// # Safety
/// - `m` and `mini_table` must be valid to deref /// - `m` and `f` must be valid to deref
/// - `f` must be a field associated with `f` /// - `f` must be a bool field associated with `f`
pub fn upb_Message_SetBaseFieldBool( pub fn upb_Message_SetBaseFieldBool(m: RawMessage, f: *const upb_MiniTableField, val: bool);
m: RawMessage,
mini_table: *const upb_MiniTableField,
val: bool,
);
/// # Safety /// # Safety
/// - `m` and `mini_table` must be valid to deref /// - `m` and `f` must be valid to deref
/// - `f` must be a field associated with `m` /// - `f` must be an i32 field associated with `m`
pub fn upb_Message_SetBaseFieldInt32(m: RawMessage, f: *const upb_MiniTableField, val: i32); pub fn upb_Message_SetBaseFieldInt32(m: RawMessage, f: *const upb_MiniTableField, val: i32);
/// # Safety /// # Safety
/// - `m` and `mini_table` must be valid to deref /// - `m` and `f` must be valid to deref
/// - `f` must be a field associated with `m` /// - `f` must be an i64 field associated with `m`
pub fn upb_Message_SetBaseFieldInt64(m: RawMessage, f: *const upb_MiniTableField, val: i64); pub fn upb_Message_SetBaseFieldInt64(m: RawMessage, f: *const upb_MiniTableField, val: i64);
/// # Safety /// # Safety
/// - `m` and `mini_table` must be valid to deref /// - `m` and `f` must be valid to deref
/// - `f` must be a field associated with `m` /// - `f` must be a u32 field associated with `m`
pub fn upb_Message_SetBaseFieldUInt32(m: RawMessage, f: *const upb_MiniTableField, val: u32); pub fn upb_Message_SetBaseFieldUInt32(m: RawMessage, f: *const upb_MiniTableField, val: u32);
/// # Safety /// # Safety
/// - `m` and `mini_table` must be valid to deref /// - `m` and `f` must be valid to deref
/// - `f` must be a field associated with `m` /// - `f` must be a u64 field associated with `m`
pub fn upb_Message_SetBaseFieldUInt64(m: RawMessage, f: *const upb_MiniTableField, val: u64); pub fn upb_Message_SetBaseFieldUInt64(m: RawMessage, f: *const upb_MiniTableField, val: u64);
/// # Safety /// # Safety
/// - `m` and `mini_table` must be valid to deref /// - `m` and `f` must be valid to deref
/// - `f` must be a field associated with `m` /// - `f` must be an f32 field associated with `m`
pub fn upb_Message_SetBaseFieldFloat(m: RawMessage, f: *const upb_MiniTableField, val: f32); pub fn upb_Message_SetBaseFieldFloat(m: RawMessage, f: *const upb_MiniTableField, val: f32);
/// # Safety /// # Safety
/// - `m` and `mini_table` must be valid to deref /// - `m` and `f` must be valid to deref
/// - `f` must be a field associated with `m` /// - `f` must be an f64 field associated with `m`
pub fn upb_Message_SetBaseFieldDouble(m: RawMessage, f: *const upb_MiniTableField, val: f64); pub fn upb_Message_SetBaseFieldDouble(m: RawMessage, f: *const upb_MiniTableField, val: f64);
/// # Safety
/// - `m` and `f` must be valid to deref
/// - `f` must be a message-typed field associated with `m`
pub fn upb_Message_SetBaseFieldMessage(
m: RawMessage,
f: *const upb_MiniTableField,
val: RawMessage,
);
} }

@ -7,6 +7,7 @@
#include <string> #include <string>
#include "absl/log/absl_check.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "google/protobuf/compiler/cpp/helpers.h" #include "google/protobuf/compiler/cpp/helpers.h"
#include "google/protobuf/compiler/rust/accessors/accessor_case.h" #include "google/protobuf/compiler/rust/accessors/accessor_case.h"
@ -14,6 +15,7 @@
#include "google/protobuf/compiler/rust/accessors/with_presence.h" #include "google/protobuf/compiler/rust/accessors/with_presence.h"
#include "google/protobuf/compiler/rust/context.h" #include "google/protobuf/compiler/rust/context.h"
#include "google/protobuf/compiler/rust/naming.h" #include "google/protobuf/compiler/rust/naming.h"
#include "google/protobuf/compiler/rust/upb_helpers.h"
#include "google/protobuf/descriptor.h" #include "google/protobuf/descriptor.h"
namespace google { namespace google {
@ -40,19 +42,25 @@ void SingularMessage::InMsgImpl(Context& ctx, const FieldDescriptor& field,
{"getter_thunk", ThunkName(ctx, field, "get")}, {"getter_thunk", ThunkName(ctx, field, "get")},
{"getter_mut_thunk", ThunkName(ctx, field, "get_mut")}, {"getter_mut_thunk", ThunkName(ctx, field, "get_mut")},
{"set_allocated_thunk", ThunkName(ctx, field, "set")}, {"set_allocated_thunk", ThunkName(ctx, field, "set")},
{"upb_mt_field_index", UpbMiniTableFieldIndex(field)},
{ {
"getter_body", "getter_body",
[&] { [&] {
if (ctx.is_upb()) { if (ctx.is_upb()) {
ctx.Emit({}, R"rs( ctx.Emit({}, R"rs(
let submsg = unsafe { $getter_thunk$(self.raw_msg()) }; let submsg = unsafe {
let f = $pbr$::upb_MiniTable_GetFieldByIndex(
<Self as $pbr$::AssociatedMiniTable>::mini_table(),
$upb_mt_field_index$);
$pbr$::upb_Message_GetMessage(self.raw_msg(), f)
};
//~ For upb, getters return null if the field is unset, so we need //~ For upb, getters return null if the field is unset, so we need
//~ to check for null and return the default instance manually. //~ to check for null and return the default instance manually.
//~ Note that a nullptr received from upb manifests as Option::None //~ Note that a nullptr received from upb manifests as Option::None
match submsg { match submsg {
//~ TODO:(b/304357029) //~ TODO:(b/304357029)
None => $msg_type$View::new($pbi$::Private, $pbr$::ScratchSpace::zeroed_block()), None => $msg_type$View::new($pbi$::Private, $pbr$::ScratchSpace::zeroed_block()),
Some(field) => $msg_type$View::new($pbi$::Private, field), Some(sub_raw_msg) => $msg_type$View::new($pbi$::Private, sub_raw_msg),
} }
)rs"); )rs");
} else { } else {
@ -84,7 +92,10 @@ void SingularMessage::InMsgImpl(Context& ctx, const FieldDescriptor& field,
} else { } else {
ctx.Emit({}, R"rs( ctx.Emit({}, R"rs(
let raw_msg = unsafe { let raw_msg = unsafe {
$getter_mut_thunk$(self.raw_msg(), self.arena().raw()) let mt = <Self as $pbr$::AssociatedMiniTable>::mini_table();
let f = $pbr$::upb_MiniTable_GetFieldByIndex(mt, $upb_mt_field_index$);
$pbr$::upb_Message_GetOrCreateMutableMessage(
self.raw_msg(), mt, f, self.arena().raw()).unwrap()
}; };
$msg_type$Mut::from_parent($pbi$::Private, $msg_type$Mut::from_parent($pbi$::Private,
self.as_mutator_message_ref($pbi$::Private), raw_msg) self.as_mutator_message_ref($pbi$::Private), raw_msg)
@ -116,7 +127,12 @@ void SingularMessage::InMsgImpl(Context& ctx, const FieldDescriptor& field,
.fuse(msg.as_mutator_message_ref($pbi$::Private).arena()); .fuse(msg.as_mutator_message_ref($pbi$::Private).arena());
unsafe { unsafe {
$set_allocated_thunk$(self.as_mutator_message_ref($pbi$::Private).msg(), let f = $pbr$::upb_MiniTable_GetFieldByIndex(
<Self as $pbr$::AssociatedMiniTable>::mini_table(),
$upb_mt_field_index$);
$pbr$::upb_Message_SetBaseFieldMessage(
self.as_mutator_message_ref($pbi$::Private).msg(),
f,
msg.as_mutator_message_ref($pbi$::Private).msg()); msg.as_mutator_message_ref($pbi$::Private).msg());
} }
)rs"); )rs");
@ -153,6 +169,7 @@ void SingularMessage::InMsgImpl(Context& ctx, const FieldDescriptor& field,
void SingularMessage::InExternC(Context& ctx, void SingularMessage::InExternC(Context& ctx,
const FieldDescriptor& field) const { const FieldDescriptor& field) const {
if (ctx.is_upb()) return;
if (field.has_presence()) { if (field.has_presence()) {
WithPresenceAccessorsInExternC(ctx, field); WithPresenceAccessorsInExternC(ctx, field);
} }
@ -198,6 +215,7 @@ void SingularMessage::InExternC(Context& ctx,
void SingularMessage::InThunkCc(Context& ctx, void SingularMessage::InThunkCc(Context& ctx,
const FieldDescriptor& field) const { const FieldDescriptor& field) const {
ABSL_CHECK(ctx.is_cpp());
if (field.has_presence()) { if (field.has_presence()) {
WithPresenceAccessorsInThunkCc(ctx, field); WithPresenceAccessorsInThunkCc(ctx, field);
} }

Loading…
Cancel
Save