• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2024 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 #ifndef GOOGLE_PROTOBUF_RUST_CPP_KERNEL_STRINGS_H__
9 #define GOOGLE_PROTOBUF_RUST_CPP_KERNEL_STRINGS_H__
10 
11 #include <cstddef>
12 #include <cstring>
13 #include <string>
14 
15 namespace google {
16 namespace protobuf {
17 namespace rust {
18 
19 // Represents an ABI-stable version of &[u8]/string_view (borrowed slice of
20 // bytes) for FFI use only.
21 struct PtrAndLen {
22   /// Borrows the memory.
23   const char* ptr;
24   size_t len;
25 };
26 
27 // Represents an owned string for FFI purposes.
28 //
29 // This must only be used to transfer a string from C++ to Rust. The
30 // below invariants must hold:
31 //   * Rust and C++ versions of this struct are ABI compatible.
32 //   * The data were allocated using the Rust allocator and are 1 byte aligned.
33 //   * The data is valid UTF-8.
34 struct RustStringRawParts {
35   // Owns the memory.
36   const char* data;
37   size_t len;
38 
39   RustStringRawParts() = delete;
40   // Copies src.
41   explicit RustStringRawParts(std::string src);
42 };
43 
44 }  // namespace rust
45 }  // namespace protobuf
46 }  // namespace google
47 
48 extern "C" {
49 
50 // Allocates a new std::string on the C++ heap and returns a pointer to it.
51 std::string* proto2_rust_cpp_new_string(google::protobuf::rust::PtrAndLen src);
52 
53 // Deletes a std::string object from the C++ heap.
54 void proto2_rust_cpp_delete_string(std::string* str);
55 
56 // Obtain a PtrAndLen, the FFI-safe view type, from a std::string.
57 google::protobuf::rust::PtrAndLen proto2_rust_cpp_string_to_view(std::string* str);
58 }
59 
60 #endif  // GOOGLE_PROTOBUF_RUST_CPP_KERNEL_STRINGS_H__
61