dep-protobuf/rust/cpp_kernel/strings.h
Protobuf Team Bot 7fbb3d2cc7 Make conversion functions for into c++ string types from PtrAndLen.
The invariant is that PtrAndLen may hold ptr+len values which are legal for _either_ C++ string_view or Rust slices: constructing one from either Rust or C++ permits the laxest constraints, and care must be taken when converting a PtrAndLen into either type.

For "into Rust slice" case to handle is that len=0 ptr must be non-null, so len=0 ptr=null gets turned into an arbitrary non-null pointer as provided by std::ptr::NonNull::dangling() (any preexisting non-null ptr is kept in that direction).

For the "into C++ slice" the risk is more obscure that a Rust non-null pointer could potentially be an illegal pointer (such that ptr+0 is not a legal operation in C++), so when going into C++ we map any len=0 cases into ptr=null to avoid this as a possible risk.

PiperOrigin-RevId: 704776210
2024-12-10 11:15:00 -08:00

76 lines
2.4 KiB
C++

// 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>
#include "absl/strings/string_view.h"
namespace google {
namespace protobuf {
namespace rust {
// Represents an ABI-stable version of &[u8]/string_view (borrowed slice of
// bytes) for FFI use only.
//
// Note that the intent is for either Rust or C++ to construct one of these with
// the pointer that they would have from a Rust slice or C++ string_view.
// Notably this means that if len==0, ptr may be any value, including nullptr or
// an invalid pointer, which may be a value incompatible for use with either a
// Rust slice or C++ string_view.
//
// It may be constructed trivially, but use the provided conversion methods
// when converting from this type into any other type to avoid obscure undefined
// behavior.
struct PtrAndLen {
/// Borrows the memory.
const char* ptr;
size_t len;
std::string CopyToString() const;
absl::string_view AsStringView() const;
void PlacementNewString(void* location);
};
// 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
} // namespace protobuf
} // namespace google
extern "C" {
// Allocates a new std::string on the C++ heap and returns a pointer to it.
std::string* proto2_rust_cpp_new_string(google::protobuf::rust::PtrAndLen src);
// Deletes a std::string object from the C++ heap.
void proto2_rust_cpp_delete_string(std::string* str);
// Obtain a PtrAndLen, the FFI-safe view type, from a std::string.
google::protobuf::rust::PtrAndLen proto2_rust_cpp_string_to_view(std::string* str);
}
#endif // GOOGLE_PROTOBUF_RUST_CPP_KERNEL_STRINGS_H__