Remove generated C++ thunks for enums as map values

Protobuf enums in C++ always have `int` as their backing type, so for the
purpose of use as a map value, we can just treat enums as 32-bit ints. This
allows us to delete the enum-specific generated C++ thunks.

PiperOrigin-RevId: 666043376
pull/17906/head
Adam Cozzette 6 months ago committed by Copybara-Service
parent 983910fa1a
commit 6a177829fc
  1. 18
      rust/cpp.rs
  2. 48
      src/google/protobuf/compiler/rust/enum.cc
  3. 3
      src/google/protobuf/compiler/rust/enum.h
  4. 1
      src/google/protobuf/compiler/rust/generator.cc
  5. 3
      src/google/protobuf/compiler/rust/message.cc
  6. 4
      src/google/protobuf/compiler/rust/naming.cc

@ -868,15 +868,15 @@ macro_rules! impl_ProxiedInMapValue_for_non_generated_value_types {
($key_t:ty, $ffi_key_t:ty, $to_ffi_key:expr, $from_ffi_key:expr, for $($t:ty, $ffi_view_t:ty, $ffi_value_t:ty, $to_ffi_value:expr, $from_ffi_value:expr;)*) => {
paste! { $(
extern "C" {
fn [< proto2_rust_thunk_Map_ $key_t _ $t _new >]() -> RawMap;
fn [< proto2_rust_thunk_Map_ $key_t _ $t _free >](m: RawMap);
fn [< proto2_rust_thunk_Map_ $key_t _ $t _clear >](m: RawMap);
fn [< proto2_rust_thunk_Map_ $key_t _ $t _size >](m: RawMap) -> usize;
fn [< proto2_rust_thunk_Map_ $key_t _ $t _insert >](m: RawMap, key: $ffi_key_t, value: $ffi_value_t) -> bool;
fn [< proto2_rust_thunk_Map_ $key_t _ $t _get >](m: RawMap, key: $ffi_key_t, value: *mut $ffi_view_t) -> bool;
fn [< proto2_rust_thunk_Map_ $key_t _ $t _iter >](m: RawMap) -> UntypedMapIterator;
fn [< proto2_rust_thunk_Map_ $key_t _ $t _iter_get >](iter: &mut UntypedMapIterator, size_info: MapNodeSizeInfo, key: *mut $ffi_key_t, value: *mut $ffi_view_t);
fn [< proto2_rust_thunk_Map_ $key_t _ $t _remove >](m: RawMap, key: $ffi_key_t, value: *mut $ffi_view_t) -> bool;
pub fn [< proto2_rust_thunk_Map_ $key_t _ $t _new >]() -> RawMap;
pub fn [< proto2_rust_thunk_Map_ $key_t _ $t _free >](m: RawMap);
pub fn [< proto2_rust_thunk_Map_ $key_t _ $t _clear >](m: RawMap);
pub fn [< proto2_rust_thunk_Map_ $key_t _ $t _size >](m: RawMap) -> usize;
pub fn [< proto2_rust_thunk_Map_ $key_t _ $t _insert >](m: RawMap, key: $ffi_key_t, value: $ffi_value_t) -> bool;
pub fn [< proto2_rust_thunk_Map_ $key_t _ $t _get >](m: RawMap, key: $ffi_key_t, value: *mut $ffi_view_t) -> bool;
pub fn [< proto2_rust_thunk_Map_ $key_t _ $t _iter >](m: RawMap) -> UntypedMapIterator;
pub fn [< proto2_rust_thunk_Map_ $key_t _ $t _iter_get >](iter: &mut UntypedMapIterator, size_info: MapNodeSizeInfo, key: *mut $ffi_key_t, value: *mut $ffi_view_t);
pub fn [< proto2_rust_thunk_Map_ $key_t _ $t _remove >](m: RawMap, key: $ffi_key_t, value: *mut $ffi_view_t) -> bool;
}
impl ProxiedInMapValue<$key_t> for $t {

@ -72,56 +72,45 @@ void EnumProxiedInMapValue(Context& ctx, const EnumDescriptor& desc) {
[&] { ctx.Emit(t.rs_from_ffi_key_expr); })
.WithSuffix("")},
R"rs(
extern "C" {
fn $map_new_thunk$() -> $pbr$::RawMap;
fn $map_free_thunk$(m: $pbr$::RawMap);
fn $map_clear_thunk$(m: $pbr$::RawMap);
fn $map_size_thunk$(m: $pbr$::RawMap) -> usize;
fn $map_insert_thunk$(m: $pbr$::RawMap, key: $ffi_key_t$, value: $name$) -> bool;
fn $map_get_thunk$(m: $pbr$::RawMap, key: $ffi_key_t$, value: *mut $name$) -> bool;
fn $map_remove_thunk$(m: $pbr$::RawMap, key: $ffi_key_t$, value: *mut $name$) -> bool;
fn $map_iter_thunk$(m: $pbr$::RawMap) -> $pbr$::UntypedMapIterator;
fn $map_iter_get_thunk$(iter: &mut $pbr$::UntypedMapIterator, size_info: $pbr$::MapNodeSizeInfo, key: *mut $ffi_key_t$, value: *mut $name$);
}
impl $pb$::ProxiedInMapValue<$key_t$> for $name$ {
fn map_new(_private: $pbi$::Private) -> $pb$::Map<$key_t$, Self> {
unsafe {
$pb$::Map::from_inner(
$pbi$::Private,
$pbr$::InnerMap::new($pbi$::Private, $map_new_thunk$())
$pbr$::InnerMap::new($pbi$::Private, $pbr$::$map_new_thunk$())
)
}
}
unsafe fn map_free(_private: $pbi$::Private, map: &mut $pb$::Map<$key_t$, Self>) {
unsafe { $map_free_thunk$(map.as_raw($pbi$::Private)); }
unsafe { $pbr$::$map_free_thunk$(map.as_raw($pbi$::Private)); }
}
fn map_clear(mut map: $pb$::Mut<'_, $pb$::Map<$key_t$, Self>>) {
unsafe { $map_clear_thunk$(map.as_raw($pbi$::Private)); }
unsafe { $pbr$::$map_clear_thunk$(map.as_raw($pbi$::Private)); }
}
fn map_len(map: $pb$::View<'_, $pb$::Map<$key_t$, Self>>) -> usize {
unsafe { $map_size_thunk$(map.as_raw($pbi$::Private)) }
unsafe { $pbr$::$map_size_thunk$(map.as_raw($pbi$::Private)) }
}
fn map_insert(mut map: $pb$::Mut<'_, $pb$::Map<$key_t$, Self>>, key: $pb$::View<'_, $key_t$>, value: impl $pb$::IntoProxied<Self>) -> bool {
unsafe { $map_insert_thunk$(map.as_raw($pbi$::Private), $to_ffi_key_expr$, value.into_proxied($pbi$::Private)) }
unsafe { $pbr$::$map_insert_thunk$(map.as_raw($pbi$::Private), $to_ffi_key_expr$, value.into_proxied($pbi$::Private).0) }
}
fn map_get<'a>(map: $pb$::View<'a, $pb$::Map<$key_t$, Self>>, key: $pb$::View<'_, $key_t$>) -> Option<$pb$::View<'a, Self>> {
let key = $to_ffi_key_expr$;
let mut value = $std$::mem::MaybeUninit::uninit();
let found = unsafe { $map_get_thunk$(map.as_raw($pbi$::Private), key, value.as_mut_ptr()) };
let found = unsafe { $pbr$::$map_get_thunk$(map.as_raw($pbi$::Private), key, value.as_mut_ptr()) };
if !found {
return None;
}
Some(unsafe { value.assume_init() })
Some(unsafe { $name$(value.assume_init()) })
}
fn map_remove(mut map: $pb$::Mut<'_, $pb$::Map<$key_t$, Self>>, key: $pb$::View<'_, $key_t$>) -> bool {
let mut value = $std$::mem::MaybeUninit::uninit();
unsafe { $map_remove_thunk$(map.as_raw($pbi$::Private), $to_ffi_key_expr$, value.as_mut_ptr()) }
unsafe { $pbr$::$map_remove_thunk$(map.as_raw($pbi$::Private), $to_ffi_key_expr$, value.as_mut_ptr()) }
}
fn map_iter(map: $pb$::View<'_, $pb$::Map<$key_t$, Self>>) -> $pb$::MapIter<'_, $key_t$, Self> {
@ -133,7 +122,7 @@ void EnumProxiedInMapValue(Context& ctx, const EnumDescriptor& desc) {
unsafe {
$pb$::MapIter::from_raw(
$pbi$::Private,
$map_iter_thunk$(map.as_raw($pbi$::Private))
$pbr$::$map_iter_thunk$(map.as_raw($pbi$::Private))
)
}
}
@ -148,10 +137,10 @@ void EnumProxiedInMapValue(Context& ctx, const EnumDescriptor& desc) {
unsafe {
iter.as_raw_mut($pbi$::Private).next_unchecked::<$key_t$, Self, _, _>(
$pbi$::Private,
$map_iter_get_thunk$,
$pbr$::$map_iter_get_thunk$,
$pbr$::MapNodeSizeInfo(0), // Ignored
|ffi_key| $from_ffi_key_expr$,
$std$::convert::identity,
|value| $name$(value),
)
}
}
@ -496,21 +485,6 @@ void GenerateEnumDefinition(Context& ctx, const EnumDescriptor& desc) {
)rs");
}
void GenerateEnumThunksCc(Context& ctx, const EnumDescriptor& desc) {
ctx.Emit(
{
{"cpp_t", cpp::QualifiedClassName(&desc)},
{"rs_t", UnderscoreDelimitFullName(ctx, desc.full_name())},
{"abi", "\"C\""}, // Workaround for syntax highlight bug in VSCode.
},
R"cc(
extern $abi$ {
__PB_RUST_EXPOSE_SCALAR_MAP_METHODS_FOR_VALUE_TYPE(
$cpp_t$, $rs_t$, $cpp_t$, $cpp_t$, value, cpp_value)
}
)cc");
}
} // namespace rust
} // namespace compiler
} // namespace protobuf

@ -26,9 +26,6 @@ namespace rust {
// Generates code for a particular enum in `.pb.rs`.
void GenerateEnumDefinition(Context& ctx, const EnumDescriptor& desc);
// Generates code for a particular enum in `.pb.thunk.cc`.
void GenerateEnumThunksCc(Context& ctx, const EnumDescriptor& desc);
// An enum value with a unique number and any aliases for it.
struct RustEnumValue {
// The canonical CamelCase name in Rust.

@ -258,7 +258,6 @@ bool RustGenerator::Generate(const FileDescriptor* file,
thunks_ctx.Emit({{"enum", enum_.full_name()}}, R"cc(
// $enum$
)cc");
GenerateEnumThunksCc(thunks_ctx, enum_);
thunks_ctx.printer().PrintRaw("\n");
}
}

@ -1402,9 +1402,6 @@ void GenerateThunksCc(Context& ctx, const Descriptor& msg) {
for (int i = 0; i < msg.nested_type_count(); ++i) {
GenerateThunksCc(ctx, *msg.nested_type(i));
}
for (int i = 0; i < msg.enum_type_count(); ++i) {
GenerateEnumThunksCc(ctx, *msg.enum_type(i));
}
}},
{"accessor_thunks",
[&] {

@ -71,8 +71,8 @@ std::string RawMapThunk(Context& ctx, const Descriptor& msg,
std::string RawMapThunk(Context& ctx, const EnumDescriptor& desc,
absl::string_view key_t, absl::string_view op) {
return absl::StrCat("proto2_rust_thunk_Map_", key_t, "_",
GetUnderscoreDelimitedFullName(ctx, *&desc), "_", op);
// Enums are always 32 bits.
return absl::StrCat("proto2_rust_thunk_Map_", key_t, "_i32_", op);
}
namespace {

Loading…
Cancel
Save