Allow non-repeated StringPiece accessors to be generated.

Add tests for this case as well as repeated and non-repeated Editions string_view typed fields.

PiperOrigin-RevId: 642264883
pull/17070/head
Protobuf Team Bot 7 months ago committed by Copybara-Service
parent 8434c12d16
commit 61a2df3129
  1. 1
      rust/test/BUILD
  2. 6
      rust/test/edition2023.proto
  3. 8
      rust/test/shared/accessors_proto3_test.rs
  4. 10
      rust/test/shared/accessors_test.rs
  5. 19
      rust/test/shared/edition2023_test.rs
  6. 10
      src/google/protobuf/compiler/rust/accessors/accessors.cc

@ -136,6 +136,7 @@ proto_library(
name = "edition2023_proto",
testonly = True,
srcs = ["edition2023.proto"],
deps = ["//src/google/protobuf:cpp_features_proto"],
)
rust_cc_proto_library(

@ -2,7 +2,13 @@ edition = "2023";
package test;
import "google/protobuf/cpp_features.proto";
message EditionsMessage {
int32 plain_field = 1;
int32 implicit_presence_field = 2 [features.field_presence = IMPLICIT];
string str_view = 3 [features.(pb.cpp).string_type = VIEW];
repeated string repeated_str_view = 4 [features.(pb.cpp).string_type = VIEW];
repeated string repeated_str = 5;
}

@ -243,3 +243,11 @@ fn test_submsg_setter() {
assert_that!(parent.optional_nested_message().bb(), eq(7));
}
#[test]
fn test_ctype_stringpiece() {
let mut msg = TestAllTypes::new();
assert_that!(msg.optional_string_piece(), eq(""));
msg.set_optional_string_piece("hello");
assert_that!(msg.optional_string_piece(), eq("hello"));
}

@ -814,3 +814,13 @@ fn test_to_owned() {
assert_that!(submsg_clone.bb(), eq(7));
assert_that!(submsg_mut.bb(), eq(8));
}
#[test]
fn test_ctype_stringpiece() {
let mut msg = TestAllTypes::new();
assert_that!(msg.optional_string_piece(), eq(""));
assert_that!(msg.has_optional_string_piece(), eq(false));
msg.set_optional_string_piece("hello");
assert_that!(msg.optional_string_piece(), eq("hello"));
assert_that!(msg.has_optional_string_piece(), eq(true));
}

@ -17,3 +17,22 @@ fn check_edition2023_works() {
assert_that!(msg.plain_field_opt().into_inner(), eq(0));
assert_that!(msg.implicit_presence_field(), eq(0));
}
#[test]
fn string_view_works() {
let mut msg = edition2023_rust_proto::EditionsMessage::new();
assert_that!(msg.str_view(), eq(""));
assert_that!(msg.has_str_view(), eq(false));
msg.set_str_view("hello");
assert_that!(msg.str_view(), eq("hello"));
assert_that!(msg.has_str_view(), eq(true));
}
#[test]
fn repeated_string_view_works() {
let mut msg = edition2023_rust_proto::EditionsMessage::new();
assert_that!(msg.repeated_str_view().len(), eq(0));
msg.repeated_str_view_mut().push("first".into());
assert_that!(msg.repeated_str_view().len(), eq(1));
assert_that!(msg.repeated_str_view().get(0), some(eq("first")));
}

@ -26,11 +26,13 @@ namespace {
std::unique_ptr<AccessorGenerator> AccessorGeneratorFor(
Context& ctx, const FieldDescriptor& field) {
// TODO: We do not support [ctype=FOO] (used to set the field
// type in C++ to cord or string_piece) in V0.6 API.
if (field.options().has_ctype()) {
// TODO: We do not support ctype=CORD fields or repeated
// ctype=STRING_PIECE fields.
auto ctype = field.options().ctype();
if (ctype == FieldOptions::CORD ||
(ctype == FieldOptions::STRING_PIECE && field.is_repeated())) {
return std::make_unique<UnsupportedField>(
"fields with ctype not supported");
"fields has an unsupported ctype");
}
if (field.is_map()) {

Loading…
Cancel
Save