Add SettableValue<Msg> for owned Msg.

Right now this just does as_view().set_on(), but can be optimized to avoid a copy in a followup.

PiperOrigin-RevId: 602807430
pull/15645/head
Protobuf Team Bot 1 year ago committed by Copybara-Service
parent 3ef0fc7bd7
commit d2b2ef34ba
  1. 14
      rust/test/shared/accessors_test.rs
  2. 18
      src/google/protobuf/compiler/rust/message.cc

@ -9,7 +9,7 @@
use googletest::prelude::*;
use matchers::{is_set, is_unset};
use protobuf::Optional;
use protobuf::{MutProxy, Optional};
use unittest_proto::{TestAllTypes, TestAllTypes_};
#[test]
@ -668,16 +668,22 @@ fn test_nonempty_default_string_accessors() {
#[test]
fn test_singular_msg_field() {
use TestAllTypes_::{NestedMessageMut, NestedMessageView};
use TestAllTypes_::*;
let mut msg = TestAllTypes::new();
let msg_view: NestedMessageView = msg.optional_nested_message();
// testing reading an int inside a view
assert_that!(msg_view.bb(), eq(0));
let msg_mut: NestedMessageMut = msg.optional_nested_message_mut().or_default();
let mut nested_msg_mut: NestedMessageMut = msg.optional_nested_message_mut().or_default();
// test reading an int inside a mut
assert_that!(msg_mut.bb(), eq(0));
assert_that!(nested_msg_mut.bb(), eq(0));
// Test setting an owned NestedMessage onto another message.
let mut new_nested = NestedMessage::new();
new_nested.bb_mut().set(7);
nested_msg_mut.set(new_nested);
assert_that!(nested_msg_mut.bb(), eq(7));
}
#[test]

@ -192,7 +192,7 @@ void MessageDrop(Context& ctx, const Descriptor& msg) {
)rs");
}
void MessageSettableValue(Context& ctx, const Descriptor& msg) {
void MessageSettableValueForView(Context& ctx, const Descriptor& msg) {
switch (ctx.opts().kernel) {
case Kernel::kCpp:
ctx.Emit({{"copy_from_thunk", ThunkName(ctx, msg, "copy_from")}}, R"rs(
@ -519,7 +519,8 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
AccessorCase::MUT);
}
}},
{"settable_impl", [&] { MessageSettableValue(ctx, msg); }},
{"settable_impl_for_view",
[&] { MessageSettableValueForView(ctx, msg); }},
{"repeated_impl", [&] { MessageProxiedInRepeated(ctx, msg); }},
{"unwrap_upb",
[&] {
@ -655,7 +656,18 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
}
}
$settable_impl$
$settable_impl_for_view$
impl $pb$::SettableValue<$Msg$> for $Msg$ {
fn set_on<'dst>(
self, _private: $pbi$::Private, mutator: $pb$::Mut<'dst, $Msg$>)
where $Msg$: 'dst {
//~ TODO: b/320701507 - This current will copy the message and then
//~ drop it, this copy would be avoided on upb kernel.
self.as_view().set_on($pbi$::Private, mutator);
}
}
$repeated_impl$
#[derive(Debug)]

Loading…
Cancel
Save