diff --git a/rust/test/BUILD b/rust/test/BUILD index aa673aa048..0fedc17d2a 100644 --- a/rust/test/BUILD +++ b/rust/test/BUILD @@ -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( diff --git a/rust/test/edition2023.proto b/rust/test/edition2023.proto index d5c48a2ff3..3d5450c3fe 100644 --- a/rust/test/edition2023.proto +++ b/rust/test/edition2023.proto @@ -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; } diff --git a/rust/test/shared/accessors_proto3_test.rs b/rust/test/shared/accessors_proto3_test.rs index f7517e32cd..f4096ea43a 100644 --- a/rust/test/shared/accessors_proto3_test.rs +++ b/rust/test/shared/accessors_proto3_test.rs @@ -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")); +} diff --git a/rust/test/shared/accessors_test.rs b/rust/test/shared/accessors_test.rs index 262d0c8a99..956ff06a6b 100644 --- a/rust/test/shared/accessors_test.rs +++ b/rust/test/shared/accessors_test.rs @@ -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)); +} diff --git a/rust/test/shared/edition2023_test.rs b/rust/test/shared/edition2023_test.rs index 835ae66e5e..9c5aeba1fe 100644 --- a/rust/test/shared/edition2023_test.rs +++ b/rust/test/shared/edition2023_test.rs @@ -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"))); +} diff --git a/src/google/protobuf/compiler/rust/accessors/accessors.cc b/src/google/protobuf/compiler/rust/accessors/accessors.cc index 30984c9dcf..7a057e39a4 100644 --- a/src/google/protobuf/compiler/rust/accessors/accessors.cc +++ b/src/google/protobuf/compiler/rust/accessors/accessors.cc @@ -26,11 +26,13 @@ namespace { std::unique_ptr 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( - "fields with ctype not supported"); + "fields has an unsupported ctype"); } if (field.is_map()) {