parent
219e8ead2f
commit
419760f873
12 changed files with 254 additions and 171 deletions
@ -0,0 +1,23 @@ |
||||
#include "rust/cpp_kernel/debug.h" |
||||
|
||||
#include <string> |
||||
|
||||
#include "google/protobuf/message.h" |
||||
#include "google/protobuf/message_lite.h" |
||||
#include "rust/cpp_kernel/strings.h" |
||||
|
||||
extern "C" { |
||||
|
||||
google::protobuf::rust_internal::RustStringRawParts utf8_debug_string( |
||||
const google::protobuf::Message* msg) { |
||||
std::string text = google::protobuf::Utf8Format(*msg); |
||||
return google::protobuf::rust_internal::RustStringRawParts(text); |
||||
} |
||||
|
||||
google::protobuf::rust_internal::RustStringRawParts utf8_debug_string_lite( |
||||
const google::protobuf::MessageLite* msg) { |
||||
std::string text = google::protobuf::Utf8Format(*msg); |
||||
return google::protobuf::rust_internal::RustStringRawParts(text); |
||||
} |
||||
|
||||
} // extern "C"
|
@ -0,0 +1,18 @@ |
||||
#ifndef GOOGLE_PROTOBUF_RUST_CPP_KERNEL_DEBUG_H__ |
||||
#define GOOGLE_PROTOBUF_RUST_CPP_KERNEL_DEBUG_H__ |
||||
|
||||
#include "google/protobuf/message.h" |
||||
#include "google/protobuf/message_lite.h" |
||||
#include "rust/cpp_kernel/strings.h" |
||||
|
||||
extern "C" { |
||||
|
||||
google::protobuf::rust_internal::RustStringRawParts utf8_debug_string( |
||||
const google::protobuf::Message* msg); |
||||
|
||||
google::protobuf::rust_internal::RustStringRawParts utf8_debug_string_lite( |
||||
const google::protobuf::MessageLite* msg); |
||||
|
||||
} // extern "C"
|
||||
|
||||
#endif // GOOGLE_PROTOBUF_RUST_CPP_KERNEL_DEBUG_H__
|
@ -0,0 +1,39 @@ |
||||
#include "rust/cpp_kernel/map.h" |
||||
|
||||
#include <cstdint> |
||||
#include <string> |
||||
|
||||
#include "google/protobuf/map.h" |
||||
#include "rust/cpp_kernel/strings.h" |
||||
|
||||
extern "C" { |
||||
|
||||
void __rust_proto_thunk__UntypedMapIterator_increment( |
||||
google::protobuf::internal::UntypedMapIterator* iter) { |
||||
iter->PlusPlus(); |
||||
} |
||||
|
||||
__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, ProtoBytes, 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, ProtoString, google::protobuf::rust_internal::PtrAndLen, |
||||
std::string(value.ptr, value.len), |
||||
google::protobuf::rust_internal::PtrAndLen(cpp_value.data(), cpp_value.size())); |
||||
|
||||
} // extern "C"
|
@ -0,0 +1,11 @@ |
||||
#ifndef GOOGLE_PROTOBUF_RUST_CPP_KERNEL_RUST_ALLOC_FOR_CPP_API_H__ |
||||
#define GOOGLE_PROTOBUF_RUST_CPP_KERNEL_RUST_ALLOC_FOR_CPP_API_H__ |
||||
|
||||
#include <cstddef> |
||||
|
||||
// Allocates memory using the current Rust global allocator.
|
||||
//
|
||||
// This function is defined in `rust_alloc_for_cpp_api.rs`.
|
||||
extern "C" void* __pb_rust_alloc(size_t size, size_t align); |
||||
|
||||
#endif // GOOGLE_PROTOBUF_RUST_CPP_KERNEL_RUST_ALLOC_FOR_CPP_API_H__
|
@ -0,0 +1,64 @@ |
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2024 Google LLC. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file or at
|
||||
// https://developers.google.com/open-source/licenses/bsd
|
||||
|
||||
#ifndef GOOGLE_PROTOBUF_RUST_CPP_KERNEL_SERIALIZED_DATA_H__ |
||||
#define GOOGLE_PROTOBUF_RUST_CPP_KERNEL_SERIALIZED_DATA_H__ |
||||
|
||||
#include <climits> |
||||
#include <cstddef> |
||||
#include <cstdint> |
||||
#include <cstring> |
||||
|
||||
#include "absl/log/absl_check.h" |
||||
#include "absl/log/absl_log.h" |
||||
#include "google/protobuf/message_lite.h" |
||||
#include "rust/cpp_kernel/rust_alloc_for_cpp_api.h" |
||||
|
||||
namespace google { |
||||
namespace protobuf { |
||||
namespace rust_internal { |
||||
|
||||
// Represents serialized Protobuf wire format data.
|
||||
//
|
||||
// Only to be used to transfer serialized data from C++ to Rust under these
|
||||
// assumptions:
|
||||
// * Rust and C++ versions of this struct are ABI compatible.
|
||||
// * Rust version owns and frees its data.
|
||||
// * The data were allocated using the Rust allocator.
|
||||
//
|
||||
extern "C" struct SerializedData { |
||||
// Owns the memory, must be freed by Rust.
|
||||
const uint8_t* data; |
||||
size_t len; |
||||
|
||||
SerializedData(const uint8_t* data, size_t len) : data(data), len(len) {} |
||||
}; |
||||
|
||||
inline bool SerializeMsg(const google::protobuf::MessageLite* msg, SerializedData* out) { |
||||
ABSL_DCHECK(msg->IsInitialized()); |
||||
size_t len = msg->ByteSizeLong(); |
||||
if (len > INT_MAX) { |
||||
ABSL_LOG(ERROR) << msg->GetTypeName() |
||||
<< " exceeded maximum protobuf size of 2GB: " << len; |
||||
return false; |
||||
} |
||||
uint8_t* bytes = static_cast<uint8_t*>(__pb_rust_alloc(len, alignof(char))); |
||||
if (bytes == nullptr) { |
||||
ABSL_LOG(FATAL) << "Rust allocator failed to allocate memory."; |
||||
} |
||||
if (!msg->SerializeWithCachedSizesToArray(bytes)) { |
||||
return false; |
||||
} |
||||
*out = SerializedData(bytes, len); |
||||
return true; |
||||
} |
||||
|
||||
} // namespace rust_internal
|
||||
} // namespace protobuf
|
||||
} // namespace google
|
||||
|
||||
#endif // GOOGLE_PROTOBUF_RUST_CPP_KERNEL_SERIALIZED_DATA_H__
|
@ -0,0 +1,26 @@ |
||||
#include "rust/cpp_kernel/strings.h" |
||||
|
||||
#include <cstring> |
||||
#include <string> |
||||
|
||||
#include "rust/cpp_kernel/rust_alloc_for_cpp_api.h" |
||||
|
||||
namespace google { |
||||
namespace protobuf { |
||||
namespace rust_internal { |
||||
|
||||
RustStringRawParts::RustStringRawParts(std::string src) { |
||||
if (src.empty()) { |
||||
data = nullptr; |
||||
len = 0; |
||||
} else { |
||||
void* d = __pb_rust_alloc(src.length(), 1); |
||||
std::memcpy(d, src.data(), src.length()); |
||||
data = static_cast<char*>(d); |
||||
len = src.length(); |
||||
} |
||||
} |
||||
|
||||
} // namespace rust_internal
|
||||
} // namespace protobuf
|
||||
} // namespace google
|
@ -0,0 +1,50 @@ |
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2024 Google LLC. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file or at
|
||||
// https://developers.google.com/open-source/licenses/bsd
|
||||
|
||||
#ifndef GOOGLE_PROTOBUF_RUST_CPP_KERNEL_STRINGS_H__ |
||||
#define GOOGLE_PROTOBUF_RUST_CPP_KERNEL_STRINGS_H__ |
||||
|
||||
#include <cstddef> |
||||
#include <cstring> |
||||
#include <string> |
||||
|
||||
namespace google { |
||||
namespace protobuf { |
||||
namespace rust_internal { |
||||
|
||||
// Represents an ABI-stable version of &[u8]/string_view (borrowed slice of
|
||||
// bytes) for FFI use only.
|
||||
struct PtrAndLen { |
||||
/// Borrows the memory.
|
||||
const char* ptr; |
||||
size_t len; |
||||
|
||||
PtrAndLen(const char* ptr, size_t len) : ptr(ptr), len(len) {} |
||||
}; |
||||
|
||||
// Represents an owned string for FFI purposes.
|
||||
//
|
||||
// This must only be used to transfer a string from C++ to Rust. The
|
||||
// below invariants must hold:
|
||||
// * Rust and C++ versions of this struct are ABI compatible.
|
||||
// * The data were allocated using the Rust allocator and are 1 byte aligned.
|
||||
// * The data is valid UTF-8.
|
||||
struct RustStringRawParts { |
||||
// Owns the memory.
|
||||
const char* data; |
||||
size_t len; |
||||
|
||||
RustStringRawParts() = delete; |
||||
// Copies src.
|
||||
explicit RustStringRawParts(std::string src); |
||||
}; |
||||
|
||||
} // namespace rust_internal
|
||||
} // namespace protobuf
|
||||
} // namespace google
|
||||
|
||||
#endif // GOOGLE_PROTOBUF_RUST_CPP_KERNEL_STRINGS_H__
|
Loading…
Reference in new issue