Add support accessing simple scalars (int32s, bools, and floats) in submessages

PiperOrigin-RevId: 566403598
pull/14123/head
Hong Shin 1 year ago committed by Copybara-Service
parent df1aad1617
commit 80a4df3306
  1. 32
      rust/test/BUILD
  2. 18
      rust/test/nested.proto
  3. 14
      rust/test/shared/BUILD
  4. 16
      rust/test/shared/simple_nested_test.rs
  5. 43
      src/google/protobuf/compiler/rust/message.cc

@ -298,3 +298,35 @@ rust_upb_proto_library(
],
deps = [":reserved_proto"],
)
proto_library(
name = "nested_proto",
testonly = True,
srcs = ["nested.proto"],
)
cc_proto_library(
name = "nested_cc_proto",
testonly = True,
deps = [":nested_proto"],
)
rust_cc_proto_library(
name = "nested_cc_rust_proto",
testonly = True,
visibility = [
"//rust/test/cpp:__subpackages__",
"//rust/test/shared:__subpackages__",
],
deps = [":nested_cc_proto"],
)
rust_upb_proto_library(
name = "nested_upb_rust_proto",
testonly = True,
visibility = [
"//rust/test/shared:__subpackages__",
"//rust/test/upb:__subpackages__",
],
deps = [":nested_proto"],
)

@ -0,0 +1,18 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
syntax = "proto2";
package nest;
message Outer {
message Inner {
optional int32 num = 1;
optional bool boolean = 2;
}
optional Inner inner = 1;
}

@ -201,3 +201,17 @@ rust_test(
],
deps = ["//rust/test:unittest_cc_rust_proto"],
)
rust_test(
name = "simple_nested_cpp_test",
srcs = ["simple_nested_test.rs"],
deps = ["//rust/test:nested_cc_rust_proto"],
)
# TODO(b/285309454): enable once we emit default messages for upb at the rust level
# The default submsg that upb emits is NULL (whereas cpp returns a default constructed msg)
#rust_test(
# name = "simple_nested_upb_test",
# srcs = ["simple_nested_test.rs"],
# deps = ["//rust/test:nested_upb_rust_proto"],
#)

@ -0,0 +1,16 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
use nested_proto::nest::Outer;
#[test]
fn test_simple_nested_proto() {
let outer_msg = Outer::new();
// TODO(b/285309454): passing cpp, segfaulting upb
assert_eq!(outer_msg.inner().num(), 0);
assert!(!outer_msg.inner().boolean());
}

@ -162,6 +162,38 @@ void MessageDrop(Context<Descriptor> msg) {
unsafe { $delete_thunk$(self.inner.msg); }
)rs");
}
// TODO(b/285309454): deferring on strings and bytes for now, eventually this
// check will go away as we support more than just simple scalars
bool IsSimpleScalar(FieldDescriptor::Type type) {
return type == FieldDescriptor::TYPE_DOUBLE ||
type == FieldDescriptor::TYPE_FLOAT ||
type == FieldDescriptor::TYPE_INT32 ||
type == FieldDescriptor::TYPE_INT64 ||
type == FieldDescriptor::TYPE_UINT32 ||
type == FieldDescriptor::TYPE_UINT64 ||
type == FieldDescriptor::TYPE_SINT32 ||
type == FieldDescriptor::TYPE_SINT64 ||
type == FieldDescriptor::TYPE_FIXED32 ||
type == FieldDescriptor::TYPE_FIXED64 ||
type == FieldDescriptor::TYPE_SFIXED32 ||
type == FieldDescriptor::TYPE_SFIXED64 ||
type == FieldDescriptor::TYPE_BOOL;
}
void GenerateSubView(Context<FieldDescriptor> field) {
field.Emit(
{
{"field", field.desc().name()},
{"getter_thunk", Thunk(field, "get")},
{"Scalar", PrimitiveRsTypeName(field.desc())},
},
R"rs(
pub fn r#$field$(&self) -> $Scalar$ { unsafe {
$getter_thunk$(self.msg)
} }
)rs");
}
} // namespace
void GenerateRs(Context<Descriptor> msg) {
@ -246,6 +278,16 @@ void GenerateRs(Context<Descriptor> msg) {
} // mod $Msg$_
)rs");
}},
{"subviews",
[&] {
for (int i = 0; i < msg.desc().field_count(); ++i) {
auto field = msg.WithDesc(*msg.desc().field(i));
if (field.desc().is_repeated()) continue;
if (!IsSimpleScalar(field.desc().type())) continue;
GenerateSubView(field);
msg.printer().PrintRaw("\n");
}
}},
},
R"rs(
#[allow(non_camel_case_types)]
@ -279,6 +321,7 @@ void GenerateRs(Context<Descriptor> msg) {
pub fn new(_private: $pbi$::Private, msg: $pbi$::RawMessage) -> Self {
Self { msg, _phantom: std::marker::PhantomData }
}
$subviews$
}
// SAFETY:

Loading…
Cancel
Save