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(); auto& value_type = *field.message_type()->map_value();
ctx.Emit({{"field", field.name()}, ctx.Emit({{"field", field.name()},
{"Key", PrimitiveRsTypeName(key_type)}, {"Key", RsTypePath(ctx, key_type)},
{"Value", PrimitiveRsTypeName(value_type)}, {"Value", RsTypePath(ctx, value_type)},
{"getter_thunk", ThunkName(ctx, field, "get")}, {"getter_thunk", ThunkName(ctx, field, "get")},
{"getter_mut_thunk", ThunkName(ctx, field, "get_mut")}, {"getter_mut_thunk", ThunkName(ctx, field, "get_mut")},
{"getter", {"getter",

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

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

@ -23,7 +23,7 @@ void SingularScalar::InMsgImpl(Context& ctx,
ctx.Emit( ctx.Emit(
{ {
{"field", field.name()}, {"field", field.name()},
{"Scalar", PrimitiveRsTypeName(field)}, {"Scalar", RsTypePath(ctx, field)},
{"hazzer_thunk", ThunkName(ctx, field, "has")}, {"hazzer_thunk", ThunkName(ctx, field, "has")},
{"default_value", DefaultValue(field)}, {"default_value", DefaultValue(field)},
{"getter", {"getter",
@ -117,7 +117,7 @@ void SingularScalar::InMsgImpl(Context& ctx,
void SingularScalar::InExternC(Context& ctx, void SingularScalar::InExternC(Context& ctx,
const FieldDescriptor& field) const { const FieldDescriptor& field) const {
ctx.Emit({{"Scalar", PrimitiveRsTypeName(field)}, ctx.Emit({{"Scalar", RsTypePath(ctx, field)},
{"hazzer_thunk", ThunkName(ctx, field, "has")}, {"hazzer_thunk", ThunkName(ctx, field, "has")},
{"getter_thunk", ThunkName(ctx, field, "get")}, {"getter_thunk", ThunkName(ctx, field, "get")},
{"setter_thunk", ThunkName(ctx, field, "set")}, {"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 hazzer_thunk = ThunkName(ctx, field, "has");
std::string getter_thunk = ThunkName(ctx, field, "get"); std::string getter_thunk = ThunkName(ctx, field, "get");
std::string setter_thunk = ThunkName(ctx, field, "set"); std::string setter_thunk = ThunkName(ctx, field, "set");
std::string proxied_type = PrimitiveRsTypeName(field); std::string proxied_type = RsTypePath(ctx, field);
auto transform_view = [&] { auto transform_view = [&] {
if (field.type() == FieldDescriptor::TYPE_STRING) { if (field.type() == FieldDescriptor::TYPE_STRING) {
ctx.Emit(R"rs( ctx.Emit(R"rs(

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

@ -175,7 +175,7 @@ std::string ThunkName(Context& ctx, const Descriptor& msg,
op); op);
} }
std::string PrimitiveRsTypeName(const FieldDescriptor& field) { std::string RsTypePath(Context& ctx, const FieldDescriptor& field) {
switch (field.type()) { switch (field.type()) {
case FieldDescriptor::TYPE_BOOL: case FieldDescriptor::TYPE_BOOL:
return "bool"; return "bool";
@ -201,6 +201,14 @@ std::string PrimitiveRsTypeName(const FieldDescriptor& field) {
return "[u8]"; return "[u8]";
case FieldDescriptor::TYPE_STRING: case FieldDescriptor::TYPE_STRING:
return "::__pb::ProtoStr"; 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: default:
break; break;
} }

@ -33,7 +33,10 @@ std::string ThunkName(Context& ctx, const OneofDescriptor& field,
std::string ThunkName(Context& ctx, const Descriptor& msg, std::string ThunkName(Context& ctx, const Descriptor& msg,
absl::string_view op); 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); 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_FLOAT:
case FieldDescriptor::TYPE_DOUBLE: case FieldDescriptor::TYPE_DOUBLE:
case FieldDescriptor::TYPE_BOOL: case FieldDescriptor::TYPE_BOOL:
return PrimitiveRsTypeName(field); return RsTypePath(ctx, field);
case FieldDescriptor::TYPE_BYTES: case FieldDescriptor::TYPE_BYTES:
return "&'msg [u8]"; return "&'msg [u8]";
case FieldDescriptor::TYPE_STRING: case FieldDescriptor::TYPE_STRING:
return "&'msg ::__pb::ProtoStr"; return "&'msg ::__pb::ProtoStr";
case FieldDescriptor::TYPE_MESSAGE: case FieldDescriptor::TYPE_MESSAGE:
return absl::StrCat( return absl::StrCat("::__pb::View<'msg, ", RsTypePath(ctx, field), ">");
"::__pb::View<'msg, crate::",
GetCrateRelativeQualifiedPath(ctx, *field.message_type()), ">");
case FieldDescriptor::TYPE_ENUM: // TODO: b/300257770 - Support enums. case FieldDescriptor::TYPE_ENUM: // TODO: b/300257770 - Support enums.
case FieldDescriptor::TYPE_GROUP: // Not supported yet. case FieldDescriptor::TYPE_GROUP: // Not supported yet.
return ""; return "";
@ -130,12 +128,8 @@ std::string RsTypeNameMut(Context& ctx, const FieldDescriptor& field) {
case FieldDescriptor::TYPE_BOOL: case FieldDescriptor::TYPE_BOOL:
case FieldDescriptor::TYPE_BYTES: case FieldDescriptor::TYPE_BYTES:
case FieldDescriptor::TYPE_STRING: case FieldDescriptor::TYPE_STRING:
return absl::StrCat("::__pb::Mut<'msg, ", PrimitiveRsTypeName(field),
">");
case FieldDescriptor::TYPE_MESSAGE: case FieldDescriptor::TYPE_MESSAGE:
return absl::StrCat( return absl::StrCat("::__pb::Mut<'msg, ", RsTypePath(ctx, field), ">");
"::__pb::Mut<'msg, crate::",
GetCrateRelativeQualifiedPath(ctx, *field.message_type()), ">");
case FieldDescriptor::TYPE_ENUM: // TODO: b/300257770 - Support enums. case FieldDescriptor::TYPE_ENUM: // TODO: b/300257770 - Support enums.
case FieldDescriptor::TYPE_GROUP: // Not supported yet. case FieldDescriptor::TYPE_GROUP: // Not supported yet.
return ""; return "";

Loading…
Cancel
Save