blob: 975743bf3b1a83af5c6d4f5ea39a91f39710254f [file]
// Protocol Buffers - Google's data interchange format
// Copyright 2023 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
#include <cstddef>
#include <cstdint>
#include "absl/log/absl_check.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
#include "rust/cpp_kernel/serialized_data.h"
#include "rust/cpp_kernel/strings.h"
#include "rust/test/cpp/interop/interop_test.pb.h"
using google::protobuf::rust::SerializedData;
using google::protobuf::rust::SerializeMsg;
using rust_cpp_interop_test::InteropTestMessage;
extern "C" void MutateInteropTestMessage(InteropTestMessage* msg) {
msg->set_i64(42);
msg->set_bytes("something mysterious");
msg->set_b(false);
}
extern "C" SerializedData SerializeInteropTestMessage(
const InteropTestMessage* msg) {
SerializedData data;
ABSL_CHECK(SerializeMsg(msg, &data));
return data;
}
extern "C" void DeleteInteropTestMessage(InteropTestMessage* msg) {
delete msg;
}
extern "C" void* DeserializeInteropTestMessage(const void* data, size_t size) {
auto* proto = new InteropTestMessage;
ABSL_CHECK(proto->ParseFromArray(data, static_cast<int>(size)));
return proto;
}
extern "C" void* NewWithExtension() {
auto* proto = new InteropTestMessage;
proto->SetExtension(rust_cpp_interop_test::bytes_extension, "smuggled");
return proto;
}
extern "C" google::protobuf::rust::PtrAndLen GetBytesExtension(
const InteropTestMessage* proto) {
absl::string_view bytes =
proto->GetExtension(rust_cpp_interop_test::bytes_extension);
return {bytes.data(), bytes.size()};
}
extern "C" int64_t TakeOwnershipAndGetOptionalInt64(InteropTestMessage* msg) {
int64_t i = msg->i64();
delete msg;
return i;
}
extern "C" const void* GetConstStaticInteropTestMessage() {
static const auto* msg = new InteropTestMessage;
return msg;
}
extern "C" bool IsExpectedDescriptor(const google::protobuf::Descriptor* descriptor) {
return descriptor == InteropTestMessage::GetDescriptor();
}
extern "C" void* NewArenaInteropTestMessage(google::protobuf::Arena** out_arena) {
*out_arena = new google::protobuf::Arena();
return google::protobuf::Arena::Create<InteropTestMessage>(*out_arena);
}
extern "C" void VerifyAndDeleteArena(google::protobuf::Arena* arena,
InteropTestMessage* msg) {
const InteropTestMessage& sub =
msg->map_string_msg().at("non_sso_map_key_exceeding_32_bytes_1");
ABSL_CHECK_EQ(sub.GetArena(), arena);
ABSL_CHECK_EQ(sub.bytes(), "non_sso_submessage_bytes_exceeding_32_bytes");
ABSL_CHECK_EQ(msg->map_string_bytes().size(), 1u);
ABSL_CHECK_EQ(
msg->map_string_bytes().at("non_sso_map_key_exceeding_32_bytes_2"),
"non_sso_bytes_value_exceeding_32_bytes");
delete arena;
}