Make the oneof view accessors use consuming-self and the corresponding longer lifetime, avoiding unnecessary shortening of lifetimes in some cases.

This follows the precedent established on other accessors on Msg Views.

PiperOrigin-RevId: 614658415
pull/16116/head
Protobuf Team Bot 9 months ago committed by Copybara-Service
parent 686361e79e
commit bb1c0abcbf
  1. 27
      rust/test/shared/accessors_proto3_test.rs
  2. 4
      src/google/protobuf/compiler/rust/oneof.cc

@ -273,15 +273,10 @@ fn test_oneof_accessors() {
assert_that!(msg.oneof_uint32_opt(), eq(Optional::Unset(0)));
assert_that!(msg.oneof_field(), matches_pattern!(not_set(_)));
// TODO: the submessage api is still in progress so we can't yet
// cause a submsg to be set here.
msg.oneof_nested_message_mut().or_default(); // Cause the nested_message field to become set.
// msg.oneof_nested_message_mut().or_default(); // Cause the nested_message
// field to become set.
// assert_that!(msg.oneof_bytes_opt(),
// eq(Optional::Unset(_))); assert_that!(msg.oneof_field(),
// matches_pattern!(OneofNestedMessage(_)));
assert_that!(msg.oneof_bytes_opt(), matches_pattern!(Optional::Unset(_)));
assert_that!(msg.oneof_field(), matches_pattern!(OneofNestedMessage(_)));
msg.set_oneof_uint32(7);
msg.set_oneof_bytes(b"123");
@ -292,6 +287,22 @@ fn test_oneof_accessors() {
assert_that!(msg.oneof_field(), matches_pattern!(not_set(_)));
}
#[test]
fn test_oneof_accessors_view_long_lifetime() {
use TestAllTypes_::OneofField::*;
let mut msg = TestAllTypes::new();
msg.set_oneof_uint32(7);
// The oneof-view accessor on MsgViews should maintain the longest lifetime (can
// outlive the message view).
let oneof = {
let view = msg.as_view();
view.oneof_field()
};
assert_that!(oneof, matches_pattern!(OneofUint32(eq(7))));
}
#[test]
fn test_oneof_enum_accessors() {
use unittest_proto3::{

@ -235,6 +235,8 @@ void GenerateOneofAccessors(Context& ctx, const OneofDescriptor& oneof,
AccessorCase accessor_case) {
ctx.Emit(
{{"oneof_name", RsSafeName(oneof.name())},
{"view_lifetime", ViewLifetime(accessor_case)},
{"view_self", ViewReceiver(accessor_case)},
{"view_enum_name", OneofViewEnumRsName(oneof)},
{"mut_enum_name", OneofMutEnumRsName(oneof)},
{"case_enum_name", OneofCaseEnumRsName(oneof)},
@ -293,7 +295,7 @@ void GenerateOneofAccessors(Context& ctx, const OneofDescriptor& oneof,
{"getter",
[&] {
ctx.Emit({}, R"rs(
pub fn $oneof_name$(&self) -> $Msg$_::$view_enum_name$ {
pub fn $oneof_name$($view_self$) -> $Msg$_::$view_enum_name$<$view_lifetime$> {
match unsafe { $case_thunk$(self.raw_msg()) } {
$view_cases$
_ => $Msg$_::$view_enum_name$::not_set(std::marker::PhantomData)

Loading…
Cancel
Save