blob: 1e33618488145d69c74480d9162fc2859b51a62e [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 Tolnay37dd7e12020-04-25 12:51:59 -070010#include <vector>
David Tolnay59b5ba12020-04-10 11:32:19 -070011#if defined(_WIN32)
12#include <BaseTsd.h>
13#endif
David Tolnay7db73692019-10-20 14:51:12 -040014
David Tolnay750755e2020-03-01 13:04:08 -080015namespace rust {
David Tolnay8c730492020-03-13 01:29:06 -070016inline namespace cxxbridge02 {
David Tolnay7db73692019-10-20 14:51:12 -040017
David Tolnay313b10e2020-04-25 16:30:51 -070018#ifndef CXXBRIDGE02_RUST_BITCOPY
19#define CXXBRIDGE02_RUST_BITCOPY
20struct unsafe_bitcopy_t {
21 explicit unsafe_bitcopy_t() = default;
22};
23constexpr unsafe_bitcopy_t unsafe_bitcopy{};
24#endif // CXXBRIDGE02_RUST_BITCOPY
David Tolnayd1e2efc2020-03-03 22:25:43 -080025
David Tolnayb7a7cb62020-03-17 21:18:40 -070026#ifndef CXXBRIDGE02_RUST_STRING
27#define CXXBRIDGE02_RUST_STRING
David Tolnay56082162020-03-01 12:57:33 -080028class String final {
David Tolnay7db73692019-10-20 14:51:12 -040029public:
David Tolnay56082162020-03-01 12:57:33 -080030 String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080031 String(const String &) noexcept;
32 String(String &&) noexcept;
David Tolnay56082162020-03-01 12:57:33 -080033 ~String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080034
35 String(const std::string &);
36 String(const char *);
37
38 String &operator=(const String &) noexcept;
39 String &operator=(String &&) noexcept;
40
David Tolnay404d6892020-03-01 20:19:41 -080041 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040042
43 // Note: no null terminator.
44 const char *data() const noexcept;
45 size_t size() const noexcept;
46 size_t length() const noexcept;
47
David Tolnayd1e2efc2020-03-03 22:25:43 -080048 // Internal API only intended for the cxxbridge code generator.
49 String(unsafe_bitcopy_t, const String &) noexcept;
50
David Tolnay7db73692019-10-20 14:51:12 -040051private:
52 // Size and alignment statically verified by rust_string.rs.
53 std::array<uintptr_t, 3> repr;
54};
David Tolnayb7a7cb62020-03-17 21:18:40 -070055#endif // CXXBRIDGE02_RUST_STRING
David Tolnay7db73692019-10-20 14:51:12 -040056
David Tolnayb7a7cb62020-03-17 21:18:40 -070057#ifndef CXXBRIDGE02_RUST_STR
58#define CXXBRIDGE02_RUST_STR
David Tolnay09dbe752020-03-01 13:00:40 -080059class Str final {
David Tolnay7db73692019-10-20 14:51:12 -040060public:
David Tolnay09dbe752020-03-01 13:00:40 -080061 Str() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080062 Str(const Str &) noexcept;
63
David Tolnay851677c2020-03-01 23:49:46 -080064 Str(const std::string &);
65 Str(const char *);
66 Str(std::string &&) = delete;
David Tolnayd9c4ac92020-03-01 20:33:58 -080067
68 Str &operator=(Str) noexcept;
69
David Tolnay404d6892020-03-01 20:19:41 -080070 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040071
72 // Note: no null terminator.
73 const char *data() const noexcept;
74 size_t size() const noexcept;
75 size_t length() const noexcept;
76
77 // Repr is PRIVATE; must not be used other than by our generated code.
78 //
79 // Not necessarily ABI compatible with &str. Codegen will translate to
80 // cxx::rust_str::RustStr which matches this layout.
81 struct Repr {
82 const char *ptr;
83 size_t len;
84 };
David Tolnayd9c4ac92020-03-01 20:33:58 -080085 Str(Repr) noexcept;
David Tolnaybaae4432020-03-01 20:20:10 -080086 explicit operator Repr() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -040087
88private:
89 Repr repr;
90};
David Tolnayb7a7cb62020-03-17 21:18:40 -070091#endif // CXXBRIDGE02_RUST_STR
David Tolnay7db73692019-10-20 14:51:12 -040092
David Tolnayefe81052020-04-14 16:28:24 -070093#ifndef CXXBRIDGE02_RUST_SLICE
94#define CXXBRIDGE02_RUST_SLICE
95template <typename T>
96class Slice final {
97public:
98 Slice() noexcept : repr(Repr{reinterpret_cast<const T *>(this), 0}) {}
99 Slice(const Slice<T> &) noexcept = default;
100
101 Slice(const T *s, size_t size) : repr(Repr{s, size}) {}
102
103 Slice &operator=(Slice<T> other) noexcept {
104 this->repr = other.repr;
105 return *this;
106 }
107
108 const T *data() const noexcept { return this->repr.ptr; }
109 size_t size() const noexcept { return this->repr.len; }
110 size_t length() const noexcept { return this->repr.len; }
111
112 // Repr is PRIVATE; must not be used other than by our generated code.
113 //
114 // At present this class is only used for &[u8] slices.
115 // Not necessarily ABI compatible with &[u8]. Codegen will translate to
David Tolnaye710af12020-04-14 16:31:54 -0700116 // cxx::rust_sliceu8::RustSliceU8 which matches this layout.
David Tolnayefe81052020-04-14 16:28:24 -0700117 struct Repr {
118 const T *ptr;
119 size_t len;
120 };
121 Slice(Repr repr_) noexcept : repr(repr_) {}
122 explicit operator Repr() noexcept { return this->repr; }
123
124private:
125 Repr repr;
126};
127#endif // CXXBRIDGE02_RUST_SLICE
128
David Tolnay8c730492020-03-13 01:29:06 -0700129#ifndef CXXBRIDGE02_RUST_BOX
130#define CXXBRIDGE02_RUST_BOX
David Tolnayf262d382020-04-11 22:12:40 -0700131template <typename T>
132class Box final {
David Tolnay7db73692019-10-20 14:51:12 -0400133public:
David Tolnayf6292372020-03-01 21:09:11 -0800134 using value_type = T;
David Tolnay9706a512020-04-24 17:09:01 -0700135 using const_pointer =
136 typename std::add_pointer<typename std::add_const<T>::type>::type;
137 using pointer = typename std::add_pointer<T>::type;
David Tolnayf6292372020-03-01 21:09:11 -0800138
David Tolnay324437a2020-03-01 13:02:24 -0800139 Box(const Box &other) : Box(*other) {}
David Tolnay33169bd2020-03-06 13:02:08 -0800140 Box(Box &&other) noexcept : ptr(other.ptr) { other.ptr = nullptr; }
David Tolnay7db7dad2020-04-11 14:12:49 -0700141 explicit Box(const T &val) {
David Tolnay7db73692019-10-20 14:51:12 -0400142 this->uninit();
David Tolnay33169bd2020-03-06 13:02:08 -0800143 ::new (this->ptr) T(val);
David Tolnay7db73692019-10-20 14:51:12 -0400144 }
David Tolnay7db7dad2020-04-11 14:12:49 -0700145 explicit Box(T &&val) {
David Tolnay47b3cf22020-04-11 00:54:39 -0700146 this->uninit();
147 ::new (this->ptr) T(std::move(val));
148 }
David Tolnay324437a2020-03-01 13:02:24 -0800149 Box &operator=(const Box &other) {
David Tolnay7db73692019-10-20 14:51:12 -0400150 if (this != &other) {
David Tolnay33169bd2020-03-06 13:02:08 -0800151 if (this->ptr) {
David Tolnay7db73692019-10-20 14:51:12 -0400152 **this = *other;
153 } else {
154 this->uninit();
David Tolnay33169bd2020-03-06 13:02:08 -0800155 ::new (this->ptr) T(*other);
David Tolnay7db73692019-10-20 14:51:12 -0400156 }
157 }
158 return *this;
159 }
David Tolnay324437a2020-03-01 13:02:24 -0800160 Box &operator=(Box &&other) noexcept {
David Tolnay33169bd2020-03-06 13:02:08 -0800161 if (this->ptr) {
David Tolnay7db73692019-10-20 14:51:12 -0400162 this->drop();
163 }
David Tolnay33169bd2020-03-06 13:02:08 -0800164 this->ptr = other.ptr;
165 other.ptr = nullptr;
David Tolnay7db73692019-10-20 14:51:12 -0400166 return *this;
167 }
David Tolnay324437a2020-03-01 13:02:24 -0800168 ~Box() noexcept {
David Tolnay33169bd2020-03-06 13:02:08 -0800169 if (this->ptr) {
David Tolnay7db73692019-10-20 14:51:12 -0400170 this->drop();
171 }
172 }
173
David Tolnay33169bd2020-03-06 13:02:08 -0800174 const T *operator->() const noexcept { return this->ptr; }
175 const T &operator*() const noexcept { return *this->ptr; }
176 T *operator->() noexcept { return this->ptr; }
177 T &operator*() noexcept { return *this->ptr; }
David Tolnay7db73692019-10-20 14:51:12 -0400178
David Tolnayf262d382020-04-11 22:12:40 -0700179 template <typename... Fields>
180 static Box in_place(Fields &&... fields) {
David Tolnay7ce59fc2020-04-11 11:46:33 -0700181 Box box;
182 box.uninit();
183 ::new (box.ptr) T{std::forward<Fields>(fields)...};
184 return box;
185 }
186
David Tolnay7db73692019-10-20 14:51:12 -0400187 // Important: requires that `raw` came from an into_raw call. Do not pass a
188 // pointer from `new` or any other source.
David Tolnay324437a2020-03-01 13:02:24 -0800189 static Box from_raw(T *raw) noexcept {
190 Box box;
David Tolnay33169bd2020-03-06 13:02:08 -0800191 box.ptr = raw;
David Tolnay7db73692019-10-20 14:51:12 -0400192 return box;
193 }
194
195 T *into_raw() noexcept {
David Tolnay33169bd2020-03-06 13:02:08 -0800196 T *raw = this->ptr;
197 this->ptr = nullptr;
David Tolnay7db73692019-10-20 14:51:12 -0400198 return raw;
199 }
200
201private:
David Tolnay324437a2020-03-01 13:02:24 -0800202 Box() noexcept {}
David Tolnay7db73692019-10-20 14:51:12 -0400203 void uninit() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400204 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800205 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400206};
David Tolnay8c730492020-03-13 01:29:06 -0700207#endif // CXXBRIDGE02_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400208
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700209#ifndef CXXBRIDGE02_RUST_VEC
210#define CXXBRIDGE02_RUST_VEC
211template <typename T>
212class Vec final {
213public:
David Tolnayc87c2152020-04-24 17:07:41 -0700214 using value_type = T;
215
David Tolnayf97c2d52020-04-25 16:37:48 -0700216 Vec() noexcept;
217 Vec(Vec &&other) noexcept {
218 this->repr = other.repr;
219 new (&other) Vec();
220 }
David Tolnaycb800572020-04-24 20:30:43 -0700221 ~Vec() noexcept { this->drop(); }
222
David Tolnayf97c2d52020-04-25 16:37:48 -0700223 Vec &operator=(Vec &&other) noexcept {
224 if (this != &other) {
225 this->drop();
226 this->repr = other.repr;
227 new (&other) Vec();
228 }
229 return *this;
230 }
231
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700232 size_t size() const noexcept;
David Tolnay465305b2020-04-24 16:48:18 -0700233 bool empty() const noexcept { return size() == 0; }
David Tolnay219c0792020-04-24 20:31:37 -0700234 const T *data() const noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700235
David Tolnayc87c2152020-04-24 17:07:41 -0700236 class const_iterator {
237 public:
myronahnda9be502020-04-29 05:47:23 +0700238 using difference_type = ptrdiff_t;
David Tolnayc87c2152020-04-24 17:07:41 -0700239 using value_type = typename std::add_const<T>::type;
myronahnda9be502020-04-29 05:47:23 +0700240 using pointer = typename std::add_pointer<
241 typename std::add_const<T>::type>::type;
David Tolnayc87c2152020-04-24 17:07:41 -0700242 using reference = typename std::add_lvalue_reference<
243 typename std::add_const<T>::type>::type;
myronahnda9be502020-04-29 05:47:23 +0700244 using iterator_category = std::forward_iterator_tag;
David Tolnayc87c2152020-04-24 17:07:41 -0700245
246 const T &operator*() const { return *static_cast<const T *>(this->pos); }
myronahnda9be502020-04-29 05:47:23 +0700247 const T *operator->() const { return static_cast<const T *>(this->pos); }
David Tolnayc87c2152020-04-24 17:07:41 -0700248 const_iterator &operator++() {
249 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
250 return *this;
251 }
myronahnda9be502020-04-29 05:47:23 +0700252 const_iterator operator++(int) {
253 auto ret = const_iterator(*this);
254 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
255 return ret;
256 }
David Tolnayc87c2152020-04-24 17:07:41 -0700257 bool operator==(const const_iterator &other) const {
258 return this->pos == other.pos;
259 }
260 bool operator!=(const const_iterator &other) const {
261 return this->pos != other.pos;
262 }
263
264 private:
265 friend class Vec;
266 const void *pos;
267 size_t stride;
268 };
269
270 const_iterator begin() const noexcept {
271 const_iterator it;
272 it.pos = this->data();
273 it.stride = this->stride();
274 return it;
275 }
276 const_iterator end() const noexcept {
277 const_iterator it = this->begin();
278 it.pos = static_cast<const uint8_t *>(it.pos) + it.stride * this->size();
279 return it;
280 }
281
David Tolnay313b10e2020-04-25 16:30:51 -0700282 // Internal API only intended for the cxxbridge code generator.
283 Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
284
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700285private:
David Tolnay503d0192020-04-24 22:18:56 -0700286 static size_t stride() noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700287 void drop() noexcept;
288
289 // Size and alignment statically verified by rust_vec.rs.
290 std::array<uintptr_t, 3> repr;
291};
292#endif // CXXBRIDGE02_RUST_VEC
293
David Tolnay75dca2e2020-03-25 20:17:52 -0700294#ifndef CXXBRIDGE02_RUST_FN
295#define CXXBRIDGE02_RUST_FN
David Tolnayf262d382020-04-11 22:12:40 -0700296template <typename Signature, bool Throws = false>
297class Fn;
David Tolnay75dca2e2020-03-25 20:17:52 -0700298
299template <typename Ret, typename... Args, bool Throws>
300class Fn<Ret(Args...), Throws> {
301public:
David Tolnay533d4582020-04-08 20:29:14 -0700302 Ret operator()(Args... args) const noexcept(!Throws);
303 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700304
305private:
306 Ret (*trampoline)(Args..., void *fn) noexcept(!Throws);
307 void *fn;
308};
309
David Tolnayf262d382020-04-11 22:12:40 -0700310template <typename Signature>
311using TryFn = Fn<Signature, true>;
David Tolnay75dca2e2020-03-25 20:17:52 -0700312#endif // CXXBRIDGE02_RUST_FN
313
David Tolnayb7a7cb62020-03-17 21:18:40 -0700314#ifndef CXXBRIDGE02_RUST_ERROR
315#define CXXBRIDGE02_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700316class Error final : std::exception {
317public:
318 Error(const Error &);
319 Error(Error &&) noexcept;
320 Error(Str::Repr) noexcept;
321 ~Error() noexcept;
322 const char *what() const noexcept override;
323
324private:
325 Str::Repr msg;
326};
David Tolnayb7a7cb62020-03-17 21:18:40 -0700327#endif // CXXBRIDGE02_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700328
David Tolnayb8a6fb22020-04-10 11:17:28 -0700329#ifndef CXXBRIDGE02_RUST_ISIZE
330#define CXXBRIDGE02_RUST_ISIZE
331#if defined(_WIN32)
332using isize = SSIZE_T;
333#else
334using isize = ssize_t;
335#endif
336#endif // CXXBRIDGE02_RUST_ISIZE
337
David Tolnay851677c2020-03-01 23:49:46 -0800338std::ostream &operator<<(std::ostream &, const String &);
339std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400340
David Tolnay3b0c9882020-03-01 14:08:57 -0800341// Snake case aliases for use in code that uses this style for type names.
342using string = String;
343using str = Str;
David Tolnayf262d382020-04-11 22:12:40 -0700344template <class T>
345using box = Box<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700346using error = Error;
David Tolnay75dca2e2020-03-25 20:17:52 -0700347template <typename Signature, bool Throws = false>
348using fn = Fn<Signature, Throws>;
David Tolnayf262d382020-04-11 22:12:40 -0700349template <typename Signature>
350using try_fn = TryFn<Signature>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800351
David Tolnay75dca2e2020-03-25 20:17:52 -0700352template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700353Ret Fn<Ret(Args...), Throws>::operator()(Args... args) const noexcept(!Throws) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700354 return (*this->trampoline)(std::move(args)..., this->fn);
355}
356
David Tolnaya23129c2020-04-08 20:08:21 -0700357template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700358Fn<Ret(Args...), Throws> Fn<Ret(Args...), Throws>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700359 return *this;
360}
361
David Tolnay8c730492020-03-13 01:29:06 -0700362} // namespace cxxbridge02
David Tolnay750755e2020-03-01 13:04:08 -0800363} // namespace rust