Skip to content

Commit

Permalink
Add submessage support for string_mut
Browse files Browse the repository at this point in the history
We augment the TYPE_STRING block inside message.cc:GetterForViewOrMut so that we properly return a Mut for strings when it comes to submessages.

PiperOrigin-RevId: 589944498
  • Loading branch information
honglooker authored and copybara-github committed Dec 11, 2023
1 parent 5b12bc8 commit e35cf10
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
5 changes: 3 additions & 2 deletions rust/test/shared/simple_nested_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ fn test_nested_muts() {
bool(): eq(false)
})
);
assert_that!(inner_msg.string_mut().get(), eq(""));

set_and_test_mut!(inner_msg =>
double_mut, 543.21;
Expand All @@ -93,9 +94,9 @@ fn test_nested_muts() {
fixed32_mut, 77;
fixed64_mut, 999;
bool_mut, true;

string_mut, "hi";
);
// TODO: add mutation tests for strings and bytes
// TODO: add mutation test for bytes
}

#[test]
Expand Down
49 changes: 38 additions & 11 deletions src/google/protobuf/compiler/rust/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ void GetterForViewOrMut(Context<FieldDescriptor> field, bool is_mut) {
// 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) {
Context<Descriptor> d = field.WithDesc(field.desc().message_type());
// TODO: support messages which are defined in other crates.
Expand Down Expand Up @@ -217,18 +218,44 @@ void GetterForViewOrMut(Context<FieldDescriptor> field, bool is_mut) {

auto rsType = PrimitiveRsTypeName(field.desc());
if (fieldType == FieldDescriptor::TYPE_STRING) {
field.Emit(
{
{"field", fieldName},
{"self", self},
{"getter_thunk", getter_thunk},
{"RsType", rsType},
},
R"rs(
pub fn r#$field$(&self) -> &$RsType$ {
field.Emit({{"field", fieldName},
{"self", self},
{"getter_thunk", getter_thunk},
{"setter_thunk", setter_thunk},
{"RsType", rsType},
{"maybe_mutator",
[&] {
if (is_mut) {
field.Emit({}, R"rs(
pub fn r#$field$_mut(&self) -> $pb$::Mut<'_, $RsType$> {
static VTABLE: $pbi$::BytesMutVTable =
$pbi$::BytesMutVTable::new(
$pbi$::Private,
$getter_thunk$,
$setter_thunk$,
);
unsafe {
<$pb$::Mut<$RsType$>>::from_inner(
$pbi$::Private,
$pbi$::RawVTableMutator::new(
$pbi$::Private,
self.inner,
&VTABLE,
)
)
}
}
)rs");
}
}}},
R"rs(
pub fn r#$field$(&self) -> $pb$::View<'_, $RsType$> {
let s = unsafe { $getter_thunk$($self$).as_ref() };
unsafe { __pb::ProtoStr::from_utf8_unchecked(s) }
}
$maybe_mutator$
)rs");
} else if (fieldType == FieldDescriptor::TYPE_BYTES) {
field.Emit(
Expand All @@ -239,7 +266,7 @@ void GetterForViewOrMut(Context<FieldDescriptor> field, bool is_mut) {
{"RsType", rsType},
},
R"rs(
pub fn r#$field$(&self) -> &$RsType$ {
pub fn r#$field$(&self) -> $pb$::View<'_, $RsType$> {
unsafe { $getter_thunk$($self$).as_ref() }
}
)rs");
Expand Down Expand Up @@ -275,7 +302,7 @@ void GetterForViewOrMut(Context<FieldDescriptor> field, bool is_mut) {
}
}}},
R"rs(
pub fn r#$field$(&self) -> $RsType$ {
pub fn r#$field$(&self) -> $pb$::View<'_, $RsType$> {
unsafe { $getter_thunk$($self$) }
}
Expand Down

0 comments on commit e35cf10

Please sign in to comment.