diff --git a/rust/cpp_kernel/cpp_api.cc b/rust/cpp_kernel/cpp_api.cc index 80655eb62b..c41e04f565 100644 --- a/rust/cpp_kernel/cpp_api.cc +++ b/rust/cpp_kernel/cpp_api.cc @@ -101,106 +101,26 @@ void __rust_proto_thunk__UntypedMapIterator_increment( iter->PlusPlus(); } -#define expose_scalar_map_methods(key_ty, rust_key_ty, ffi_key_ty, to_cpp_key, \ - to_ffi_key, value_ty, rust_value_ty, \ - ffi_value_ty, to_cpp_value, to_ffi_value) \ - google::protobuf::Map* \ - __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_new() { \ - return new google::protobuf::Map(); \ - } \ - void __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_free( \ - google::protobuf::Map* m) { \ - delete m; \ - } \ - void __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_clear( \ - google::protobuf::Map* m) { \ - m->clear(); \ - } \ - size_t __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_size( \ - const google::protobuf::Map* m) { \ - return m->size(); \ - } \ - bool __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_insert( \ - google::protobuf::Map* m, ffi_key_ty key, ffi_value_ty value) { \ - auto iter_and_inserted = m->try_emplace(to_cpp_key, to_cpp_value); \ - return iter_and_inserted.second; \ - } \ - bool __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_get( \ - const google::protobuf::Map* m, ffi_key_ty key, \ - ffi_value_ty* value) { \ - auto cpp_key = to_cpp_key; \ - auto it = m->find(cpp_key); \ - if (it == m->end()) { \ - return false; \ - } \ - auto& cpp_value = it->second; \ - *value = to_ffi_value; \ - return true; \ - } \ - google::protobuf::internal::UntypedMapIterator \ - __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_iter( \ - const google::protobuf::Map* m) { \ - return google::protobuf::internal::UntypedMapIterator::FromTyped(m->cbegin()); \ - } \ - void __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_iter_get( \ - const google::protobuf::internal::UntypedMapIterator* iter, ffi_key_ty* key, \ - ffi_value_ty* value) { \ - auto typed_iter = \ - iter->ToTyped::const_iterator>(); \ - const auto& cpp_key = typed_iter->first; \ - const auto& cpp_value = typed_iter->second; \ - *key = to_ffi_key; \ - *value = to_ffi_value; \ - } \ - bool __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_remove( \ - google::protobuf::Map* m, ffi_key_ty key, ffi_value_ty* value) { \ - auto cpp_key = to_cpp_key; \ - auto num_removed = m->erase(cpp_key); \ - return num_removed > 0; \ - } - -#define expose_scalar_map_methods_for_key_type( \ - key_ty, rust_key_ty, ffi_key_ty, to_cpp_key, to_ffi_key) \ - expose_scalar_map_methods(key_ty, rust_key_ty, ffi_key_ty, to_cpp_key, \ - to_ffi_key, int32_t, i32, int32_t, value, \ - cpp_value); \ - expose_scalar_map_methods(key_ty, rust_key_ty, ffi_key_ty, to_cpp_key, \ - to_ffi_key, uint32_t, u32, uint32_t, value, \ - cpp_value); \ - expose_scalar_map_methods(key_ty, rust_key_ty, ffi_key_ty, to_cpp_key, \ - to_ffi_key, float, f32, float, value, cpp_value); \ - expose_scalar_map_methods(key_ty, rust_key_ty, ffi_key_ty, to_cpp_key, \ - to_ffi_key, double, f64, double, value, \ - cpp_value); \ - expose_scalar_map_methods(key_ty, rust_key_ty, ffi_key_ty, to_cpp_key, \ - to_ffi_key, bool, bool, bool, value, cpp_value); \ - expose_scalar_map_methods(key_ty, rust_key_ty, ffi_key_ty, to_cpp_key, \ - to_ffi_key, uint64_t, u64, uint64_t, value, \ - cpp_value); \ - expose_scalar_map_methods(key_ty, rust_key_ty, ffi_key_ty, to_cpp_key, \ - to_ffi_key, int64_t, i64, int64_t, value, \ - cpp_value); \ - expose_scalar_map_methods( \ - key_ty, rust_key_ty, ffi_key_ty, to_cpp_key, to_ffi_key, std::string, \ - Bytes, google::protobuf::rust_internal::PtrAndLen, \ - std::string(value.ptr, value.len), \ - google::protobuf::rust_internal::PtrAndLen(cpp_value.data(), cpp_value.size())); \ - expose_scalar_map_methods( \ - key_ty, rust_key_ty, ffi_key_ty, to_cpp_key, to_ffi_key, std::string, \ - ProtoStr, google::protobuf::rust_internal::PtrAndLen, \ - std::string(value.ptr, value.len), \ - google::protobuf::rust_internal::PtrAndLen(cpp_value.data(), cpp_value.size())); - -expose_scalar_map_methods_for_key_type(int32_t, i32, int32_t, key, cpp_key); -expose_scalar_map_methods_for_key_type(uint32_t, u32, uint32_t, key, cpp_key); -expose_scalar_map_methods_for_key_type(bool, bool, bool, key, cpp_key); -expose_scalar_map_methods_for_key_type(uint64_t, u64, uint64_t, key, cpp_key); -expose_scalar_map_methods_for_key_type(int64_t, i64, int64_t, key, cpp_key); -expose_scalar_map_methods_for_key_type( +__PB_RUST_EXPOSE_SCALAR_MAP_METHODS_FOR_VALUE_TYPE(int32_t, i32, int32_t, value, + cpp_value); +__PB_RUST_EXPOSE_SCALAR_MAP_METHODS_FOR_VALUE_TYPE(uint32_t, u32, uint32_t, + value, cpp_value); +__PB_RUST_EXPOSE_SCALAR_MAP_METHODS_FOR_VALUE_TYPE(float, f32, float, value, + cpp_value); +__PB_RUST_EXPOSE_SCALAR_MAP_METHODS_FOR_VALUE_TYPE(double, f64, double, value, + cpp_value); +__PB_RUST_EXPOSE_SCALAR_MAP_METHODS_FOR_VALUE_TYPE(bool, bool, bool, value, + cpp_value); +__PB_RUST_EXPOSE_SCALAR_MAP_METHODS_FOR_VALUE_TYPE(uint64_t, u64, uint64_t, + value, cpp_value); +__PB_RUST_EXPOSE_SCALAR_MAP_METHODS_FOR_VALUE_TYPE(int64_t, i64, int64_t, value, + cpp_value); +__PB_RUST_EXPOSE_SCALAR_MAP_METHODS_FOR_VALUE_TYPE( + std::string, Bytes, google::protobuf::rust_internal::PtrAndLen, + std::string(value.ptr, value.len), + google::protobuf::rust_internal::PtrAndLen(cpp_value.data(), cpp_value.size())); +__PB_RUST_EXPOSE_SCALAR_MAP_METHODS_FOR_VALUE_TYPE( std::string, ProtoStr, google::protobuf::rust_internal::PtrAndLen, - std::string(key.ptr, key.len), - google::protobuf::rust_internal::PtrAndLen(cpp_key.data(), cpp_key.size())); - -#undef expose_scalar_map_methods -#undef expose_map_methods + std::string(value.ptr, value.len), + google::protobuf::rust_internal::PtrAndLen(cpp_value.data(), cpp_value.size())); } diff --git a/rust/cpp_kernel/cpp_api.h b/rust/cpp_kernel/cpp_api.h index 5bb468d050..5ef9317f98 100644 --- a/rust/cpp_kernel/cpp_api.h +++ b/rust/cpp_kernel/cpp_api.h @@ -61,6 +61,89 @@ struct PtrAndLen { PtrAndLen(const char* ptr, size_t len) : ptr(ptr), len(len) {} }; +// Defines concrete thunks to access typed map methods from Rust. +#define __PB_RUST_EXPOSE_SCALAR_MAP_METHODS( \ + key_ty, rust_key_ty, ffi_key_ty, to_cpp_key, to_ffi_key, value_ty, \ + rust_value_ty, ffi_value_ty, to_cpp_value, to_ffi_value) \ + google::protobuf::Map* \ + __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_new() { \ + return new google::protobuf::Map(); \ + } \ + void __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_free( \ + google::protobuf::Map* m) { \ + delete m; \ + } \ + void __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_clear( \ + google::protobuf::Map* m) { \ + m->clear(); \ + } \ + size_t __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_size( \ + const google::protobuf::Map* m) { \ + return m->size(); \ + } \ + bool __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_insert( \ + google::protobuf::Map* m, ffi_key_ty key, ffi_value_ty value) { \ + auto iter_and_inserted = m->try_emplace(to_cpp_key, to_cpp_value); \ + return iter_and_inserted.second; \ + } \ + bool __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_get( \ + const google::protobuf::Map* m, ffi_key_ty key, \ + ffi_value_ty* value) { \ + auto cpp_key = to_cpp_key; \ + auto it = m->find(cpp_key); \ + if (it == m->end()) { \ + return false; \ + } \ + auto& cpp_value = it->second; \ + *value = to_ffi_value; \ + return true; \ + } \ + google::protobuf::internal::UntypedMapIterator \ + __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_iter( \ + const google::protobuf::Map* m) { \ + return google::protobuf::internal::UntypedMapIterator::FromTyped(m->cbegin()); \ + } \ + void __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_iter_get( \ + const google::protobuf::internal::UntypedMapIterator* iter, ffi_key_ty* key, \ + ffi_value_ty* value) { \ + auto typed_iter = \ + iter->ToTyped::const_iterator>(); \ + const auto& cpp_key = typed_iter->first; \ + const auto& cpp_value = typed_iter->second; \ + *key = to_ffi_key; \ + *value = to_ffi_value; \ + } \ + bool __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_remove( \ + google::protobuf::Map* m, ffi_key_ty key, ffi_value_ty* value) { \ + auto cpp_key = to_cpp_key; \ + auto num_removed = m->erase(cpp_key); \ + return num_removed > 0; \ + } + +// Defines the map thunks for all supported key types for a given value type. +#define __PB_RUST_EXPOSE_SCALAR_MAP_METHODS_FOR_VALUE_TYPE( \ + value_ty, rust_value_ty, ffi_value_ty, to_cpp_value, to_ffi_value) \ + __PB_RUST_EXPOSE_SCALAR_MAP_METHODS(int32_t, i32, int32_t, key, cpp_key, \ + value_ty, rust_value_ty, ffi_value_ty, \ + to_cpp_value, to_ffi_value); \ + __PB_RUST_EXPOSE_SCALAR_MAP_METHODS(uint32_t, u32, uint32_t, key, cpp_key, \ + value_ty, rust_value_ty, ffi_value_ty, \ + to_cpp_value, to_ffi_value); \ + __PB_RUST_EXPOSE_SCALAR_MAP_METHODS(bool, bool, bool, key, cpp_key, \ + value_ty, rust_value_ty, ffi_value_ty, \ + to_cpp_value, to_ffi_value); \ + __PB_RUST_EXPOSE_SCALAR_MAP_METHODS(uint64_t, u64, uint64_t, key, cpp_key, \ + value_ty, rust_value_ty, ffi_value_ty, \ + to_cpp_value, to_ffi_value); \ + __PB_RUST_EXPOSE_SCALAR_MAP_METHODS(int64_t, i64, int64_t, key, cpp_key, \ + value_ty, rust_value_ty, ffi_value_ty, \ + to_cpp_value, to_ffi_value); \ + __PB_RUST_EXPOSE_SCALAR_MAP_METHODS( \ + std::string, ProtoStr, google::protobuf::rust_internal::PtrAndLen, \ + std::string(key.ptr, key.len), \ + google::protobuf::rust_internal::PtrAndLen(cpp_key.data(), cpp_key.size()), \ + value_ty, rust_value_ty, ffi_value_ty, to_cpp_value, to_ffi_value); + } // namespace rust_internal } // namespace protobuf } // namespace google