Emit all string fields on Rust-upb regardless of ctype set.

As ctype only affects cpp implementation, these fields necessarily already work the same as any other string field.

PiperOrigin-RevId: 646156640
pull/17180/head
Protobuf Team Bot 7 months ago committed by Copybara-Service
parent 8d8db9cea8
commit 6429885156
  1. 5
      rust/test/BUILD
  2. 16
      rust/test/upb/BUILD
  3. 35
      rust/test/upb/string_ctypes_test.rs
  4. 7
      src/google/protobuf/compiler/rust/accessors/accessors.cc

@ -39,7 +39,10 @@ rust_cc_proto_library(
rust_upb_proto_library( rust_upb_proto_library(
name = "unittest_proto3_upb_rust_proto", name = "unittest_proto3_upb_rust_proto",
testonly = True, testonly = True,
visibility = ["//rust/test/shared:__subpackages__"], visibility = [
"//rust/test/shared:__subpackages__",
"//rust/test/upb:__subpackages__",
],
deps = [UNITTEST_PROTO3_TARGET], deps = [UNITTEST_PROTO3_TARGET],
) )

@ -10,3 +10,19 @@
# * `rust_upb_proto_library` instead of `rust_proto_library`. # * `rust_upb_proto_library` instead of `rust_proto_library`.
# * `//rust:protobuf_upb_export` instead of # * `//rust:protobuf_upb_export` instead of
# `//rust:protobuf`. # `//rust:protobuf`.
load("@rules_rust//rust:defs.bzl", "rust_test")
# TODO: Enable this for the cpp kernel and move these tests to shared.
rust_test(
name = "string_ctypes_test_upb_test",
srcs = ["string_ctypes_test.rs"],
aliases = {
"//rust:protobuf_upb_export": "protobuf",
},
deps = [
"//rust:protobuf_upb_export",
"//rust/test:unittest_proto3_upb_rust_proto",
"@crate_index//:googletest",
],
)

@ -0,0 +1,35 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2024 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 googletest::prelude::*;
use unittest_proto3_rust_proto::TestAllTypes;
#[test]
fn test_stringpiece_repeated() {
let mut msg = TestAllTypes::new();
assert_that!(msg.repeated_string_piece().len(), eq(0));
msg.repeated_string_piece_mut().push("hello".into());
assert_that!(msg.repeated_string_piece().len(), eq(1));
assert_that!(msg.repeated_string_piece().get(0), some(eq("hello")));
}
#[test]
fn test_cord() {
let mut msg = TestAllTypes::new();
assert_that!(msg.optional_cord(), eq(""));
msg.set_optional_cord("hello");
assert_that!(msg.optional_cord(), eq("hello"));
}
#[test]
fn test_cord_repeated() {
let mut msg = TestAllTypes::new();
assert_that!(msg.repeated_cord().len(), eq(0));
msg.repeated_cord_mut().push("hello".into());
assert_that!(msg.repeated_cord().len(), eq(1));
assert_that!(msg.repeated_cord().get(0), some(eq("hello")));
}

@ -27,10 +27,11 @@ namespace {
std::unique_ptr<AccessorGenerator> AccessorGeneratorFor( std::unique_ptr<AccessorGenerator> AccessorGeneratorFor(
Context& ctx, const FieldDescriptor& field) { Context& ctx, const FieldDescriptor& field) {
// TODO: We do not support ctype=CORD fields or repeated // TODO: We do not support ctype=CORD fields or repeated
// ctype=STRING_PIECE fields. // ctype=STRING_PIECE fields on cpp kernel yet (upb doesn't care about ctype).
auto ctype = field.options().ctype(); auto ctype = field.options().ctype();
if (ctype == FieldOptions::CORD || if (ctx.is_cpp() &&
(ctype == FieldOptions::STRING_PIECE && field.is_repeated())) { (ctype == FieldOptions::CORD ||
(ctype == FieldOptions::STRING_PIECE && field.is_repeated()))) {
return std::make_unique<UnsupportedField>( return std::make_unique<UnsupportedField>(
"fields has an unsupported ctype"); "fields has an unsupported ctype");
} }

Loading…
Cancel
Save