Change .submsg_mut() to return $Msg$Mut instead of FieldEntry<$Msg$>.

PiperOrigin-RevId: 615849358
pull/16166/head
Protobuf Team Bot 10 months ago committed by Copybara-Service
parent 958dd59892
commit f72c45ff75
  1. 17
      rust/test/shared/accessors_test.rs
  2. 16
      src/google/protobuf/compiler/rust/accessors/singular_message.cc
  3. 3
      src/google/protobuf/compiler/rust/message.cc

@ -524,7 +524,7 @@ 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]
@ -532,28 +532,27 @@ 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]

@ -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,
@ -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");
}},
@ -124,6 +135,7 @@ void SingularMessage::InMsgImpl(Context& ctx, const FieldDescriptor& field,
R"rs(
$getter$
$getter_mut$
$private_getter_entry$
$getter_opt$
$setter$
$clearer$

@ -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()
}

Loading…
Cancel
Save