Progress towards reusing the same accessor definitions on Msg+MsgMut+MsgView

This adds private methods of:
-- .raw_msg() to Msg+MsgMut+MsgView
-- .raw_arena() to Msg+MsgMut [upb kernel only]

And updates the accessors to use the self.raw_msg() / self.raw_arena().

A couple more things will need to be changed before the accessors can be verbatim reused in Msg/MsgView/MsgMut which will be mailed separately.

PiperOrigin-RevId: 598869392
pull/15430/head
Protobuf Team Bot 1 year ago committed by Copybara-Service
parent e4ae0c110f
commit deb2dff4e1
  1. 4
      rust/upb.rs
  2. 12
      src/google/protobuf/compiler/rust/accessors/map.cc
  3. 10
      src/google/protobuf/compiler/rust/accessors/repeated_scalar.cc
  4. 10
      src/google/protobuf/compiler/rust/accessors/singular_message.cc
  5. 8
      src/google/protobuf/compiler/rust/accessors/singular_scalar.cc
  6. 8
      src/google/protobuf/compiler/rust/accessors/singular_string.cc
  7. 68
      src/google/protobuf/compiler/rust/message.cc
  8. 4
      src/google/protobuf/compiler/rust/oneof.cc

@ -289,8 +289,8 @@ impl<'msg> MutatorMessageRef<'msg> {
self.msg
}
pub fn raw_arena(&self, _private: Private) -> RawArena {
self.arena.raw()
pub fn arena(&self, _private: Private) -> &Arena {
self.arena
}
}

@ -33,7 +33,7 @@ void Map::InMsgImpl(Context& ctx, const FieldDescriptor& field) const {
pub fn r#$field$(&self)
-> $pb$::MapView<'_, $Key$, $Value$> {
unsafe {
$getter_thunk$(self.inner.msg)
$getter_thunk$(self.raw_msg())
.map_or_else(
$pbr$::empty_map::<$Key$, $Value$>,
|raw| $pb$::MapView::from_raw($pbi$::Private, raw)
@ -46,7 +46,7 @@ void Map::InMsgImpl(Context& ctx, const FieldDescriptor& field) const {
-> $pb$::MapView<'_, $Key$, $Value$> {
unsafe {
$pb$::MapView::from_raw($pbi$::Private,
$getter_thunk$(self.inner.msg))
$getter_thunk$(self.raw_msg()))
}
})rs");
}
@ -58,11 +58,11 @@ void Map::InMsgImpl(Context& ctx, const FieldDescriptor& field) const {
pub fn r#$field$_mut(&mut self)
-> $pb$::MapMut<'_, $Key$, $Value$> {
let raw = unsafe {
$getter_mut_thunk$(self.inner.msg,
self.inner.arena.raw())
$getter_mut_thunk$(self.raw_msg(),
self.arena().raw())
};
let inner = $pbr$::InnerMapMut::new($pbi$::Private,
raw, self.inner.arena.raw());
raw, self.arena().raw());
unsafe { $pb$::MapMut::from_inner($pbi$::Private, inner) }
})rs");
} else {
@ -70,7 +70,7 @@ void Map::InMsgImpl(Context& ctx, const FieldDescriptor& field) const {
pub fn r#$field$_mut(&mut self)
-> $pb$::MapMut<'_, $Key$, $Value$> {
let inner = $pbr$::InnerMapMut::new($pbi$::Private,
unsafe { $getter_mut_thunk$(self.inner.msg) });
unsafe { $getter_mut_thunk$(self.raw_msg()) });
unsafe { $pb$::MapMut::from_inner($pbi$::Private, inner) }
})rs");
}

