1 #include "rust/cpp_kernel/strings.h" 2 3 #include <cstring> 4 #include <string> 5 6 #include "rust/cpp_kernel/rust_alloc_for_cpp_api.h" 7 8 namespace google { 9 namespace protobuf { 10 namespace rust { 11 RustStringRawParts(std::string src)12RustStringRawParts::RustStringRawParts(std::string src) { 13 if (src.empty()) { 14 data = nullptr; 15 len = 0; 16 } else { 17 void* d = proto2_rust_alloc(src.length(), 1); 18 std::memcpy(d, src.data(), src.length()); 19 data = static_cast<char*>(d); 20 len = src.length(); 21 } 22 } 23 24 } // namespace rust 25 } // namespace protobuf 26 } // namespace google 27 28 extern "C" { proto2_rust_cpp_new_string(google::protobuf::rust::PtrAndLen src)29std::string* proto2_rust_cpp_new_string(google::protobuf::rust::PtrAndLen src) { 30 return new std::string(src.ptr, src.len); 31 } 32 proto2_rust_cpp_delete_string(std::string * str)33void proto2_rust_cpp_delete_string(std::string* str) { delete str; } 34 proto2_rust_cpp_string_to_view(std::string * str)35google::protobuf::rust::PtrAndLen proto2_rust_cpp_string_to_view(std::string* str) { 36 return google::protobuf::rust::PtrAndLen{str->data(), str->length()}; 37 } 38 } 39