From 4745f526d44f74e6b2957ea48248e5af981d3fc8 Mon Sep 17 00:00:00 2001 From: Jakob Buchgraber Date: Tue, 28 Mar 2023 04:58:09 -0700 Subject: [PATCH] Small style guide, naming and formatting fixes PiperOrigin-RevId: 519993508 --- .../protobuf/compiler/rust/generator.cc | 84 ++++++++++--------- 1 file changed, 44 insertions(+), 40 deletions(-) diff --git a/src/google/protobuf/compiler/rust/generator.cc b/src/google/protobuf/compiler/rust/generator.cc index ca22664630..09b66b8a92 100644 --- a/src/google/protobuf/compiler/rust/generator.cc +++ b/src/google/protobuf/compiler/rust/generator.cc @@ -58,13 +58,13 @@ bool ExperimentalRustGeneratorEnabled( }); } -// Marks which kernel Rust codegen assumes and generates gencode for. +// Marks which kernel the Rust codegen should generate code for. enum class Kernel { kUpb, kCpp, }; -absl::optional ParsekernelConfiguration( +absl::optional ParseKernelConfiguration( const std::vector>& options) { for (const auto& pair : options) { if (pair.first == "kernel") { @@ -79,7 +79,7 @@ absl::optional ParsekernelConfiguration( return absl::nullopt; } -std::string get_crate_name(const FileDescriptor* dependency) { +std::string GetCrateName(const FileDescriptor* dependency) { absl::string_view path = dependency->name(); auto basename = path.substr(path.rfind('/') + 1); return absl::StrReplaceAll(basename, { @@ -90,38 +90,42 @@ std::string get_crate_name(const FileDescriptor* dependency) { void GenerateForUpb(const FileDescriptor* file, google::protobuf::io::Printer& p) { for (int i = 0; i < file->message_type_count(); ++i) { - std::string full_name = file->message_type(i)->full_name(); - absl::StrReplaceAll({{".", "_"}}, &full_name); + // The prefix used by the UPB compiler to generate unique function + // names. + std::string upb_msg_prefix = file->message_type(i)->full_name(); + absl::StrReplaceAll({{".", "_"}}, &upb_msg_prefix); // TODO(b/275365731): Implement field accessors et. al. - p.Emit({{"Msg", file->message_type(i)->name()}, {"pkg_Msg", full_name}}, - R"rs( - pub struct $Msg$ { - msg: ::__std::ptr::NonNull, - arena: *mut ::__pb::Arena, - } + p.Emit( + {{"Msg", file->message_type(i)->name()}, {"pkg_Msg", upb_msg_prefix}}, + R"rs( + pub struct $Msg$ { + msg: ::__std::ptr::NonNull, + arena: *mut ::__pb::Arena, + } - impl $Msg$ { - pub fn new() -> Self { - let arena = unsafe { ::__pb::Arena::new() }; - let msg = unsafe { $pkg_Msg$_new(arena) }; - $Msg$ { msg, arena } - } - pub fn serialize(&self) -> ::__pb::SerializedData { - let arena = unsafe { ::__pb::__runtime::upb_Arena_New() }; - let mut len = 0; - let chars = unsafe { $pkg_Msg$_serialize(self.msg, arena, &mut len) }; - unsafe {::__pb::SerializedData::from_raw_parts(arena, chars, len)} - } + impl $Msg$ { + pub fn new() -> Self { + let arena = unsafe { ::__pb::Arena::new() }; + let msg = unsafe { $pkg_Msg$_new(arena) }; + $Msg$ { msg, arena } + } + + pub fn serialize(&self) -> ::__pb::SerializedData { + let arena = unsafe { ::__pb::__runtime::upb_Arena_New() }; + let mut len = 0; + let chars = unsafe { $pkg_Msg$_serialize(self.msg, arena, &mut len) }; + unsafe {::__pb::SerializedData::from_raw_parts(arena, chars, len)} } + } - extern "C" { - fn $pkg_Msg$_new(arena: *mut ::__pb::Arena) -> ::__std::ptr::NonNull; - fn $pkg_Msg$_serialize( - msg: ::__std::ptr::NonNull, - arena: *mut ::__pb::Arena, - len: &mut usize) -> ::__std::ptr::NonNull; - } + extern "C" { + fn $pkg_Msg$_new(arena: *mut ::__pb::Arena) -> ::__std::ptr::NonNull; + fn $pkg_Msg$_serialize( + msg: ::__std::ptr::NonNull, + arena: *mut ::__pb::Arena, + len: &mut usize) -> ::__std::ptr::NonNull; + } )rs"); } } @@ -131,15 +135,15 @@ void GenerateForCpp(const FileDescriptor* file, google::protobuf::io::Printer& p // TODO(b/272728844): Implement real logic p.Emit({{"Msg", file->message_type(i)->name()}}, R"rs( - pub struct $Msg$ { - msg: ::__std::ptr::NonNull, - } + pub struct $Msg$ { + msg: ::__std::ptr::NonNull, + } - impl $Msg$ { - pub fn new() -> Self { Self { msg: ::__std::ptr::NonNull::dangling() }} - pub fn serialize(&self) -> Vec { vec![] } - } - )rs"); + impl $Msg$ { + pub fn new() -> Self { Self { msg: ::__std::ptr::NonNull::dangling() }} + pub fn serialize(&self) -> Vec { vec![] } + } + )rs"); } } @@ -158,7 +162,7 @@ bool RustGenerator::Generate(const FileDescriptor* file, return false; } - absl::optional kernel = ParsekernelConfiguration(options); + absl::optional kernel = ParseKernelConfiguration(options); if (!kernel.has_value()) { *error = "Mandatory option `kernel` missing, please specify `cpp` or " @@ -182,7 +186,7 @@ bool RustGenerator::Generate(const FileDescriptor* file, // Rust crate names (currently Bazel labels). for (int i = 0; i < file->public_dependency_count(); ++i) { const FileDescriptor* dep = file->public_dependency(i); - std::string crate_name = get_crate_name(dep); + std::string crate_name = GetCrateName(dep); for (int j = 0; j < dep->message_type_count(); ++j) { // TODO(b/272728844): Implement real logic p.Emit(