Add `Msg::as_{view,mut}()`

PiperOrigin-RevId: 597969809
pull/15429/head
Kevin King 1 year ago committed by Copybara-Service
parent a37522001f
commit 7b42f1c08b
  1. 12
      rust/test/shared/accessors_test.rs
  2. 4
      src/google/protobuf/compiler/rust/accessors/singular_message.cc
  3. 14
      src/google/protobuf/compiler/rust/message.cc

@ -10,7 +10,7 @@
use googletest::prelude::*; use googletest::prelude::*;
use matchers::{is_set, is_unset}; use matchers::{is_set, is_unset};
use protobuf::Optional; use protobuf::Optional;
use unittest_proto::proto2_unittest::{NestedTestAllTypes, TestAllTypes, TestAllTypes_}; use unittest_proto::proto2_unittest::{TestAllTypes, TestAllTypes_};
#[test] #[test]
fn test_default_accessors() { fn test_default_accessors() {
@ -948,11 +948,11 @@ fn test_oneof_default_mut_accessors() {
#[test] #[test]
fn test_set_message_from_view() { fn test_set_message_from_view() {
use protobuf::MutProxy; use protobuf::MutProxy;
let mut m1 = NestedTestAllTypes::new();
let mut m2 = NestedTestAllTypes::new(); let mut m1 = TestAllTypes::new();
m2.payload_mut().optional_int32_mut().set(1); m1.optional_int32_mut().set(1);
let mut m2 = TestAllTypes::new();
m2.as_mut().set(m1.as_view());
m1.payload_mut().set(m2.payload()); assert_that!(m2.optional_int32(), eq(1i32));
assert_that!(m1.payload().optional_int32(), eq(1));
} }

@ -63,12 +63,12 @@ void SingularMessage::InMsgImpl(Context& ctx,
let submsg = unsafe { let submsg = unsafe {
$getter_mut_thunk$(self.inner.msg, self.inner.arena.raw()) $getter_mut_thunk$(self.inner.msg, self.inner.arena.raw())
}; };
$msg_type$Mut::new($pbi$::Private, &mut self.inner, submsg) $msg_type$Mut::from_parent($pbi$::Private, &mut self.inner, submsg)
)rs"); )rs");
} else { } else {
ctx.Emit({}, R"rs( ctx.Emit({}, R"rs(
let submsg = unsafe { $getter_mut_thunk$(self.inner.msg) }; let submsg = unsafe { $getter_mut_thunk$(self.inner.msg) };
$msg_type$Mut::new($pbi$::Private, &mut self.inner, submsg) $msg_type$Mut::from_parent($pbi$::Private, &mut self.inner, submsg)
)rs"); )rs");
} }
}}, }},

@ -497,7 +497,7 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
impl<'a> $Msg$Mut<'a> { impl<'a> $Msg$Mut<'a> {
#[doc(hidden)] #[doc(hidden)]
pub fn new(_private: $pbi$::Private, pub fn from_parent(_private: $pbi$::Private,
parent: &'a mut $pbr$::MessageInner, parent: &'a mut $pbr$::MessageInner,
msg: $pbi$::RawMessage) msg: $pbi$::RawMessage)
-> Self { -> Self {
@ -506,6 +506,10 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
$pbi$::Private, parent, msg) $pbi$::Private, parent, msg)
} }
} }
#[doc(hidden)]
pub fn new(_private: $pbi$::Private, msg: &'a mut $pbr$::MessageInner) -> Self {
Self{ inner: $pbr$::MutatorMessageRef::new(_private, msg) }
}
$accessor_fns_for_muts$ $accessor_fns_for_muts$
} }
@ -544,6 +548,14 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
$Msg::deserialize$ $Msg::deserialize$
} }
pub fn as_view(&self) -> $Msg$View {
$Msg$View::new($pbi$::Private, self.inner.msg)
}
pub fn as_mut(&mut self) -> $Msg$Mut {
$Msg$Mut::new($pbi$::Private, &mut self.inner)
}
$accessor_fns$ $accessor_fns$
$oneof_accessor_fns$ $oneof_accessor_fns$

Loading…
Cancel
Save