Expand PrimitiveRsTypeName to work with non-primitives, rename, refactor

PiperOrigin-RevId: 596765288
pull/15323/head
Alyssa Haroldsen 1 year ago committed by Copybara-Service
parent 1eab5a0237
commit fa67ce8d4d
  1. 4
      src/google/protobuf/compiler/rust/accessors/map.cc
  2. 4
      src/google/protobuf/compiler/rust/accessors/repeated_scalar.cc
  3. 5
      src/google/protobuf/compiler/rust/accessors/singular_message.cc
  4. 4
      src/google/protobuf/compiler/rust/accessors/singular_scalar.cc
  5. 2
      src/google/protobuf/compiler/rust/accessors/singular_string.cc
  6. 4
      src/google/protobuf/compiler/rust/message.cc
  7. 10
      src/google/protobuf/compiler/rust/naming.cc
  8. 5
      src/google/protobuf/compiler/rust/naming.h
  9. 12
      src/google/protobuf/compiler/rust/oneof.cc

@ -22,8 +22,8 @@ void Map::InMsgImpl(Context& ctx, const FieldDescriptor& field) const {
auto& value_type = *field.message_type()->map_value();
ctx.Emit({{"field", field.name()},
{"Key", PrimitiveRsTypeName(key_type)},
{"Value", PrimitiveRsTypeName(value_type)},
{"Key", RsTypePath(ctx, key_type)},
{"Value", RsTypePath(ctx, value_type)},
{"getter_thunk", ThunkName(ctx, field, "get")},
{"getter_mut_thunk", ThunkName(ctx, field, "get_mut")},
{"getter",

@ -20,7 +20,7 @@ namespace rust {
void RepeatedScalar::InMsgImpl(Context& ctx,
const FieldDescriptor& field) const {
ctx.Emit({{"field", field.name()},
{"Scalar", PrimitiveRsTypeName(field)},
{"Scalar", RsTypePath(ctx, field)},
{"getter_thunk", ThunkName(ctx, field, "get")},
{"getter_mut_thunk", ThunkName(ctx, field, "get_mut")},
{"getter",
@ -100,7 +100,7 @@ void RepeatedScalar::InMsgImpl(Context& ctx,
void RepeatedScalar::InExternC(Context& ctx,
const FieldDescriptor& field) const {
ctx.Emit({{"Scalar", PrimitiveRsTypeName(field)},
ctx.Emit({{"Scalar", RsTypePath(ctx, field)},
{"getter_thunk", ThunkName(ctx, field, "get")},
{"getter_mut_thunk", ThunkName(ctx, field, "get_mut")},
{"getter",

@ -5,6 +5,8 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include <string>
#include "absl/strings/string_view.h"
#include "google/protobuf/compiler/cpp/helpers.h"
#include "google/protobuf/compiler/rust/accessors/accessor_generator.h"
@ -19,8 +21,7 @@ namespace rust {
void SingularMessage::InMsgImpl(Context& ctx,
const FieldDescriptor& field) const {
auto& msg = *field.message_type();
auto prefix = "crate::" + GetCrateRelativeQualifiedPath(ctx, msg);
std::string prefix = RsTypePath(ctx, field);
ctx.Emit(
{

@ -23,7 +23,7 @@ void SingularScalar::InMsgImpl(Context& ctx,
ctx.Emit(
{
{"field", field.name()},
{"Scalar", PrimitiveRsTypeName(field)},
{"Scalar", RsTypePath(ctx, field)},
{"hazzer_thunk", ThunkName(ctx, field, "has")},
{"default_value", DefaultValue(field)},
{"getter",
@ -117,7 +117,7 @@ void SingularScalar::InMsgImpl(Context& ctx,
void SingularScalar::InExternC(Context& ctx,
const FieldDescriptor& field) const {
ctx.Emit({{"Scalar", PrimitiveRsTypeName(field)},
ctx.Emit({{"Scalar", RsTypePath(ctx, field)},
{"hazzer_thunk", ThunkName(ctx, field, "has")},
{"getter_thunk", ThunkName(ctx, field, "get")},
{"setter_thunk", ThunkName(ctx, field, "set")},

@ -25,7 +25,7 @@ void SingularString::InMsgImpl(Context& ctx,
std::string hazzer_thunk = ThunkName(ctx, field, "has");
std::string getter_thunk = ThunkName(ctx, field, "get");
std::string setter_thunk = ThunkName(ctx, field, "set");
std::string proxied_type = PrimitiveRsTypeName(field);
std::string proxied_type = RsTypePath(ctx, field);
auto transform_view = [&] {
if (field.type() == FieldDescriptor::TYPE_STRING) {
ctx.Emit(R"rs(

@ -233,7 +233,7 @@ void GetterForViewOrMut(Context& ctx, const FieldDescriptor& field,
if (!IsInCurrentlyGeneratingCrate(ctx, msg)) {
return;
}
auto prefix = "crate::" + GetCrateRelativeQualifiedPath(ctx, msg);
auto prefix = RsTypePath(ctx, field);
ctx.Emit(
{
{"prefix", prefix},
@ -270,7 +270,7 @@ void GetterForViewOrMut(Context& ctx, const FieldDescriptor& field,
return;
}
auto rsType = PrimitiveRsTypeName(field);
auto rsType = RsTypePath(ctx, field);
auto asRef = IsStringOrBytes(fieldType) ? ".as_ref()" : "";
auto vtable =
IsStringOrBytes(fieldType) ? "BytesMutVTable" : "PrimitiveVTable";

@ -175,7 +175,7 @@ std::string ThunkName(Context& ctx, const Descriptor& msg,
op);
}
std::string PrimitiveRsTypeName(const FieldDescriptor& field) {
std::string RsTypePath(Context& ctx, const FieldDescriptor& field) {
switch (field.type()) {
case FieldDescriptor::TYPE_BOOL:
return "bool";
@ -201,6 +201,14 @@ std::string PrimitiveRsTypeName(const FieldDescriptor& field) {
return "[u8]";
case FieldDescriptor::TYPE_STRING:
return "::__pb::ProtoStr";
case FieldDescriptor::TYPE_MESSAGE:
// TODO: Fix depending on types from other proto_libraries.
return absl::StrCat(
"crate::", GetCrateRelativeQualifiedPath(ctx, *field.message_type()));
case FieldDescriptor::TYPE_ENUM:
// TODO: Fix depending on types from other proto_libraries.
return absl::StrCat(
"crate::", GetCrateRelativeQualifiedPath(ctx, *field.enum_type()));
default:
break;
}

@ -33,7 +33,10 @@ std::string ThunkName(Context& ctx, const OneofDescriptor& field,
std::string ThunkName(Context& ctx, const Descriptor& msg,
absl::string_view op);
std::string PrimitiveRsTypeName(const FieldDescriptor& field);
// Returns an absolute path to the Proxied Rust type of the given field.
// The absolute path is guaranteed to work in the crate that defines the field.
// It may be crate-relative, or directly reference the owning crate of the type.
std::string RsTypePath(Context& ctx, const FieldDescriptor& field);
std::string EnumRsName(const EnumDescriptor& desc);

@ -92,15 +92,13 @@ std::string RsTypeNameView(Context& ctx, const FieldDescriptor& field) {
case FieldDescriptor::TYPE_FLOAT:
case FieldDescriptor::TYPE_DOUBLE:
case FieldDescriptor::TYPE_BOOL:
return PrimitiveRsTypeName(field);
return RsTypePath(ctx, field);
case FieldDescriptor::TYPE_BYTES:
return "&'msg [u8]";
case FieldDescriptor::TYPE_STRING:
return "&'msg ::__pb::ProtoStr";
case FieldDescriptor::TYPE_MESSAGE:
return absl::StrCat(
"::__pb::View<'msg, crate::",
GetCrateRelativeQualifiedPath(ctx, *field.message_type()), ">");
return absl::StrCat("::__pb::View<'msg, ", RsTypePath(ctx, field), ">");
case FieldDescriptor::TYPE_ENUM: // TODO: b/300257770 - Support enums.
case FieldDescriptor::TYPE_GROUP: // Not supported yet.
return "";
@ -130,12 +128,8 @@ std::string RsTypeNameMut(Context& ctx, const FieldDescriptor& field) {
case FieldDescriptor::TYPE_BOOL:
case FieldDescriptor::TYPE_BYTES:
case FieldDescriptor::TYPE_STRING:
return absl::StrCat("::__pb::Mut<'msg, ", PrimitiveRsTypeName(field),
">");
case FieldDescriptor::TYPE_MESSAGE:
return absl::StrCat(
"::__pb::Mut<'msg, crate::",
GetCrateRelativeQualifiedPath(ctx, *field.message_type()), ">");
return absl::StrCat("::__pb::Mut<'msg, ", RsTypePath(ctx, field), ">");
case FieldDescriptor::TYPE_ENUM: // TODO: b/300257770 - Support enums.
case FieldDescriptor::TYPE_GROUP: // Not supported yet.
return "";

Loading…
Cancel
Save