blob: 4be5eca371a460dfa5a27f135499b61f09e9ce63 [file] [log] [blame]
Marcel Hlopko884ae7f2021-08-18 13:58:22 +00001// Part of the Crubit project, under the Apache License v2.0 with LLVM
2// Exceptions. See /LICENSE for license information.
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
Marcel Hlopko65978eb2022-04-08 07:40:18 -07005#ifndef CRUBIT_COMMON_FFI_TYPES_H_
6#define CRUBIT_COMMON_FFI_TYPES_H_
Marcel Hlopko884ae7f2021-08-18 13:58:22 +00007
8#include <cstddef>
9
Lukasz Anforowiczcec7a8a2022-04-27 10:24:51 -070010#include "absl/strings/string_view.h"
Marcel Hlopko884ae7f2021-08-18 13:58:22 +000011
Marcel Hlopkof15e8ce2022-04-08 08:46:09 -070012namespace crubit {
Marcel Hlopko884ae7f2021-08-18 13:58:22 +000013
14// Owned, Rust-allocated byte array. Call `FreeFfiU8SliceBox` to
15// deallocate.
16struct FfiU8SliceBox {
Marcel Hlopkoe936aac2021-08-24 20:52:27 +000017 const char* ptr;
Marcel Hlopko884ae7f2021-08-18 13:58:22 +000018 size_t size;
19};
20
21// Borrowed byte array.
22struct FfiU8Slice {
Marcel Hlopkoe936aac2021-08-24 20:52:27 +000023 const char* ptr;
Marcel Hlopko884ae7f2021-08-18 13:58:22 +000024 size_t size;
25};
26
Googlerf427e6a2021-10-01 07:59:59 +000027// Returns an `FfiU8Slice` referencing the same data as `s`.
Marcel Hlopko884ae7f2021-08-18 13:58:22 +000028FfiU8Slice MakeFfiU8Slice(absl::string_view s);
29
Googlerf427e6a2021-10-01 07:59:59 +000030// Returns a `string_view` referencing the same data as `ffi_u8_slice`.
31absl::string_view StringViewFromFfiU8Slice(FfiU8Slice ffi_u8_slice);
32
33// Returns an `FfiU8SliceBox` containing a copy of the data in `ffi_u8_slice`.
34// The returned `FfiU8SliceBox` must be freed by calling `FreeFfiU8SliceBox()`.
35// Implemented in Rust.
36extern "C" FfiU8SliceBox AllocFfiU8SliceBox(FfiU8Slice ffi_u8_slice);
37
38// Frees the memory associated with an `FfiU8SliceBox`.
39// Implemented in Rust.
Marcel Hlopko884ae7f2021-08-18 13:58:22 +000040extern "C" void FreeFfiU8SliceBox(FfiU8SliceBox);
41
Googler69e09632023-03-03 12:18:35 -080042// Whether or not the generated binding will have doc comments indicating their
43// source location.
44enum SourceLocationDocComment {
45 Disabled,
46 Enabled,
47};
48
Marcel Hlopkof15e8ce2022-04-08 08:46:09 -070049} // namespace crubit
Marcel Hlopko884ae7f2021-08-18 13:58:22 +000050
Marcel Hlopko65978eb2022-04-08 07:40:18 -070051#endif // CRUBIT_COMMON_FFI_TYPES_H_