blob: 27034f09f6d946a06625d18b6ce0d74f4a5f1969 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001#pragma once
2#include <array>
David Tolnay30430f12020-03-19 20:49:00 -07003#include <cstddef>
David Tolnay7db73692019-10-20 14:51:12 -04004#include <cstdint>
David Tolnayb7a7cb62020-03-17 21:18:40 -07005#include <exception>
David Tolnay001102a2020-03-01 20:05:04 -08006#include <iosfwd>
David Tolnay7db73692019-10-20 14:51:12 -04007#include <string>
David Tolnayf6292372020-03-01 21:09:11 -08008#include <type_traits>
David Tolnay4791f1c2020-03-17 21:53:16 -07009#include <utility>
David Tolnay59b5ba12020-04-10 11:32:19 -070010#if defined(_WIN32)
11#include <BaseTsd.h>
12#endif
David Tolnay7db73692019-10-20 14:51:12 -040013
David Tolnay750755e2020-03-01 13:04:08 -080014namespace rust {
David Tolnay8c730492020-03-13 01:29:06 -070015inline namespace cxxbridge02 {
David Tolnay7db73692019-10-20 14:51:12 -040016
David Tolnayd1e2efc2020-03-03 22:25:43 -080017struct unsafe_bitcopy_t;
18
David Tolnayb7a7cb62020-03-17 21:18:40 -070019#ifndef CXXBRIDGE02_RUST_STRING
20#define CXXBRIDGE02_RUST_STRING
David Tolnay56082162020-03-01 12:57:33 -080021class String final {
David Tolnay7db73692019-10-20 14:51:12 -040022public:
David Tolnay56082162020-03-01 12:57:33 -080023 String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080024 String(const String &) noexcept;
25 String(String &&) noexcept;
David Tolnay56082162020-03-01 12:57:33 -080026 ~String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080027
28 String(const std::string &);
29 String(const char *);
30
31 String &operator=(const String &) noexcept;
32 String &operator=(String &&) noexcept;
33
David Tolnay404d6892020-03-01 20:19:41 -080034 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040035
36 // Note: no null terminator.
37 const char *data() const noexcept;
38 size_t size() const noexcept;
39 size_t length() const noexcept;
40
David Tolnayd1e2efc2020-03-03 22:25:43 -080041 // Internal API only intended for the cxxbridge code generator.
42 String(unsafe_bitcopy_t, const String &) noexcept;
43
David Tolnay7db73692019-10-20 14:51:12 -040044private:
45 // Size and alignment statically verified by rust_string.rs.
46 std::array<uintptr_t, 3> repr;
47};
David Tolnayb7a7cb62020-03-17 21:18:40 -070048#endif // CXXBRIDGE02_RUST_STRING
David Tolnay7db73692019-10-20 14:51:12 -040049
David Tolnayb7a7cb62020-03-17 21:18:40 -070050#ifndef CXXBRIDGE02_RUST_STR
51#define CXXBRIDGE02_RUST_STR
David Tolnay09dbe752020-03-01 13:00:40 -080052class Str final {
David Tolnay7db73692019-10-20 14:51:12 -040053public:
David Tolnay09dbe752020-03-01 13:00:40 -080054 Str() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080055 Str(const Str &) noexcept;
56
David Tolnay851677c2020-03-01 23:49:46 -080057 Str(const std::string &);
58 Str(const char *);
59 Str(std::string &&) = delete;
David Tolnayd9c4ac92020-03-01 20:33:58 -080060
61 Str &operator=(Str) noexcept;
62
David Tolnay404d6892020-03-01 20:19:41 -080063 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040064
65 // Note: no null terminator.
66 const char *data() const noexcept;
67 size_t size() const noexcept;
68 size_t length() const noexcept;
69
70 // Repr is PRIVATE; must not be used other than by our generated code.
71 //
72 // Not necessarily ABI compatible with &str. Codegen will translate to
73 // cxx::rust_str::RustStr which matches this layout.
74 struct Repr {
75 const char *ptr;
76 size_t len;
77 };
David Tolnayd9c4ac92020-03-01 20:33:58 -080078 Str(Repr) noexcept;
David Tolnaybaae4432020-03-01 20:20:10 -080079 explicit operator Repr() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -040080
81private:
82 Repr repr;
83};
David Tolnayb7a7cb62020-03-17 21:18:40 -070084#endif // CXXBRIDGE02_RUST_STR
David Tolnay7db73692019-10-20 14:51:12 -040085
David Tolnay8c730492020-03-13 01:29:06 -070086#ifndef CXXBRIDGE02_RUST_BOX
87#define CXXBRIDGE02_RUST_BOX
David Tolnay324437a2020-03-01 13:02:24 -080088template <typename T> class Box final {
David Tolnay7db73692019-10-20 14:51:12 -040089public:
David Tolnayf6292372020-03-01 21:09:11 -080090 using value_type = T;
David Tolnay9f921372020-03-01 21:09:25 -080091 using const_pointer = typename std::add_pointer<
92 typename std::add_const<value_type>::type>::type;
93 using pointer = typename std::add_pointer<value_type>::type;
David Tolnayf6292372020-03-01 21:09:11 -080094
David Tolnay324437a2020-03-01 13:02:24 -080095 Box(const Box &other) : Box(*other) {}
David Tolnay33169bd2020-03-06 13:02:08 -080096 Box(Box &&other) noexcept : ptr(other.ptr) { other.ptr = nullptr; }
David Tolnay324437a2020-03-01 13:02:24 -080097 Box(const T &val) {
David Tolnay7db73692019-10-20 14:51:12 -040098 this->uninit();
David Tolnay33169bd2020-03-06 13:02:08 -080099 ::new (this->ptr) T(val);
David Tolnay7db73692019-10-20 14:51:12 -0400100 }
David Tolnay324437a2020-03-01 13:02:24 -0800101 Box &operator=(const Box &other) {
David Tolnay7db73692019-10-20 14:51:12 -0400102 if (this != &other) {
David Tolnay33169bd2020-03-06 13:02:08 -0800103 if (this->ptr) {
David Tolnay7db73692019-10-20 14:51:12 -0400104 **this = *other;
105 } else {
106 this->uninit();
David Tolnay33169bd2020-03-06 13:02:08 -0800107 ::new (this->ptr) T(*other);
David Tolnay7db73692019-10-20 14:51:12 -0400108 }
109 }
110 return *this;
111 }
David Tolnay324437a2020-03-01 13:02:24 -0800112 Box &operator=(Box &&other) noexcept {
David Tolnay33169bd2020-03-06 13:02:08 -0800113 if (this->ptr) {
David Tolnay7db73692019-10-20 14:51:12 -0400114 this->drop();
115 }
David Tolnay33169bd2020-03-06 13:02:08 -0800116 this->ptr = other.ptr;
117 other.ptr = nullptr;
David Tolnay7db73692019-10-20 14:51:12 -0400118 return *this;
119 }
David Tolnay324437a2020-03-01 13:02:24 -0800120 ~Box() noexcept {
David Tolnay33169bd2020-03-06 13:02:08 -0800121 if (this->ptr) {
David Tolnay7db73692019-10-20 14:51:12 -0400122 this->drop();
123 }
124 }
125
David Tolnay33169bd2020-03-06 13:02:08 -0800126 const T *operator->() const noexcept { return this->ptr; }
127 const T &operator*() const noexcept { return *this->ptr; }
128 T *operator->() noexcept { return this->ptr; }
129 T &operator*() noexcept { return *this->ptr; }
David Tolnay7db73692019-10-20 14:51:12 -0400130
131 // Important: requires that `raw` came from an into_raw call. Do not pass a
132 // pointer from `new` or any other source.
David Tolnay324437a2020-03-01 13:02:24 -0800133 static Box from_raw(T *raw) noexcept {
134 Box box;
David Tolnay33169bd2020-03-06 13:02:08 -0800135 box.ptr = raw;
David Tolnay7db73692019-10-20 14:51:12 -0400136 return box;
137 }
138
139 T *into_raw() noexcept {
David Tolnay33169bd2020-03-06 13:02:08 -0800140 T *raw = this->ptr;
141 this->ptr = nullptr;
David Tolnay7db73692019-10-20 14:51:12 -0400142 return raw;
143 }
144
145private:
David Tolnay324437a2020-03-01 13:02:24 -0800146 Box() noexcept {}
David Tolnay7db73692019-10-20 14:51:12 -0400147 void uninit() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400148 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800149 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400150};
David Tolnay8c730492020-03-13 01:29:06 -0700151#endif // CXXBRIDGE02_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400152
David Tolnay75dca2e2020-03-25 20:17:52 -0700153#ifndef CXXBRIDGE02_RUST_FN
154#define CXXBRIDGE02_RUST_FN
155template <typename Signature, bool Throws = false> class Fn;
156
157template <typename Ret, typename... Args, bool Throws>
158class Fn<Ret(Args...), Throws> {
159public:
David Tolnay533d4582020-04-08 20:29:14 -0700160 Ret operator()(Args... args) const noexcept(!Throws);
161 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700162
163private:
164 Ret (*trampoline)(Args..., void *fn) noexcept(!Throws);
165 void *fn;
166};
167
168template <typename Signature> using TryFn = Fn<Signature, true>;
169#endif // CXXBRIDGE02_RUST_FN
170
David Tolnayb7a7cb62020-03-17 21:18:40 -0700171#ifndef CXXBRIDGE02_RUST_ERROR
172#define CXXBRIDGE02_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700173class Error final : std::exception {
174public:
175 Error(const Error &);
176 Error(Error &&) noexcept;
177 Error(Str::Repr) noexcept;
178 ~Error() noexcept;
179 const char *what() const noexcept override;
180
181private:
182 Str::Repr msg;
183};
David Tolnayb7a7cb62020-03-17 21:18:40 -0700184#endif // CXXBRIDGE02_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700185
David Tolnayb8a6fb22020-04-10 11:17:28 -0700186#ifndef CXXBRIDGE02_RUST_ISIZE
187#define CXXBRIDGE02_RUST_ISIZE
188#if defined(_WIN32)
189using isize = SSIZE_T;
190#else
191using isize = ssize_t;
192#endif
193#endif // CXXBRIDGE02_RUST_ISIZE
194
David Tolnay851677c2020-03-01 23:49:46 -0800195std::ostream &operator<<(std::ostream &, const String &);
196std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400197
David Tolnay3b0c9882020-03-01 14:08:57 -0800198// Snake case aliases for use in code that uses this style for type names.
199using string = String;
200using str = Str;
201template <class T> using box = Box<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700202using error = Error;
David Tolnay75dca2e2020-03-25 20:17:52 -0700203template <typename Signature, bool Throws = false>
204using fn = Fn<Signature, Throws>;
205template <typename Signature> using try_fn = TryFn<Signature>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800206
David Tolnayb7a7cb62020-03-17 21:18:40 -0700207#ifndef CXXBRIDGE02_RUST_BITCOPY
208#define CXXBRIDGE02_RUST_BITCOPY
David Tolnayd1e2efc2020-03-03 22:25:43 -0800209struct unsafe_bitcopy_t {
210 explicit unsafe_bitcopy_t() = default;
211};
212constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnayb7a7cb62020-03-17 21:18:40 -0700213#endif // CXXBRIDGE02_RUST_BITCOPY
David Tolnayd1e2efc2020-03-03 22:25:43 -0800214
David Tolnay75dca2e2020-03-25 20:17:52 -0700215template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700216Ret Fn<Ret(Args...), Throws>::operator()(Args... args) const noexcept(!Throws) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700217 return (*this->trampoline)(std::move(args)..., this->fn);
218}
219
David Tolnaya23129c2020-04-08 20:08:21 -0700220template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700221Fn<Ret(Args...), Throws> Fn<Ret(Args...), Throws>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700222 return *this;
223}
224
David Tolnay8c730492020-03-13 01:29:06 -0700225} // namespace cxxbridge02
David Tolnay750755e2020-03-01 13:04:08 -0800226} // namespace rust