@ -30,7 +30,7 @@ void RepeatedScalar::InMsgImpl(Context& ctx,
pub fn r#$field$(&self) -> $pb$::RepeatedView<'_, $Scalar$> {
unsafe {
$getter_thunk$(
self.inner.msg,
self.raw_msg(),
/* optional size pointer */ std::ptr::null(),
) }
.map_or_else(
@ -47,7 +47,7 @@ void RepeatedScalar::InMsgImpl(Context& ctx,
unsafe {
$pb$::RepeatedView::from_raw(
$pbi$::Private,
unsafe { $getter_thunk$(self.inner.msg) },
unsafe { $getter_thunk$(self.raw_msg()) },
)
}
}
@ -66,9 +66,9 @@ void RepeatedScalar::InMsgImpl(Context& ctx,
$pbr$::InnerRepeatedMut::new(
$pbi$::Private,
$getter_mut_thunk$(
self.inner.msg,
self.raw_msg(),
/* optional size pointer */ std::ptr::null(),
self.inner.arena.raw(),
self.arena().raw(),
),
&self.inner.arena,
),
@ -84,7 +84,7 @@ void RepeatedScalar::InMsgImpl(Context& ctx,
$pbi$::Private,
$pbr$::InnerRepeatedMut::new(
$pbi$::Private,
$getter_mut_thunk$(self.inner.msg),
$getter_mut_thunk$(self.raw_msg()),
),
)
}

@ -35,7 +35,7 @@ void SingularMessage::InMsgImpl(Context& ctx,
[&] {
if (ctx.is_upb()) {
ctx.Emit({}, R"rs(
let submsg = unsafe { $getter_thunk$(self.inner.msg) };
let submsg = unsafe { $getter_thunk$(self.raw_msg()) };
//~ For upb, getters return null if the field is unset, so we need
//~ to check for null and return the default instance manually.
//~ Note that a nullptr received from upb manifests as Option::None
@ -50,7 +50,7 @@ void SingularMessage::InMsgImpl(Context& ctx,
ctx.Emit({}, R"rs(
//~ For C++ kernel, getters automatically return the
//~ default_instance if the field is unset.
let submsg = unsafe { $getter_thunk$(self.inner.msg) };
let submsg = unsafe { $getter_thunk$(self.raw_msg()) };
$msg_type$View::new($pbi$::Private, submsg)
)rs");
}
@ -61,13 +61,13 @@ void SingularMessage::InMsgImpl(Context& ctx,
if (ctx.is_upb()) {
ctx.Emit({}, R"rs(
let submsg = unsafe {
$getter_mut_thunk$(self.inner.msg, self.inner.arena.raw())
$getter_mut_thunk$(self.raw_msg(), self.arena().raw())
};
$msg_type$Mut::from_parent($pbi$::Private, &mut self.inner, submsg)
)rs");
} else {
ctx.Emit({}, R"rs(
let submsg = unsafe { $getter_mut_thunk$(self.inner.msg) };
let submsg = unsafe { $getter_mut_thunk$(self.raw_msg()) };
$msg_type$Mut::from_parent($pbi$::Private, &mut self.inner, submsg)
)rs");
}
@ -83,7 +83,7 @@ void SingularMessage::InMsgImpl(Context& ctx,
}
pub fn $field$_clear(&mut self) {
unsafe { $clearer_thunk$(self.inner.msg) }
unsafe { $clearer_thunk$(self.raw_msg()) }
}
)rs");
}

@ -32,7 +32,7 @@ void SingularScalar::InMsgImpl(Context& ctx,
[&] {
ctx.Emit({}, R"rs(
pub fn r#$field$(&self) -> $Scalar$ {
unsafe { $getter_thunk$(self.inner.msg) }
unsafe { $getter_thunk$(self.raw_msg()) }
}
)rs");
}},
@ -42,10 +42,10 @@ void SingularScalar::InMsgImpl(Context& ctx,
if (!field.has_presence()) return;
ctx.Emit({}, R"rs(
pub fn r#$field$_opt(&self) -> $pb$::Optional<$Scalar$> {
if !unsafe { $hazzer_thunk$(self.inner.msg) } {
if !unsafe { $hazzer_thunk$(self.raw_msg()) } {
return $pb$::Optional::Unset($default_value$);
}
let value = unsafe { $getter_thunk$(self.inner.msg) };
let value = unsafe { $getter_thunk$(self.raw_msg()) };
$pb$::Optional::Set(value)
}
)rs");
@ -68,7 +68,7 @@ void SingularScalar::InMsgImpl(Context& ctx,
);
unsafe {
let has = $hazzer_thunk$(self.inner.msg);
let has = $hazzer_thunk$(self.raw_msg());
$pbi$::new_vtable_field_entry::<$Scalar$>(
$pbi$::Private,
$pbr$::MutatorMessageRef::new($pbi$::Private, &mut self.inner),

@ -53,10 +53,10 @@ void SingularString::InMsgImpl(Context& ctx,
{"transform_view", transform_view}},
R"rs(
pub fn $field$_opt(&self) -> $pb$::Optional<&$proxied_type$> {
let view = unsafe { $getter_thunk$(self.inner.msg).as_ref() };
let view = unsafe { $getter_thunk$(self.raw_msg()).as_ref() };
$pb$::Optional::new(
$transform_view$ ,
unsafe { $hazzer_thunk$(self.inner.msg) }
unsafe { $hazzer_thunk$(self.raw_msg()) }
)
}
)rs");
@ -99,7 +99,7 @@ void SingularString::InMsgImpl(Context& ctx,
)
};
let out = unsafe {
let has = $hazzer_thunk$(self.inner.msg);
let has = $hazzer_thunk$(self.raw_msg());
$pbi$::new_vtable_field_entry(
$pbi$::Private,
$pbr$::MutatorMessageRef::new(
@ -142,7 +142,7 @@ void SingularString::InMsgImpl(Context& ctx,
},
R"rs(
pub fn r#$field$(&self) -> &$proxied_type$ {
let view = unsafe { $getter_thunk$(self.inner.msg).as_ref() };
let view = unsafe { $getter_thunk$(self.raw_msg()).as_ref() };
$transform_view$
}

@ -61,7 +61,7 @@ void MessageSerialize(Context& ctx, const Descriptor& msg) {
switch (ctx.opts().kernel) {
case Kernel::kCpp:
ctx.Emit({{"serialize_thunk", ThunkName(ctx, msg, "serialize")}}, R"rs(
unsafe { $serialize_thunk$(self.inner.msg) }
unsafe { $serialize_thunk$(self.raw_msg()) }
)rs");
return;
@ -70,7 +70,7 @@ void MessageSerialize(Context& ctx, const Descriptor& msg) {
let arena = $pbr$::Arena::new();
let mut len = 0;
unsafe {
let data = $serialize_thunk$(self.inner.msg, arena.raw(), &mut len);
let data = $serialize_thunk$(self.raw_msg(), arena.raw(), &mut len);
$pbr$::SerializedData::from_raw_parts(arena, data, len)
}
)rs");
@ -94,7 +94,7 @@ void MessageDeserialize(Context& ctx, const Descriptor& msg) {
data.len(),
);
$deserialize_thunk$(self.inner.msg, data)
$deserialize_thunk$(self.raw_msg(), data)
};
success.then_some(()).ok_or($pb$::ParseError)
)rs");
@ -174,7 +174,7 @@ void MessageDrop(Context& ctx, const Descriptor& msg) {
}
ctx.Emit({{"delete_thunk", ThunkName(ctx, msg, "delete")}}, R"rs(
unsafe { $delete_thunk$(self.inner.msg); }
unsafe { $delete_thunk$(self.raw_msg()); }
)rs");
}
@ -206,7 +206,7 @@ void MessageSettableValue(Context& ctx, const Descriptor& msg) {
mutator.inner.msg(),
self.msg,
$std$::ptr::addr_of!($minitable$),
mutator.inner.raw_arena($pbi$::Private),
mutator.inner.arena($pbi$::Private).raw(),
) };
}
}
@ -223,9 +223,6 @@ void GetterForViewOrMut(Context& ctx, const FieldDescriptor& field,
auto fieldType = field.type();
auto getter_thunk = ThunkName(ctx, field, "get");
auto setter_thunk = ThunkName(ctx, field, "set");
// If we're dealing with a Mut, the getter must be supplied
// self.inner.msg() whereas a View has to be supplied self.msg
auto self = is_mut ? "self.inner.msg()" : "self.msg";
if (fieldType == FieldDescriptor::TYPE_MESSAGE) {
const Descriptor& msg = *field.message_type();
@ -238,7 +235,6 @@ void GetterForViewOrMut(Context& ctx, const FieldDescriptor& field,
{
{"prefix", prefix},
{"field", fieldName},
{"self", self},
{"getter_thunk", getter_thunk},
// TODO: dedupe with singular_message.cc
{
@ -246,7 +242,7 @@ void GetterForViewOrMut(Context& ctx, const FieldDescriptor& field,
[&] {
if (ctx.is_upb()) {
ctx.Emit({}, R"rs(
let submsg = unsafe { $getter_thunk$($self$) };
let submsg = unsafe { $getter_thunk$(self.raw_msg()) };
match submsg {
None => $prefix$View::new($pbi$::Private,
$pbr$::ScratchSpace::zeroed_block($pbi$::Private)),
@ -255,7 +251,7 @@ void GetterForViewOrMut(Context& ctx, const FieldDescriptor& field,
)rs");
} else {
ctx.Emit({}, R"rs(
let submsg = unsafe { $getter_thunk$($self$) };
let submsg = unsafe { $getter_thunk$(self.raw_msg()) };
$prefix$View::new($pbi$::Private, submsg)
)rs");
}
@ -293,7 +289,6 @@ void GetterForViewOrMut(Context& ctx, const FieldDescriptor& field,
ctx.Emit({{"field", fieldName},
{"getter_thunk", getter_thunk},
{"setter_thunk", setter_thunk},
{"self", self},
{"RsType", rsType},
{"as_ref", asRef},
{"vtable", vtable},
@ -326,7 +321,7 @@ void GetterForViewOrMut(Context& ctx, const FieldDescriptor& field,
}}},
R"rs(
pub fn r#$field$(&self) -> $pb$::View<'_, $RsType$> {
let res = unsafe { $getter_thunk$($self$)$as_ref$ };
let res = unsafe { $getter_thunk$(self.raw_msg())$as_ref$ };
$string_transform$
}
@ -426,6 +421,26 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
} // mod $Msg$_
)rs");
}},
{"raw_arena_getter_for_message",
[&] {
if (ctx.is_upb()) {
ctx.Emit({}, R"rs(
fn arena(&self) -> &$pbr$::Arena {
&self.inner.arena
}
)rs");
}
}},
{"raw_arena_getter_for_msgmut",
[&] {
if (ctx.is_upb()) {
ctx.Emit({}, R"rs(
fn arena(&self) -> &$pbr$::Arena {
self.inner.arena($pbi$::Private)
}
)rs");
}
}},
{"accessor_fns_for_views",
[&] { AccessorsForViewOrMut(ctx, msg, false); }},
{"accessor_fns_for_muts",
@ -458,11 +473,17 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
_phantom: $Phantom$<&'a ()>,
}
#[allow(dead_code)]
impl<'a> $Msg$View<'a> {
#[doc(hidden)]
pub fn new(_private: $pbi$::Private, msg: $pbi$::RawMessage) -> Self {
Self { msg, _phantom: std::marker::PhantomData }
}
fn raw_msg(&self) -> $pbi$::RawMessage {
self.msg
}
$accessor_fns_for_views$
}
@ -495,6 +516,7 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
inner: $pbr$::MutatorMessageRef<'a>,
}
#[allow(dead_code)]
impl<'a> $Msg$Mut<'a> {
#[doc(hidden)]
pub fn from_parent(_private: $pbi$::Private,
@ -510,6 +532,13 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
pub fn new(_private: $pbi$::Private, msg: &'a mut $pbr$::MessageInner) -> Self {
Self{ inner: $pbr$::MutatorMessageRef::new(_private, msg) }
}
fn raw_msg(&self) -> $pbi$::RawMessage {
self.inner.msg()
}
$raw_arena_getter_for_msgmut$
$accessor_fns_for_muts$
}
@ -529,18 +558,25 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
impl<'a> $pb$::ViewProxy<'a> for $Msg$Mut<'a> {
type Proxied = $Msg$;
fn as_view(&self) -> $pb$::View<'_, $Msg$> {
$Msg$View { msg: self.inner.msg(), _phantom: std::marker::PhantomData }
$Msg$View { msg: self.raw_msg(), _phantom: std::marker::PhantomData }
}
fn into_view<'shorter>(self) -> $pb$::View<'shorter, $Msg$> where 'a: 'shorter {
$Msg$View { msg: self.inner.msg(), _phantom: std::marker::PhantomData }
$Msg$View { msg: self.raw_msg(), _phantom: std::marker::PhantomData }
}
}
#[allow(dead_code)]
impl $Msg$ {
pub fn new() -> Self {
$Msg::new$
}
fn raw_msg(&self) -> $pbi$::RawMessage {
self.inner.msg
}
$raw_arena_getter_for_message$
pub fn serialize(&self) -> $pbr$::SerializedData {
$Msg::serialize$
}
@ -588,7 +624,7 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
Self { inner: $pbr$::MessageInner { msg } }
}
pub fn __unstable_cpp_repr_grant_permission_to_break(&mut self) -> $pbi$::RawMessage {
self.inner.msg
self.raw_msg()
}
}
)rs");

@ -322,14 +322,14 @@ void GenerateOneofAccessors(Context& ctx, const OneofDescriptor& oneof) {
{"case_thunk", ThunkName(ctx, oneof, "case")}},
R"rs(
pub fn r#$oneof_name$(&self) -> $Msg$_::$view_enum_name$ {
match unsafe { $case_thunk$(self.inner.msg) } {
match unsafe { $case_thunk$(self.raw_msg()) } {
$view_cases$
_ => $Msg$_::$view_enum_name$::not_set(std::marker::PhantomData)
}
}
pub fn r#$oneof_name$_mut(&mut self) -> $Msg$_::$mut_enum_name$ {
match unsafe { $case_thunk$(self.inner.msg) } {
match unsafe { $case_thunk$(self.raw_msg()) } {
$mut_cases$
_ => $Msg$_::$mut_enum_name$::not_set(std::marker::PhantomData)
}

Loading…
Cancel
Save