1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2023 Google LLC. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 #include <cstddef> 9 #include <cstdint> 10 11 #include "absl/log/absl_check.h" 12 #include "absl/strings/string_view.h" 13 #include "rust/cpp_kernel/serialized_data.h" 14 #include "rust/cpp_kernel/strings.h" 15 #include "google/protobuf/unittest.pb.h" 16 17 using google::protobuf::rust::SerializedData; 18 using google::protobuf::rust::SerializeMsg; 19 MutateTestAllTypes(protobuf_unittest::TestAllTypes * msg)20extern "C" void MutateTestAllTypes(protobuf_unittest::TestAllTypes* msg) { 21 msg->set_optional_int64(42); 22 msg->set_optional_bytes("something mysterious"); 23 msg->set_optional_bool(false); 24 } 25 SerializeTestAllTypes(const protobuf_unittest::TestAllTypes * msg)26extern "C" SerializedData SerializeTestAllTypes( 27 const protobuf_unittest::TestAllTypes* msg) { 28 SerializedData data; 29 ABSL_CHECK(SerializeMsg(msg, &data)); 30 return data; 31 } 32 DeleteTestAllTypes(protobuf_unittest::TestAllTypes * msg)33extern "C" void DeleteTestAllTypes(protobuf_unittest::TestAllTypes* msg) { 34 delete msg; 35 } 36 DeserializeTestAllTypes(const void * data,size_t size)37extern "C" void* DeserializeTestAllTypes(const void* data, size_t size) { 38 auto* proto = new protobuf_unittest::TestAllTypes; 39 proto->ParseFromArray(data, static_cast<int>(size)); 40 return proto; 41 } 42 NewWithExtension()43extern "C" void* NewWithExtension() { 44 auto* proto = new protobuf_unittest::TestAllExtensions; 45 proto->SetExtension(protobuf_unittest::optional_bytes_extension, "smuggled"); 46 return proto; 47 } 48 GetBytesExtension(const protobuf_unittest::TestAllExtensions * proto)49extern "C" google::protobuf::rust::PtrAndLen GetBytesExtension( 50 const protobuf_unittest::TestAllExtensions* proto) { 51 absl::string_view bytes = 52 proto->GetExtension(protobuf_unittest::optional_bytes_extension); 53 return {bytes.data(), bytes.size()}; 54 } 55 TakeOwnershipAndGetOptionalInt32(protobuf_unittest::TestAllTypes * msg)56extern "C" int32_t TakeOwnershipAndGetOptionalInt32( 57 protobuf_unittest::TestAllTypes* msg) { 58 int32_t i = msg->optional_int32(); 59 delete msg; 60 return i; 61 } 62 GetConstStaticTestAllTypes()63extern "C" const void* GetConstStaticTestAllTypes() { 64 static const auto* msg = new protobuf_unittest::TestAllTypes; 65 return msg; 66 } 67