Simplify format variables in singular_string

PiperOrigin-RevId: 599335169
pull/15477/head
Alyssa Haroldsen 1 year ago committed by Copybara-Service
parent c1bb1981a4
commit 32c17c0919
  1. 54
      src/google/protobuf/compiler/rust/accessors/singular_string.cc

@ -23,39 +23,32 @@ namespace rust {
void SingularString::InMsgImpl(Context& ctx, const FieldDescriptor& field, void SingularString::InMsgImpl(Context& ctx, const FieldDescriptor& field,
AccessorCase accessor_case) const { AccessorCase accessor_case) const {
std::string hazzer_thunk = ThunkName(ctx, field, "has");
std::string getter_thunk = ThunkName(ctx, field, "get");
std::string setter_thunk = ThunkName(ctx, field, "set");
std::string proxied_type = RsTypePath(ctx, field);
auto transform_view = [&] {
if (field.type() == FieldDescriptor::TYPE_STRING) {
ctx.Emit(R"rs(
// SAFETY: The runtime doesn't require ProtoStr to be UTF-8.
unsafe { $pb$::ProtoStr::from_utf8_unchecked(view) }
)rs");
} else {
ctx.Emit("view");
}
};
ctx.Emit( ctx.Emit(
{ {
{"field", RsSafeName(field.name())}, {"field", RsSafeName(field.name())},
{"hazzer_thunk", hazzer_thunk}, {"hazzer_thunk", ThunkName(ctx, field, "has")},
{"getter_thunk", getter_thunk}, {"getter_thunk", ThunkName(ctx, field, "get")},
{"setter_thunk", setter_thunk}, {"setter_thunk", ThunkName(ctx, field, "set")},
{"proxied_type", proxied_type}, {"clearer_thunk", ThunkName(ctx, field, "clear")},
{"transform_view", transform_view}, {"proxied_type", RsTypePath(ctx, field)},
{"transform_view",
[&] {
if (field.type() == FieldDescriptor::TYPE_STRING) {
ctx.Emit(R"rs(
// SAFETY: The runtime doesn't require ProtoStr to be UTF-8.
unsafe { $pb$::ProtoStr::from_utf8_unchecked(view) }
)rs");
} else {
ctx.Emit("view");
}
}},
{"view_lifetime", ViewLifetime(accessor_case)}, {"view_lifetime", ViewLifetime(accessor_case)},
{"view_self", ViewReceiver(accessor_case)}, {"view_self", ViewReceiver(accessor_case)},
{"field_optional_getter", {"field_optional_getter",
[&] { [&] {
if (!field.is_optional()) return; if (!field.is_optional()) return;
if (!field.has_presence()) return; if (!field.has_presence()) return;
ctx.Emit({{"hazzer_thunk", hazzer_thunk}, ctx.Emit(R"rs(
{"getter_thunk", getter_thunk},
{"transform_view", transform_view}},
R"rs(
pub fn $field$_opt($view_self$) -> $pb$::Optional<&$view_lifetime$ $proxied_type$> { pub fn $field$_opt($view_self$) -> $pb$::Optional<&$view_lifetime$ $proxied_type$> {
let view = unsafe { $getter_thunk$(self.raw_msg()).as_ref() }; let view = unsafe { $getter_thunk$(self.raw_msg()).as_ref() };
$pb$::Optional::new( $pb$::Optional::new(
@ -73,10 +66,7 @@ void SingularString::InMsgImpl(Context& ctx, const FieldDescriptor& field,
if (field.has_presence()) { if (field.has_presence()) {
ctx.Emit( ctx.Emit(
{ {
{"field", RsSafeName(field.name())},
{"proxied_type", proxied_type},
{"default_val", DefaultValue(ctx, field)}, {"default_val", DefaultValue(ctx, field)},
{"view_type", proxied_type},
{"transform_field_entry", {"transform_field_entry",
[&] { [&] {
if (field.type() == FieldDescriptor::TYPE_STRING) { if (field.type() == FieldDescriptor::TYPE_STRING) {
@ -89,10 +79,6 @@ void SingularString::InMsgImpl(Context& ctx, const FieldDescriptor& field,
ctx.Emit("out"); ctx.Emit("out");
} }
}}, }},
{"hazzer_thunk", hazzer_thunk},
{"getter_thunk", getter_thunk},
{"setter_thunk", setter_thunk},
{"clearer_thunk", ThunkName(ctx, field, "clear")},
}, },
R"rs( R"rs(
pub fn $field$_mut(&mut self) -> $pb$::FieldEntry<'_, $proxied_type$> { pub fn $field$_mut(&mut self) -> $pb$::FieldEntry<'_, $proxied_type$> {
@ -118,11 +104,7 @@ void SingularString::InMsgImpl(Context& ctx, const FieldDescriptor& field,
} }
)rs"); )rs");
} else { } else {
ctx.Emit({{"field", RsSafeName(field.name())}, ctx.Emit(R"rs(
{"proxied_type", proxied_type},
{"getter_thunk", getter_thunk},
{"setter_thunk", setter_thunk}},
R"rs(
pub fn $field$_mut(&mut self) -> $pb$::Mut<'_, $proxied_type$> { pub fn $field$_mut(&mut self) -> $pb$::Mut<'_, $proxied_type$> {
static VTABLE: $pbi$::BytesMutVTable = static VTABLE: $pbi$::BytesMutVTable =
$pbi$::BytesMutVTable::new( $pbi$::BytesMutVTable::new(

Loading…
Cancel
Save