blob: e40876ddf7085904f01213e24863984bc628de13 [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 Tolnayd1df4c72020-11-25 20:38:05 -08007#include <iterator>
David Tolnay0ecd05a2020-07-29 16:32:03 -07008#include <new>
Stephen Crane9e48d5b2020-08-21 12:17:02 -07009#include <stdexcept>
David Tolnay7db73692019-10-20 14:51:12 -040010#include <string>
David Tolnayf6292372020-03-01 21:09:11 -080011#include <type_traits>
David Tolnay4791f1c2020-03-17 21:53:16 -070012#include <utility>
David Tolnay37dd7e12020-04-25 12:51:59 -070013#include <vector>
David Tolnay59b5ba12020-04-10 11:32:19 -070014#if defined(_WIN32)
David Tolnayda38b7c2020-09-16 11:50:04 -040015#include <basetsd.h>
David Tolnay59b5ba12020-04-10 11:32:19 -070016#endif
David Tolnay7db73692019-10-20 14:51:12 -040017
David Tolnay750755e2020-03-01 13:04:08 -080018namespace rust {
David Tolnay0f0162f2020-11-16 23:43:37 -080019inline namespace cxxbridge1 {
David Tolnay7db73692019-10-20 14:51:12 -040020
David Tolnay2a2b9ad2020-05-12 20:07:26 -070021struct unsafe_bitcopy_t;
David Tolnayd1e2efc2020-03-03 22:25:43 -080022
David Tolnay84ddf9e2020-10-31 15:36:48 -070023namespace {
24template <typename T>
25class impl;
26}
27
David Tolnay0f0162f2020-11-16 23:43:37 -080028#ifndef CXXBRIDGE1_RUST_STRING
29#define CXXBRIDGE1_RUST_STRING
David Tolnaye468f052020-11-29 19:39:35 -080030// https://cxx.rs/binding/string.html
David Tolnay56082162020-03-01 12:57:33 -080031class String final {
David Tolnay7db73692019-10-20 14:51:12 -040032public:
David Tolnay56082162020-03-01 12:57:33 -080033 String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080034 String(const String &) noexcept;
35 String(String &&) noexcept;
David Tolnay56082162020-03-01 12:57:33 -080036 ~String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080037
38 String(const std::string &);
39 String(const char *);
David Tolnayc2bbd952020-07-29 18:15:26 -070040 String(const char *, size_t);
David Tolnayd9c4ac92020-03-01 20:33:58 -080041
42 String &operator=(const String &) noexcept;
43 String &operator=(String &&) noexcept;
44
David Tolnay404d6892020-03-01 20:19:41 -080045 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040046
47 // Note: no null terminator.
48 const char *data() const noexcept;
49 size_t size() const noexcept;
50 size_t length() const noexcept;
51
David Tolnayff7f5fb2020-11-25 20:50:32 -080052 using iterator = char *;
53 iterator begin() noexcept;
54 iterator end() noexcept;
55
56 using const_iterator = const char *;
57 const_iterator begin() const noexcept;
58 const_iterator end() const noexcept;
59 const_iterator cbegin() const noexcept;
60 const_iterator cend() const noexcept;
61
David Tolnayd1e2efc2020-03-03 22:25:43 -080062 // Internal API only intended for the cxxbridge code generator.
63 String(unsafe_bitcopy_t, const String &) noexcept;
64
David Tolnay7db73692019-10-20 14:51:12 -040065private:
66 // Size and alignment statically verified by rust_string.rs.
67 std::array<uintptr_t, 3> repr;
68};
David Tolnay0f0162f2020-11-16 23:43:37 -080069#endif // CXXBRIDGE1_RUST_STRING
David Tolnay7db73692019-10-20 14:51:12 -040070
David Tolnay0f0162f2020-11-16 23:43:37 -080071#ifndef CXXBRIDGE1_RUST_STR
David Tolnaye468f052020-11-29 19:39:35 -080072// https://cxx.rs/binding/str.html
David Tolnay09dbe752020-03-01 13:00:40 -080073class Str final {
David Tolnay7db73692019-10-20 14:51:12 -040074public:
David Tolnay09dbe752020-03-01 13:00:40 -080075 Str() noexcept;
David Tolnay828e5132020-11-29 20:40:40 -080076 Str(const String &) noexcept;
David Tolnay851677c2020-03-01 23:49:46 -080077 Str(const std::string &);
78 Str(const char *);
David Tolnay894c5e42020-07-29 18:20:00 -070079 Str(const char *, size_t);
David Tolnayd9c4ac92020-03-01 20:33:58 -080080
David Tolnay2d7f1172020-10-31 17:58:31 -070081 Str &operator=(const Str &) noexcept = default;
David Tolnayd9c4ac92020-03-01 20:33:58 -080082
David Tolnay404d6892020-03-01 20:19:41 -080083 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040084
85 // Note: no null terminator.
86 const char *data() const noexcept;
87 size_t size() const noexcept;
88 size_t length() const noexcept;
89
David Tolnayde9a5b12020-10-31 12:15:43 -070090 // Important in order for System V ABI to pass in registers.
91 Str(const Str &) noexcept = default;
92 ~Str() noexcept = default;
93
David Tolnayff7f5fb2020-11-25 20:50:32 -080094 using iterator = const char *;
95 using const_iterator = const char *;
96 const_iterator begin() const noexcept;
97 const_iterator end() const noexcept;
98 const_iterator cbegin() const noexcept;
99 const_iterator cend() const noexcept;
100
David Tolnay5df1f062020-10-31 12:31:10 -0700101private:
David Tolnay0356d332020-10-31 19:46:41 -0700102 friend impl<Str>;
David Tolnay7db73692019-10-20 14:51:12 -0400103 // Not necessarily ABI compatible with &str. Codegen will translate to
104 // cxx::rust_str::RustStr which matches this layout.
David Tolnay5df1f062020-10-31 12:31:10 -0700105 const char *ptr;
106 size_t len;
David Tolnay7db73692019-10-20 14:51:12 -0400107};
David Tolnay0f0162f2020-11-16 23:43:37 -0800108#endif // CXXBRIDGE1_RUST_STR
David Tolnay7db73692019-10-20 14:51:12 -0400109
David Tolnay0f0162f2020-11-16 23:43:37 -0800110#ifndef CXXBRIDGE1_RUST_SLICE
David Tolnayc5629f02020-11-23 18:32:46 -0800111namespace detail {
David Tolnayee9b9ee2020-11-25 08:28:50 -0800112template <bool>
David Tolnayc5629f02020-11-23 18:32:46 -0800113struct copy_assignable_if {};
David Tolnayce298232020-11-11 10:08:54 -0800114
David Tolnayc5629f02020-11-23 18:32:46 -0800115template <>
116struct copy_assignable_if<false> {
117 copy_assignable_if() noexcept = default;
118 copy_assignable_if(const copy_assignable_if &) noexcept = default;
119 copy_assignable_if &operator=(const copy_assignable_if &) noexcept = delete;
120 copy_assignable_if &operator=(copy_assignable_if &&) noexcept = default;
121};
122} // namespace detail
123
David Tolnaye468f052020-11-29 19:39:35 -0800124// https://cxx.rs/binding/slice.html
David Tolnayc5629f02020-11-23 18:32:46 -0800125template <typename T>
126class Slice final
127 : private detail::copy_assignable_if<std::is_const<T>::value> {
David Tolnayefe81052020-04-14 16:28:24 -0700128public:
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700129 Slice() noexcept;
David Tolnayce298232020-11-11 10:08:54 -0800130 Slice(T *, size_t count) noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700131
David Tolnay2d7f1172020-10-31 17:58:31 -0700132 Slice &operator=(const Slice<T> &) noexcept = default;
David Tolnayc5629f02020-11-23 18:32:46 -0800133 Slice &operator=(Slice<T> &&) noexcept = default;
David Tolnayefe81052020-04-14 16:28:24 -0700134
David Tolnayce298232020-11-11 10:08:54 -0800135 T *data() const noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700136 size_t size() const noexcept;
137 size_t length() const noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700138
David Tolnayde9a5b12020-10-31 12:15:43 -0700139 // Important in order for System V ABI to pass in registers.
140 Slice(const Slice<T> &) noexcept = default;
141 ~Slice() noexcept = default;
142
David Tolnayac6cb542020-11-25 20:18:55 -0800143 class iterator;
144 iterator begin() const noexcept;
145 iterator end() const noexcept;
146
David Tolnayefe81052020-04-14 16:28:24 -0700147private:
David Tolnay36aa9e02020-10-31 23:08:21 -0700148 friend impl<Slice>;
149 // Not necessarily ABI compatible with &[T]. Codegen will translate to
David Tolnay5515a9e2020-11-25 19:07:54 -0800150 // cxx::rust_slice::RustSlice which matches this layout.
David Tolnayce298232020-11-11 10:08:54 -0800151 T *ptr;
David Tolnay36aa9e02020-10-31 23:08:21 -0700152 size_t len;
David Tolnayefe81052020-04-14 16:28:24 -0700153};
David Tolnayac6cb542020-11-25 20:18:55 -0800154
155template <typename T>
156class Slice<T>::iterator final {
157public:
158 using difference_type = ptrdiff_t;
159 using value_type = T;
160 using pointer = typename std::add_pointer<T>::type;
161 using reference = typename std::add_lvalue_reference<T>::type;
162 using iterator_category = std::forward_iterator_tag;
163
164 T &operator*() const noexcept;
165 T *operator->() const noexcept;
166 iterator &operator++() noexcept;
167 iterator operator++(int) noexcept;
168 bool operator==(const iterator &) const noexcept;
169 bool operator!=(const iterator &) const noexcept;
170
171private:
172 friend class Slice;
173 T *pos;
174};
David Tolnay0f0162f2020-11-16 23:43:37 -0800175#endif // CXXBRIDGE1_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -0700176
David Tolnay0f0162f2020-11-16 23:43:37 -0800177#ifndef CXXBRIDGE1_RUST_BOX
David Tolnaye468f052020-11-29 19:39:35 -0800178// https://cxx.rs/binding/box.html
David Tolnayf262d382020-04-11 22:12:40 -0700179template <typename T>
180class Box final {
David Tolnay7db73692019-10-20 14:51:12 -0400181public:
David Tolnayf6292372020-03-01 21:09:11 -0800182 using value_type = T;
David Tolnay9706a512020-04-24 17:09:01 -0700183 using const_pointer =
184 typename std::add_pointer<typename std::add_const<T>::type>::type;
185 using pointer = typename std::add_pointer<T>::type;
David Tolnayf6292372020-03-01 21:09:11 -0800186
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700187 Box(const Box &);
188 Box(Box &&) noexcept;
189 ~Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400190
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700191 explicit Box(const T &);
192 explicit Box(T &&);
193
194 Box &operator=(const Box &);
195 Box &operator=(Box &&) noexcept;
196
197 const T *operator->() const noexcept;
198 const T &operator*() const noexcept;
199 T *operator->() noexcept;
200 T &operator*() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400201
David Tolnayf262d382020-04-11 22:12:40 -0700202 template <typename... Fields>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700203 static Box in_place(Fields &&...);
David Tolnay7ce59fc2020-04-11 11:46:33 -0700204
David Tolnay7db73692019-10-20 14:51:12 -0400205 // Important: requires that `raw` came from an into_raw call. Do not pass a
206 // pointer from `new` or any other source.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700207 static Box from_raw(T *) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400208
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700209 T *into_raw() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400210
211private:
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700212 Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400213 void uninit() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400214 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800215 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400216};
David Tolnay0f0162f2020-11-16 23:43:37 -0800217#endif // CXXBRIDGE1_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400218
David Tolnay0f0162f2020-11-16 23:43:37 -0800219#ifndef CXXBRIDGE1_RUST_VEC
David Tolnaye468f052020-11-29 19:39:35 -0800220// https://cxx.rs/binding/vec.html
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700221template <typename T>
222class Vec final {
223public:
David Tolnayc87c2152020-04-24 17:07:41 -0700224 using value_type = T;
225
David Tolnayf97c2d52020-04-25 16:37:48 -0700226 Vec() noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700227 Vec(Vec &&) noexcept;
228 ~Vec() noexcept;
David Tolnaycb800572020-04-24 20:30:43 -0700229
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700230 Vec &operator=(Vec &&) noexcept;
David Tolnayf97c2d52020-04-25 16:37:48 -0700231
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700232 size_t size() const noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700233 bool empty() const noexcept;
David Tolnay219c0792020-04-24 20:31:37 -0700234 const T *data() const noexcept;
David Tolnayfb6b73c2020-11-10 14:32:16 -0800235 T *data() noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700236
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700237 const T &operator[](size_t n) const noexcept;
238 const T &at(size_t n) const;
239
240 const T &front() const;
241 const T &back() const;
242
David Tolnayfb6b73c2020-11-10 14:32:16 -0800243 void reserve(size_t new_cap);
244 void push_back(const T &value);
245 void push_back(T &&value);
David Tolnay4e8c49a2020-11-11 10:00:18 -0800246 template <typename... Args>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800247 void emplace_back(Args &&... args);
248
David Tolnay960b5112020-11-25 13:18:28 -0800249 class iterator;
250 iterator begin() noexcept;
251 iterator end() noexcept;
252
David Tolnaya5d72c62020-11-25 13:57:16 -0800253 using const_iterator = typename Vec<const T>::iterator;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700254 const_iterator begin() const noexcept;
255 const_iterator end() const noexcept;
David Tolnay960b5112020-11-25 13:18:28 -0800256 const_iterator cbegin() const noexcept;
257 const_iterator cend() const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700258
David Tolnay313b10e2020-04-25 16:30:51 -0700259 // Internal API only intended for the cxxbridge code generator.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700260 Vec(unsafe_bitcopy_t, const Vec &) noexcept;
David Tolnay313b10e2020-04-25 16:30:51 -0700261
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700262private:
David Tolnay503d0192020-04-24 22:18:56 -0700263 static size_t stride() noexcept;
David Tolnayfb6b73c2020-11-10 14:32:16 -0800264 void reserve_total(size_t cap) noexcept;
265 void set_len(size_t len) noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700266 void drop() noexcept;
267
268 // Size and alignment statically verified by rust_vec.rs.
269 std::array<uintptr_t, 3> repr;
270};
David Tolnay66f216c2020-11-25 13:21:00 -0800271
272template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800273class Vec<T>::iterator final {
274public:
275 using difference_type = ptrdiff_t;
276 using value_type = T;
277 using pointer = typename std::add_pointer<T>::type;
278 using reference = typename std::add_lvalue_reference<T>::type;
279 using iterator_category = std::forward_iterator_tag;
280
281 T &operator*() const noexcept;
282 T *operator->() const noexcept;
283 iterator &operator++() noexcept;
284 iterator operator++(int) noexcept;
285 bool operator==(const iterator &) const noexcept;
286 bool operator!=(const iterator &) const noexcept;
287
288private:
289 friend class Vec;
David Tolnaya5d72c62020-11-25 13:57:16 -0800290 friend class Vec<typename std::remove_const<T>::type>;
David Tolnay960b5112020-11-25 13:18:28 -0800291 void *pos;
292 size_t stride;
293};
David Tolnay0f0162f2020-11-16 23:43:37 -0800294#endif // CXXBRIDGE1_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700295
David Tolnay0f0162f2020-11-16 23:43:37 -0800296#ifndef CXXBRIDGE1_RUST_FN
David Tolnaye468f052020-11-29 19:39:35 -0800297// https://cxx.rs/binding/fn.html
David Tolnayf031c322020-11-29 19:41:33 -0800298template <typename Signature>
David Tolnayf262d382020-04-11 22:12:40 -0700299class Fn;
David Tolnay75dca2e2020-03-25 20:17:52 -0700300
David Tolnayf031c322020-11-29 19:41:33 -0800301template <typename Ret, typename... Args>
302class Fn<Ret(Args...)> final {
David Tolnay75dca2e2020-03-25 20:17:52 -0700303public:
David Tolnayf031c322020-11-29 19:41:33 -0800304 Ret operator()(Args... args) const noexcept;
David Tolnay533d4582020-04-08 20:29:14 -0700305 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700306
307private:
David Tolnayf031c322020-11-29 19:41:33 -0800308 Ret (*trampoline)(Args..., void *fn) noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700309 void *fn;
310};
David Tolnay0f0162f2020-11-16 23:43:37 -0800311#endif // CXXBRIDGE1_RUST_FN
David Tolnay75dca2e2020-03-25 20:17:52 -0700312
David Tolnay0f0162f2020-11-16 23:43:37 -0800313#ifndef CXXBRIDGE1_RUST_ERROR
314#define CXXBRIDGE1_RUST_ERROR
David Tolnaye468f052020-11-29 19:39:35 -0800315// https://cxx.rs/binding/result.html
David Tolnaye4fa8732020-09-08 15:04:56 -0700316class Error final : public std::exception {
David Tolnay1e548172020-03-16 13:37:09 -0700317public:
318 Error(const Error &);
319 Error(Error &&) noexcept;
David Tolnay2714d2c2020-11-23 18:17:43 -0800320 ~Error() noexcept override;
David Tolnay7c6ac712020-10-31 17:22:28 -0700321
322 Error &operator=(const Error &);
David Tolnay15491062020-10-31 17:25:13 -0700323 Error &operator=(Error &&) noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700324
David Tolnay1e548172020-03-16 13:37:09 -0700325 const char *what() const noexcept override;
326
327private:
David Tolnaya0c9bc72020-10-31 14:37:14 -0700328 Error() noexcept = default;
David Tolnay84ddf9e2020-10-31 15:36:48 -0700329 friend impl<Error>;
David Tolnaya0c9bc72020-10-31 14:37:14 -0700330 const char *msg;
331 size_t len;
David Tolnay1e548172020-03-16 13:37:09 -0700332};
David Tolnay0f0162f2020-11-16 23:43:37 -0800333#endif // CXXBRIDGE1_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700334
David Tolnay0f0162f2020-11-16 23:43:37 -0800335#ifndef CXXBRIDGE1_RUST_ISIZE
336#define CXXBRIDGE1_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700337#if defined(_WIN32)
338using isize = SSIZE_T;
339#else
340using isize = ssize_t;
341#endif
David Tolnay0f0162f2020-11-16 23:43:37 -0800342#endif // CXXBRIDGE1_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700343
David Tolnay851677c2020-03-01 23:49:46 -0800344std::ostream &operator<<(std::ostream &, const String &);
345std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400346
David Tolnay365fc7c2020-11-25 16:08:13 -0800347#ifndef CXXBRIDGE1_RUST_OPAQUE
348#define CXXBRIDGE1_RUST_OPAQUE
349// Base class of generated opaque Rust types.
350class Opaque {
David Tolnaya857c322020-11-25 16:27:19 -0800351public:
David Tolnay365fc7c2020-11-25 16:08:13 -0800352 Opaque() = delete;
353 Opaque(const Opaque &) = delete;
354 ~Opaque() = delete;
355};
356#endif // CXXBRIDGE1_RUST_OPAQUE
357
David Tolnay174bd952020-11-02 09:23:12 -0800358// IsRelocatable<T> is used in assertions that a C++ type passed by value
359// between Rust and C++ is soundly relocatable by Rust.
360//
361// There may be legitimate reasons to opt out of the check for support of types
362// that the programmer knows are soundly Rust-movable despite not being
363// recognized as such by the C++ type system due to a move constructor or
364// destructor. To opt out of the relocatability check, do either of the
365// following things in any header used by `include!` in the bridge.
366//
367// --- if you define the type:
368// struct MyType {
369// ...
370// + using IsRelocatable = std::true_type;
371// };
372//
373// --- otherwise:
374// + template <>
375// + struct rust::IsRelocatable<MyType> : std::true_type {};
376template <typename T>
377struct IsRelocatable;
378
David Tolnay3b0c9882020-03-01 14:08:57 -0800379// Snake case aliases for use in code that uses this style for type names.
380using string = String;
381using str = Str;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800382template <typename T>
David Tolnay38c87642020-09-06 22:18:08 -0700383using slice = Slice<T>;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800384template <typename T>
David Tolnayf262d382020-04-11 22:12:40 -0700385using box = Box<T>;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800386template <typename T>
David Tolnay38c87642020-09-06 22:18:08 -0700387using vec = Vec<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700388using error = Error;
David Tolnayf262d382020-04-11 22:12:40 -0700389template <typename Signature>
David Tolnayf031c322020-11-29 19:41:33 -0800390using fn = Fn<Signature>;
David Tolnay174bd952020-11-02 09:23:12 -0800391template <typename T>
392using is_relocatable = IsRelocatable<T>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800393
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700394
395
396////////////////////////////////////////////////////////////////////////////////
397/// end public API, begin implementation details
398
David Tolnay0f0162f2020-11-16 23:43:37 -0800399#ifndef CXXBRIDGE1_PANIC
400#define CXXBRIDGE1_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700401template <typename Exception>
402void panic [[noreturn]] (const char *msg);
David Tolnay0f0162f2020-11-16 23:43:37 -0800403#endif // CXXBRIDGE1_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700404
David Tolnay0f0162f2020-11-16 23:43:37 -0800405#ifndef CXXBRIDGE1_RUST_FN
406#define CXXBRIDGE1_RUST_FN
David Tolnayf031c322020-11-29 19:41:33 -0800407template <typename Ret, typename... Args>
408Ret Fn<Ret(Args...)>::operator()(Args... args) const noexcept {
David Tolnay75dca2e2020-03-25 20:17:52 -0700409 return (*this->trampoline)(std::move(args)..., this->fn);
410}
411
David Tolnayf031c322020-11-29 19:41:33 -0800412template <typename Ret, typename... Args>
413Fn<Ret(Args...)> Fn<Ret(Args...)>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700414 return *this;
415}
David Tolnay0f0162f2020-11-16 23:43:37 -0800416#endif // CXXBRIDGE1_RUST_FN
David Tolnaya23129c2020-04-08 20:08:21 -0700417
David Tolnay0f0162f2020-11-16 23:43:37 -0800418#ifndef CXXBRIDGE1_RUST_BITCOPY
419#define CXXBRIDGE1_RUST_BITCOPY
David Tolnay48521222020-10-31 14:59:42 -0700420struct unsafe_bitcopy_t final {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700421 explicit unsafe_bitcopy_t() = default;
422};
423
424constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnay0f0162f2020-11-16 23:43:37 -0800425#endif // CXXBRIDGE1_RUST_BITCOPY
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700426
David Tolnay0f0162f2020-11-16 23:43:37 -0800427#ifndef CXXBRIDGE1_RUST_STR
428#define CXXBRIDGE1_RUST_STR
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700429inline const char *Str::data() const noexcept { return this->ptr; }
430
431inline size_t Str::size() const noexcept { return this->len; }
432
433inline size_t Str::length() const noexcept { return this->len; }
David Tolnay0f0162f2020-11-16 23:43:37 -0800434#endif // CXXBRIDGE1_RUST_STR
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700435
David Tolnay0f0162f2020-11-16 23:43:37 -0800436#ifndef CXXBRIDGE1_RUST_SLICE
437#define CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700438template <typename T>
David Tolnay7b16a392020-11-25 20:23:10 -0800439Slice<T>::Slice() noexcept : ptr(reinterpret_cast<T *>(alignof(T))), len(0) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700440
441template <typename T>
David Tolnayce298232020-11-11 10:08:54 -0800442Slice<T>::Slice(T *s, size_t count) noexcept : ptr(s), len(count) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700443
444template <typename T>
David Tolnayce298232020-11-11 10:08:54 -0800445T *Slice<T>::data() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700446 return this->ptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700447}
448
449template <typename T>
450size_t Slice<T>::size() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700451 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700452}
453
454template <typename T>
455size_t Slice<T>::length() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700456 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700457}
David Tolnayac6cb542020-11-25 20:18:55 -0800458
459template <typename T>
460T &Slice<T>::iterator::operator*() const noexcept {
461 return *this->pos;
462}
463
464template <typename T>
465T *Slice<T>::iterator::operator->() const noexcept {
466 return this->pos;
467}
468
469template <typename T>
470typename Slice<T>::iterator &Slice<T>::iterator::operator++() noexcept {
471 ++this->pos;
472 return *this;
473}
474
475template <typename T>
476typename Slice<T>::iterator Slice<T>::iterator::operator++(int) noexcept {
477 auto ret = iterator(*this);
478 ++this->pos;
479 return ret;
480}
481
482template <typename T>
483bool Slice<T>::iterator::operator==(const iterator &other) const noexcept {
484 return this->pos == other.pos;
485}
486
487template <typename T>
488bool Slice<T>::iterator::operator!=(const iterator &other) const noexcept {
489 return this->pos != other.pos;
490}
491
492template <typename T>
493typename Slice<T>::iterator Slice<T>::begin() const noexcept {
494 iterator it;
495 it.pos = this->ptr;
496 return it;
497}
498
499template <typename T>
500typename Slice<T>::iterator Slice<T>::end() const noexcept {
501 iterator it = this->begin();
502 it.pos += this->len;
503 return it;
504}
David Tolnay0f0162f2020-11-16 23:43:37 -0800505#endif // CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700506
David Tolnay0f0162f2020-11-16 23:43:37 -0800507#ifndef CXXBRIDGE1_RUST_BOX
508#define CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700509template <typename T>
510Box<T>::Box(const Box &other) : Box(*other) {}
511
512template <typename T>
513Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
514 other.ptr = nullptr;
515}
516
517template <typename T>
518Box<T>::Box(const T &val) {
519 this->uninit();
520 ::new (this->ptr) T(val);
521}
522
523template <typename T>
524Box<T>::Box(T &&val) {
525 this->uninit();
526 ::new (this->ptr) T(std::move(val));
527}
528
529template <typename T>
530Box<T>::~Box() noexcept {
531 if (this->ptr) {
532 this->drop();
533 }
534}
535
536template <typename T>
537Box<T> &Box<T>::operator=(const Box &other) {
538 if (this != &other) {
539 if (this->ptr) {
540 **this = *other;
541 } else {
542 this->uninit();
543 ::new (this->ptr) T(*other);
544 }
545 }
546 return *this;
547}
548
549template <typename T>
550Box<T> &Box<T>::operator=(Box &&other) noexcept {
551 if (this->ptr) {
552 this->drop();
553 }
554 this->ptr = other.ptr;
555 other.ptr = nullptr;
556 return *this;
557}
558
559template <typename T>
560const T *Box<T>::operator->() const noexcept {
561 return this->ptr;
562}
563
564template <typename T>
565const T &Box<T>::operator*() const noexcept {
566 return *this->ptr;
567}
568
569template <typename T>
570T *Box<T>::operator->() noexcept {
571 return this->ptr;
572}
573
574template <typename T>
575T &Box<T>::operator*() noexcept {
576 return *this->ptr;
577}
578
579template <typename T>
580template <typename... Fields>
581Box<T> Box<T>::in_place(Fields &&... fields) {
582 Box box;
583 box.uninit();
584 ::new (box.ptr) T{std::forward<Fields>(fields)...};
585 return box;
586}
587
588template <typename T>
589Box<T> Box<T>::from_raw(T *raw) noexcept {
590 Box box;
591 box.ptr = raw;
592 return box;
593}
594
595template <typename T>
596T *Box<T>::into_raw() noexcept {
597 T *raw = this->ptr;
598 this->ptr = nullptr;
599 return raw;
600}
601
602template <typename T>
David Tolnay79076c72020-11-23 18:16:55 -0800603Box<T>::Box() noexcept = default;
David Tolnay0f0162f2020-11-16 23:43:37 -0800604#endif // CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700605
David Tolnay0f0162f2020-11-16 23:43:37 -0800606#ifndef CXXBRIDGE1_RUST_VEC
607#define CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700608template <typename T>
David Tolnay15671862020-11-23 18:13:56 -0800609Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700610 new (&other) Vec();
611}
612
613template <typename T>
614Vec<T>::~Vec() noexcept {
615 this->drop();
616}
617
618template <typename T>
619Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
620 if (this != &other) {
621 this->drop();
622 this->repr = other.repr;
623 new (&other) Vec();
624 }
625 return *this;
626}
627
628template <typename T>
629bool Vec<T>::empty() const noexcept {
630 return size() == 0;
631}
632
633template <typename T>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800634T *Vec<T>::data() noexcept {
635 return const_cast<T *>(const_cast<const Vec<T> *>(this)->data());
636}
637
638template <typename T>
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700639const T &Vec<T>::operator[](size_t n) const noexcept {
640 auto data = reinterpret_cast<const char *>(this->data());
641 return *reinterpret_cast<const T *>(data + n * this->stride());
642}
643
644template <typename T>
645const T &Vec<T>::at(size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700646 if (n >= this->size()) {
647 panic<std::out_of_range>("rust::Vec index out of range");
648 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700649 return (*this)[n];
650}
651
652template <typename T>
653const T &Vec<T>::front() const {
654 return (*this)[0];
655}
656
657template <typename T>
658const T &Vec<T>::back() const {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700659 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700660}
661
662template <typename T>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800663void Vec<T>::reserve(size_t new_cap) {
664 this->reserve_total(new_cap);
665}
666
667template <typename T>
668void Vec<T>::push_back(const T &value) {
669 this->emplace_back(value);
670}
671
672template <typename T>
673void Vec<T>::push_back(T &&value) {
674 this->emplace_back(std::move(value));
675}
676
677template <typename T>
678template <typename... Args>
679void Vec<T>::emplace_back(Args &&... args) {
680 auto size = this->size();
681 this->reserve_total(size + 1);
682 ::new (reinterpret_cast<T *>(reinterpret_cast<char *>(this->data()) +
683 size * this->stride()))
684 T(std::forward<Args>(args)...);
685 this->set_len(size + 1);
686}
687
688template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800689T &Vec<T>::iterator::operator*() const noexcept {
690 return *static_cast<T *>(this->pos);
691}
692
693template <typename T>
694T *Vec<T>::iterator::operator->() const noexcept {
695 return static_cast<T *>(this->pos);
696}
697
698template <typename T>
699typename Vec<T>::iterator &Vec<T>::iterator::operator++() noexcept {
David Tolnay248215e2020-11-25 19:38:26 -0800700 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnay960b5112020-11-25 13:18:28 -0800701 return *this;
702}
703
704template <typename T>
705typename Vec<T>::iterator Vec<T>::iterator::operator++(int) noexcept {
706 auto ret = iterator(*this);
David Tolnay248215e2020-11-25 19:38:26 -0800707 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnay960b5112020-11-25 13:18:28 -0800708 return ret;
709}
710
711template <typename T>
712bool Vec<T>::iterator::operator==(const iterator &other) const noexcept {
713 return this->pos == other.pos;
714}
715
716template <typename T>
717bool Vec<T>::iterator::operator!=(const iterator &other) const noexcept {
718 return this->pos != other.pos;
719}
720
721template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800722typename Vec<T>::iterator Vec<T>::begin() noexcept {
723 iterator it;
David Tolnaya5d72c62020-11-25 13:57:16 -0800724 it.pos = const_cast<typename std::remove_const<T>::type *>(this->data());
David Tolnay960b5112020-11-25 13:18:28 -0800725 it.stride = this->stride();
726 return it;
727}
728
729template <typename T>
730typename Vec<T>::iterator Vec<T>::end() noexcept {
731 iterator it = this->begin();
David Tolnay248215e2020-11-25 19:38:26 -0800732 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
David Tolnay960b5112020-11-25 13:18:28 -0800733 return it;
734}
735
736template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700737typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800738 return this->cbegin();
739}
740
741template <typename T>
742typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
743 return this->cend();
744}
745
746template <typename T>
747typename Vec<T>::const_iterator Vec<T>::cbegin() const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700748 const_iterator it;
David Tolnaya5d72c62020-11-25 13:57:16 -0800749 it.pos = const_cast<typename std::remove_const<T>::type *>(this->data());
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700750 it.stride = this->stride();
751 return it;
752}
753
754template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800755typename Vec<T>::const_iterator Vec<T>::cend() const noexcept {
756 const_iterator it = this->cbegin();
David Tolnay248215e2020-11-25 19:38:26 -0800757 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700758 return it;
759}
760
761// Internal API only intended for the cxxbridge code generator.
762template <typename T>
763Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay0f0162f2020-11-16 23:43:37 -0800764#endif // CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700765
David Tolnay0f0162f2020-11-16 23:43:37 -0800766#ifndef CXXBRIDGE1_RELOCATABLE
767#define CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -0800768namespace detail {
769template <typename... Ts>
770struct make_void {
771 using type = void;
772};
773
774template <typename... Ts>
775using void_t = typename make_void<Ts...>::type;
776
777template <typename Void, template <typename...> class, typename...>
778struct detect : std::false_type {};
779template <template <typename...> class T, typename... A>
780struct detect<void_t<T<A...>>, T, A...> : std::true_type {};
781
782template <template <typename...> class T, typename... A>
783using is_detected = detect<void, T, A...>;
784
785template <typename T>
786using detect_IsRelocatable = typename T::IsRelocatable;
787
788template <typename T>
789struct get_IsRelocatable
790 : std::is_same<typename T::IsRelocatable, std::true_type> {};
791} // namespace detail
792
793template <typename T>
794struct IsRelocatable
795 : std::conditional<
796 detail::is_detected<detail::detect_IsRelocatable, T>::value,
797 detail::get_IsRelocatable<T>,
798 std::integral_constant<
799 bool, std::is_trivially_move_constructible<T>::value &&
800 std::is_trivially_destructible<T>::value>>::type {};
David Tolnay0f0162f2020-11-16 23:43:37 -0800801#endif // CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -0800802
David Tolnay0f0162f2020-11-16 23:43:37 -0800803} // namespace cxxbridge1
David Tolnay750755e2020-03-01 13:04:08 -0800804} // namespace rust