Skip to content

Commit

Permalink
Add submsg support for bytes_mut
Browse files Browse the repository at this point in the history
message.cc:GetterForViewOrMut has been updated to support _mut for byte fields inside submsgs.

PiperOrigin-RevId: 590634540
  • Loading branch information
honglooker authored and copybara-github committed Dec 13, 2023
1 parent b19deb9 commit 54d4839
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
3 changes: 2 additions & 1 deletion rust/test/shared/simple_nested_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ fn test_nested_muts() {
})
);
assert_that!(inner_msg.string_mut().get(), eq(""));
assert_that!(inner_msg.bytes_mut().get(), eq(b""));

set_and_test_mut!(inner_msg =>
double_mut, 543.21;
Expand All @@ -95,8 +96,8 @@ fn test_nested_muts() {
fixed64_mut, 999;
bool_mut, true;
string_mut, "hi";
bytes_mut, b"bye";
);
// TODO: add mutation test for bytes
}

#[test]
Expand Down
42 changes: 34 additions & 8 deletions src/google/protobuf/compiler/rust/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,43 @@ void GetterForViewOrMut(Context<FieldDescriptor> field, bool is_mut) {
$maybe_mutator$
)rs");
} else if (fieldType == FieldDescriptor::TYPE_BYTES) {
field.Emit(
{
{"field", fieldName},
{"self", self},
{"getter_thunk", getter_thunk},
{"RsType", rsType},
},
R"rs(
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$> {
unsafe { $getter_thunk$($self$).as_ref() }
}
$maybe_mutator$
)rs");
} else {
field.Emit({{"field", fieldName},
Expand Down

0 comments on commit 54d4839

Please sign in to comment.