Skip to content

Commit

Permalink
Change .submsg_mut() to return $Msg$Mut instead of FieldEntry<$Msg$>.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 615849358
  • Loading branch information
protobuf-github-bot authored and Copybara-Service committed Mar 14, 2024
1 parent 958dd59 commit f72c45f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
17 changes: 8 additions & 9 deletions rust/test/shared/accessors_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,36 +524,35 @@ fn test_message_opt_set() {
assert_that!(msg.optional_nested_message_opt().is_set(), eq(true));

msg.clear_optional_nested_message();
assert_that!(msg.optional_nested_message_mut().is_set(), eq(false));
assert_that!(msg.optional_nested_message_opt().is_set(), eq(false));
}

#[test]
fn test_setting_submsg() {
let mut msg = TestAllTypes::new();
let submsg = TestAllTypes_::NestedMessage::new();

let fieldentry = msg.optional_nested_message_mut();
assert_that!(fieldentry.is_set(), eq(false));
assert_that!(msg.optional_nested_message_opt().is_set(), eq(false));

fieldentry.or_default().set(submsg);
msg.set_optional_nested_message(submsg);
// confirm that invoking .set on a submsg indeed flips the set bit
assert_that!(msg.optional_nested_message_mut().is_set(), eq(true));
assert_that!(msg.optional_nested_message_opt().is_set(), eq(true));

msg.clear_optional_nested_message();
assert_that!(msg.optional_nested_message_mut().is_set(), eq(false));
assert_that!(msg.optional_nested_message_opt().is_set(), eq(false));
}

#[test]
fn test_msg_or_default() {
let mut msg = TestAllTypes::new();
assert_that!(msg.optional_nested_message_mut().is_set(), eq(false));
assert_that!(msg.optional_nested_message_opt().is_set(), eq(false));

let _ = msg.optional_nested_message_mut().or_default();
// confirm that that or_default makes the field Present
assert_that!(msg.optional_nested_message_mut().is_set(), eq(true));
assert_that!(msg.optional_nested_message_opt().is_set(), eq(true));

msg.clear_optional_nested_message();
assert_that!(msg.optional_nested_message_mut().is_set(), eq(false));
assert_that!(msg.optional_nested_message_opt().is_set(), eq(false));
}

#[test]
Expand Down
16 changes: 14 additions & 2 deletions src/google/protobuf/compiler/rust/accessors/singular_message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,18 @@ void SingularMessage::InMsgImpl(Context& ctx, const FieldDescriptor& field,
return;
}
ctx.Emit({}, R"rs(
pub fn $raw_field_name$_mut(&mut self)
pub fn $raw_field_name$_mut(&mut self) -> $msg_type$Mut<'_> {
self.$raw_field_name$_entry().or_default()
}
)rs");
}},
{"private_getter_entry",
[&] {
if (accessor_case == AccessorCase::VIEW) {
return;
}
ctx.Emit({}, R"rs(
fn $raw_field_name$_entry(&mut self)
-> $pb$::FieldEntry<'_, $msg_type$> {
static VTABLE: $pbr$::MessageVTable =
$pbr$::MessageVTable::new($pbi$::Private,
Expand Down Expand Up @@ -109,7 +120,7 @@ void SingularMessage::InMsgImpl(Context& ctx, const FieldDescriptor& field,
pub fn set_$raw_field_name$(&mut self, val: impl $pb$::SettableValue<$msg_type$>) {
//~ TODO: Optimize this to not go through the
//~ FieldEntry.
self.$raw_field_name$_mut().set(val);
self.$raw_field_name$_entry().set(val);
}
)rs");
}},
Expand All @@ -124,6 +135,7 @@ void SingularMessage::InMsgImpl(Context& ctx, const FieldDescriptor& field,
R"rs(
$getter$
$getter_mut$
$private_getter_entry$
$getter_opt$
$setter$
$clearer$
Expand Down
3 changes: 3 additions & 0 deletions src/google/protobuf/compiler/rust/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,9 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
Self{ inner: $pbr$::MutatorMessageRef::new(_private, msg) }
}
#[deprecated = "This .or_default() is a no-op, usages can be safely removed"]
pub fn or_default(self) -> Self { self }
fn raw_msg(&self) -> $pbi$::RawMessage {
self.inner.msg()
}
Expand Down

0 comments on commit f72c45f

Please sign in to comment.