blob: 792d57d61d4f5f4cb1ae6cfbfb82300712ad7186 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001#pragma once
David Tolnayf799b372020-11-29 23:57:42 -08002#include <algorithm>
David Tolnay7db73692019-10-20 14:51:12 -04003#include <array>
David Tolnay30430f12020-03-19 20:49:00 -07004#include <cstddef>
David Tolnay7db73692019-10-20 14:51:12 -04005#include <cstdint>
David Tolnayb7a7cb62020-03-17 21:18:40 -07006#include <exception>
David Tolnayf799b372020-11-29 23:57:42 -08007#include <initializer_list>
David Tolnay001102a2020-03-01 20:05:04 -08008#include <iosfwd>
David Tolnayd1df4c72020-11-25 20:38:05 -08009#include <iterator>
David Tolnay0ecd05a2020-07-29 16:32:03 -070010#include <new>
Stephen Crane9e48d5b2020-08-21 12:17:02 -070011#include <stdexcept>
David Tolnay7db73692019-10-20 14:51:12 -040012#include <string>
David Tolnayf6292372020-03-01 21:09:11 -080013#include <type_traits>
David Tolnay4791f1c2020-03-17 21:53:16 -070014#include <utility>
David Tolnay37dd7e12020-04-25 12:51:59 -070015#include <vector>
David Tolnay59b5ba12020-04-10 11:32:19 -070016#if defined(_WIN32)
David Tolnayda38b7c2020-09-16 11:50:04 -040017#include <basetsd.h>
David Tolnay59b5ba12020-04-10 11:32:19 -070018#endif
David Tolnay7db73692019-10-20 14:51:12 -040019
David Tolnay750755e2020-03-01 13:04:08 -080020namespace rust {
David Tolnay0f0162f2020-11-16 23:43:37 -080021inline namespace cxxbridge1 {
David Tolnay7db73692019-10-20 14:51:12 -040022
David Tolnay2a2b9ad2020-05-12 20:07:26 -070023struct unsafe_bitcopy_t;
David Tolnayd1e2efc2020-03-03 22:25:43 -080024
David Tolnay84ddf9e2020-10-31 15:36:48 -070025namespace {
26template <typename T>
27class impl;
28}
29
David Tolnay0f0162f2020-11-16 23:43:37 -080030#ifndef CXXBRIDGE1_RUST_STRING
31#define CXXBRIDGE1_RUST_STRING
David Tolnaye468f052020-11-29 19:39:35 -080032// https://cxx.rs/binding/string.html
David Tolnay56082162020-03-01 12:57:33 -080033class String final {
David Tolnay7db73692019-10-20 14:51:12 -040034public:
David Tolnay56082162020-03-01 12:57:33 -080035 String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080036 String(const String &) noexcept;
37 String(String &&) noexcept;
David Tolnay56082162020-03-01 12:57:33 -080038 ~String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080039
40 String(const std::string &);
41 String(const char *);
David Tolnayc2bbd952020-07-29 18:15:26 -070042 String(const char *, size_t);
David Tolnayd9c4ac92020-03-01 20:33:58 -080043
44 String &operator=(const String &) noexcept;
45 String &operator=(String &&) noexcept;
46
David Tolnay404d6892020-03-01 20:19:41 -080047 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040048
49 // Note: no null terminator.
50 const char *data() const noexcept;
51 size_t size() const noexcept;
52 size_t length() const noexcept;
53
David Tolnayff7f5fb2020-11-25 20:50:32 -080054 using iterator = char *;
55 iterator begin() noexcept;
56 iterator end() noexcept;
57
58 using const_iterator = const char *;
59 const_iterator begin() const noexcept;
60 const_iterator end() const noexcept;
61 const_iterator cbegin() const noexcept;
62 const_iterator cend() const noexcept;
63
David Tolnayff86dce2020-11-29 19:45:13 -080064 bool operator==(const String &) const noexcept;
65 bool operator!=(const String &) const noexcept;
66 bool operator<(const String &) const noexcept;
67 bool operator<=(const String &) const noexcept;
68 bool operator>(const String &) const noexcept;
69 bool operator>=(const String &) const noexcept;
70
David Tolnayd1e2efc2020-03-03 22:25:43 -080071 // Internal API only intended for the cxxbridge code generator.
72 String(unsafe_bitcopy_t, const String &) noexcept;
73
David Tolnay7db73692019-10-20 14:51:12 -040074private:
75 // Size and alignment statically verified by rust_string.rs.
76 std::array<uintptr_t, 3> repr;
77};
David Tolnay0f0162f2020-11-16 23:43:37 -080078#endif // CXXBRIDGE1_RUST_STRING
David Tolnay7db73692019-10-20 14:51:12 -040079
David Tolnay0f0162f2020-11-16 23:43:37 -080080#ifndef CXXBRIDGE1_RUST_STR
David Tolnaye468f052020-11-29 19:39:35 -080081// https://cxx.rs/binding/str.html
David Tolnay09dbe752020-03-01 13:00:40 -080082class Str final {
David Tolnay7db73692019-10-20 14:51:12 -040083public:
David Tolnay09dbe752020-03-01 13:00:40 -080084 Str() noexcept;
David Tolnay828e5132020-11-29 20:40:40 -080085 Str(const String &) noexcept;
David Tolnay851677c2020-03-01 23:49:46 -080086 Str(const std::string &);
87 Str(const char *);
David Tolnay894c5e42020-07-29 18:20:00 -070088 Str(const char *, size_t);
David Tolnayd9c4ac92020-03-01 20:33:58 -080089
David Tolnay2d7f1172020-10-31 17:58:31 -070090 Str &operator=(const Str &) noexcept = default;
David Tolnayd9c4ac92020-03-01 20:33:58 -080091
David Tolnay404d6892020-03-01 20:19:41 -080092 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040093
94 // Note: no null terminator.
95 const char *data() const noexcept;
96 size_t size() const noexcept;
97 size_t length() const noexcept;
98
David Tolnayde9a5b12020-10-31 12:15:43 -070099 // Important in order for System V ABI to pass in registers.
100 Str(const Str &) noexcept = default;
101 ~Str() noexcept = default;
102
David Tolnayff7f5fb2020-11-25 20:50:32 -0800103 using iterator = const char *;
104 using const_iterator = const char *;
105 const_iterator begin() const noexcept;
106 const_iterator end() const noexcept;
107 const_iterator cbegin() const noexcept;
108 const_iterator cend() const noexcept;
109
David Tolnayff86dce2020-11-29 19:45:13 -0800110 bool operator==(const Str &) const noexcept;
111 bool operator!=(const Str &) const noexcept;
112 bool operator<(const Str &) const noexcept;
113 bool operator<=(const Str &) const noexcept;
114 bool operator>(const Str &) const noexcept;
115 bool operator>=(const Str &) const noexcept;
116
David Tolnay5df1f062020-10-31 12:31:10 -0700117private:
David Tolnay0356d332020-10-31 19:46:41 -0700118 friend impl<Str>;
David Tolnay7db73692019-10-20 14:51:12 -0400119 // Not necessarily ABI compatible with &str. Codegen will translate to
120 // cxx::rust_str::RustStr which matches this layout.
David Tolnay5df1f062020-10-31 12:31:10 -0700121 const char *ptr;
122 size_t len;
David Tolnay7db73692019-10-20 14:51:12 -0400123};
David Tolnay0f0162f2020-11-16 23:43:37 -0800124#endif // CXXBRIDGE1_RUST_STR
David Tolnay7db73692019-10-20 14:51:12 -0400125
David Tolnay0f0162f2020-11-16 23:43:37 -0800126#ifndef CXXBRIDGE1_RUST_SLICE
David Tolnayc5629f02020-11-23 18:32:46 -0800127namespace detail {
David Tolnayee9b9ee2020-11-25 08:28:50 -0800128template <bool>
David Tolnayc5629f02020-11-23 18:32:46 -0800129struct copy_assignable_if {};
David Tolnayce298232020-11-11 10:08:54 -0800130
David Tolnayc5629f02020-11-23 18:32:46 -0800131template <>
132struct copy_assignable_if<false> {
133 copy_assignable_if() noexcept = default;
134 copy_assignable_if(const copy_assignable_if &) noexcept = default;
135 copy_assignable_if &operator=(const copy_assignable_if &) noexcept = delete;
136 copy_assignable_if &operator=(copy_assignable_if &&) noexcept = default;
137};
138} // namespace detail
139
David Tolnaye468f052020-11-29 19:39:35 -0800140// https://cxx.rs/binding/slice.html
David Tolnayc5629f02020-11-23 18:32:46 -0800141template <typename T>
142class Slice final
143 : private detail::copy_assignable_if<std::is_const<T>::value> {
David Tolnayefe81052020-04-14 16:28:24 -0700144public:
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700145 Slice() noexcept;
David Tolnayce298232020-11-11 10:08:54 -0800146 Slice(T *, size_t count) noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700147
David Tolnay2d7f1172020-10-31 17:58:31 -0700148 Slice &operator=(const Slice<T> &) noexcept = default;
David Tolnayc5629f02020-11-23 18:32:46 -0800149 Slice &operator=(Slice<T> &&) noexcept = default;
David Tolnayefe81052020-04-14 16:28:24 -0700150
David Tolnayce298232020-11-11 10:08:54 -0800151 T *data() const noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700152 size_t size() const noexcept;
153 size_t length() const noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700154
David Tolnayde9a5b12020-10-31 12:15:43 -0700155 // Important in order for System V ABI to pass in registers.
156 Slice(const Slice<T> &) noexcept = default;
157 ~Slice() noexcept = default;
158
David Tolnayac6cb542020-11-25 20:18:55 -0800159 class iterator;
160 iterator begin() const noexcept;
161 iterator end() const noexcept;
162
David Tolnayefe81052020-04-14 16:28:24 -0700163private:
David Tolnay36aa9e02020-10-31 23:08:21 -0700164 friend impl<Slice>;
165 // Not necessarily ABI compatible with &[T]. Codegen will translate to
David Tolnay5515a9e2020-11-25 19:07:54 -0800166 // cxx::rust_slice::RustSlice which matches this layout.
David Tolnayce298232020-11-11 10:08:54 -0800167 T *ptr;
David Tolnay36aa9e02020-10-31 23:08:21 -0700168 size_t len;
David Tolnayefe81052020-04-14 16:28:24 -0700169};
David Tolnayac6cb542020-11-25 20:18:55 -0800170
171template <typename T>
172class Slice<T>::iterator final {
173public:
174 using difference_type = ptrdiff_t;
175 using value_type = T;
176 using pointer = typename std::add_pointer<T>::type;
177 using reference = typename std::add_lvalue_reference<T>::type;
178 using iterator_category = std::forward_iterator_tag;
179
180 T &operator*() const noexcept;
181 T *operator->() const noexcept;
182 iterator &operator++() noexcept;
183 iterator operator++(int) noexcept;
184 bool operator==(const iterator &) const noexcept;
185 bool operator!=(const iterator &) const noexcept;
186
187private:
188 friend class Slice;
189 T *pos;
190};
David Tolnay0f0162f2020-11-16 23:43:37 -0800191#endif // CXXBRIDGE1_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -0700192
David Tolnay0f0162f2020-11-16 23:43:37 -0800193#ifndef CXXBRIDGE1_RUST_BOX
David Tolnaye468f052020-11-29 19:39:35 -0800194// https://cxx.rs/binding/box.html
David Tolnayf262d382020-04-11 22:12:40 -0700195template <typename T>
196class Box final {
David Tolnay7db73692019-10-20 14:51:12 -0400197public:
David Tolnayf6292372020-03-01 21:09:11 -0800198 using value_type = T;
David Tolnay9706a512020-04-24 17:09:01 -0700199 using const_pointer =
200 typename std::add_pointer<typename std::add_const<T>::type>::type;
201 using pointer = typename std::add_pointer<T>::type;
David Tolnayf6292372020-03-01 21:09:11 -0800202
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700203 Box(const Box &);
204 Box(Box &&) noexcept;
205 ~Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400206
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700207 explicit Box(const T &);
208 explicit Box(T &&);
209
210 Box &operator=(const Box &);
211 Box &operator=(Box &&) noexcept;
212
213 const T *operator->() const noexcept;
214 const T &operator*() const noexcept;
215 T *operator->() noexcept;
216 T &operator*() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400217
David Tolnayf262d382020-04-11 22:12:40 -0700218 template <typename... Fields>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700219 static Box in_place(Fields &&...);
David Tolnay7ce59fc2020-04-11 11:46:33 -0700220
David Tolnay7db73692019-10-20 14:51:12 -0400221 // Important: requires that `raw` came from an into_raw call. Do not pass a
222 // pointer from `new` or any other source.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700223 static Box from_raw(T *) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400224
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700225 T *into_raw() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400226
227private:
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700228 Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400229 void uninit() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400230 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800231 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400232};
David Tolnay0f0162f2020-11-16 23:43:37 -0800233#endif // CXXBRIDGE1_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400234
David Tolnay0f0162f2020-11-16 23:43:37 -0800235#ifndef CXXBRIDGE1_RUST_VEC
David Tolnaye468f052020-11-29 19:39:35 -0800236// https://cxx.rs/binding/vec.html
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700237template <typename T>
238class Vec final {
239public:
David Tolnayc87c2152020-04-24 17:07:41 -0700240 using value_type = T;
241
David Tolnayf97c2d52020-04-25 16:37:48 -0700242 Vec() noexcept;
David Tolnayf799b372020-11-29 23:57:42 -0800243 Vec(std::initializer_list<T>);
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700244 Vec(Vec &&) noexcept;
245 ~Vec() noexcept;
David Tolnaycb800572020-04-24 20:30:43 -0700246
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700247 Vec &operator=(Vec &&) noexcept;
David Tolnayf97c2d52020-04-25 16:37:48 -0700248
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700249 size_t size() const noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700250 bool empty() const noexcept;
David Tolnay219c0792020-04-24 20:31:37 -0700251 const T *data() const noexcept;
David Tolnayfb6b73c2020-11-10 14:32:16 -0800252 T *data() noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700253
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700254 const T &operator[](size_t n) const noexcept;
255 const T &at(size_t n) const;
256
257 const T &front() const;
258 const T &back() const;
259
David Tolnayfb6b73c2020-11-10 14:32:16 -0800260 void reserve(size_t new_cap);
261 void push_back(const T &value);
262 void push_back(T &&value);
David Tolnay4e8c49a2020-11-11 10:00:18 -0800263 template <typename... Args>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800264 void emplace_back(Args &&... args);
265
David Tolnay960b5112020-11-25 13:18:28 -0800266 class iterator;
267 iterator begin() noexcept;
268 iterator end() noexcept;
269
David Tolnaya5d72c62020-11-25 13:57:16 -0800270 using const_iterator = typename Vec<const T>::iterator;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700271 const_iterator begin() const noexcept;
272 const_iterator end() const noexcept;
David Tolnay960b5112020-11-25 13:18:28 -0800273 const_iterator cbegin() const noexcept;
274 const_iterator cend() const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700275
David Tolnay313b10e2020-04-25 16:30:51 -0700276 // Internal API only intended for the cxxbridge code generator.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700277 Vec(unsafe_bitcopy_t, const Vec &) noexcept;
David Tolnay313b10e2020-04-25 16:30:51 -0700278
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700279private:
David Tolnay503d0192020-04-24 22:18:56 -0700280 static size_t stride() noexcept;
David Tolnayfb6b73c2020-11-10 14:32:16 -0800281 void reserve_total(size_t cap) noexcept;
282 void set_len(size_t len) noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700283 void drop() noexcept;
284
285 // Size and alignment statically verified by rust_vec.rs.
286 std::array<uintptr_t, 3> repr;
287};
David Tolnay66f216c2020-11-25 13:21:00 -0800288
289template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800290class Vec<T>::iterator final {
291public:
292 using difference_type = ptrdiff_t;
293 using value_type = T;
294 using pointer = typename std::add_pointer<T>::type;
295 using reference = typename std::add_lvalue_reference<T>::type;
296 using iterator_category = std::forward_iterator_tag;
297
298 T &operator*() const noexcept;
299 T *operator->() const noexcept;
300 iterator &operator++() noexcept;
301 iterator operator++(int) noexcept;
302 bool operator==(const iterator &) const noexcept;
303 bool operator!=(const iterator &) const noexcept;
304
305private:
306 friend class Vec;
David Tolnaya5d72c62020-11-25 13:57:16 -0800307 friend class Vec<typename std::remove_const<T>::type>;
David Tolnay960b5112020-11-25 13:18:28 -0800308 void *pos;
309 size_t stride;
310};
David Tolnay0f0162f2020-11-16 23:43:37 -0800311#endif // CXXBRIDGE1_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700312
David Tolnay0f0162f2020-11-16 23:43:37 -0800313#ifndef CXXBRIDGE1_RUST_FN
David Tolnaye468f052020-11-29 19:39:35 -0800314// https://cxx.rs/binding/fn.html
David Tolnayf031c322020-11-29 19:41:33 -0800315template <typename Signature>
David Tolnayf262d382020-04-11 22:12:40 -0700316class Fn;
David Tolnay75dca2e2020-03-25 20:17:52 -0700317
David Tolnayf031c322020-11-29 19:41:33 -0800318template <typename Ret, typename... Args>
319class Fn<Ret(Args...)> final {
David Tolnay75dca2e2020-03-25 20:17:52 -0700320public:
David Tolnayf031c322020-11-29 19:41:33 -0800321 Ret operator()(Args... args) const noexcept;
David Tolnay533d4582020-04-08 20:29:14 -0700322 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700323
324private:
David Tolnayf031c322020-11-29 19:41:33 -0800325 Ret (*trampoline)(Args..., void *fn) noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700326 void *fn;
327};
David Tolnay0f0162f2020-11-16 23:43:37 -0800328#endif // CXXBRIDGE1_RUST_FN
David Tolnay75dca2e2020-03-25 20:17:52 -0700329
David Tolnay0f0162f2020-11-16 23:43:37 -0800330#ifndef CXXBRIDGE1_RUST_ERROR
331#define CXXBRIDGE1_RUST_ERROR
David Tolnaye468f052020-11-29 19:39:35 -0800332// https://cxx.rs/binding/result.html
David Tolnaye4fa8732020-09-08 15:04:56 -0700333class Error final : public std::exception {
David Tolnay1e548172020-03-16 13:37:09 -0700334public:
335 Error(const Error &);
336 Error(Error &&) noexcept;
David Tolnay2714d2c2020-11-23 18:17:43 -0800337 ~Error() noexcept override;
David Tolnay7c6ac712020-10-31 17:22:28 -0700338
339 Error &operator=(const Error &);
David Tolnay15491062020-10-31 17:25:13 -0700340 Error &operator=(Error &&) noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700341
David Tolnay1e548172020-03-16 13:37:09 -0700342 const char *what() const noexcept override;
343
344private:
David Tolnaya0c9bc72020-10-31 14:37:14 -0700345 Error() noexcept = default;
David Tolnay84ddf9e2020-10-31 15:36:48 -0700346 friend impl<Error>;
David Tolnaya0c9bc72020-10-31 14:37:14 -0700347 const char *msg;
348 size_t len;
David Tolnay1e548172020-03-16 13:37:09 -0700349};
David Tolnay0f0162f2020-11-16 23:43:37 -0800350#endif // CXXBRIDGE1_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700351
David Tolnay0f0162f2020-11-16 23:43:37 -0800352#ifndef CXXBRIDGE1_RUST_ISIZE
353#define CXXBRIDGE1_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700354#if defined(_WIN32)
355using isize = SSIZE_T;
356#else
357using isize = ssize_t;
358#endif
David Tolnay0f0162f2020-11-16 23:43:37 -0800359#endif // CXXBRIDGE1_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700360
David Tolnay851677c2020-03-01 23:49:46 -0800361std::ostream &operator<<(std::ostream &, const String &);
362std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400363
David Tolnay365fc7c2020-11-25 16:08:13 -0800364#ifndef CXXBRIDGE1_RUST_OPAQUE
365#define CXXBRIDGE1_RUST_OPAQUE
366// Base class of generated opaque Rust types.
367class Opaque {
David Tolnaya857c322020-11-25 16:27:19 -0800368public:
David Tolnay365fc7c2020-11-25 16:08:13 -0800369 Opaque() = delete;
370 Opaque(const Opaque &) = delete;
371 ~Opaque() = delete;
372};
373#endif // CXXBRIDGE1_RUST_OPAQUE
374
David Tolnay174bd952020-11-02 09:23:12 -0800375// IsRelocatable<T> is used in assertions that a C++ type passed by value
376// between Rust and C++ is soundly relocatable by Rust.
377//
378// There may be legitimate reasons to opt out of the check for support of types
379// that the programmer knows are soundly Rust-movable despite not being
380// recognized as such by the C++ type system due to a move constructor or
381// destructor. To opt out of the relocatability check, do either of the
382// following things in any header used by `include!` in the bridge.
383//
384// --- if you define the type:
385// struct MyType {
386// ...
387// + using IsRelocatable = std::true_type;
388// };
389//
390// --- otherwise:
391// + template <>
392// + struct rust::IsRelocatable<MyType> : std::true_type {};
393template <typename T>
394struct IsRelocatable;
395
David Tolnay3b0c9882020-03-01 14:08:57 -0800396// Snake case aliases for use in code that uses this style for type names.
397using string = String;
398using str = Str;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800399template <typename T>
David Tolnay38c87642020-09-06 22:18:08 -0700400using slice = Slice<T>;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800401template <typename T>
David Tolnayf262d382020-04-11 22:12:40 -0700402using box = Box<T>;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800403template <typename T>
David Tolnay38c87642020-09-06 22:18:08 -0700404using vec = Vec<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700405using error = Error;
David Tolnayf262d382020-04-11 22:12:40 -0700406template <typename Signature>
David Tolnayf031c322020-11-29 19:41:33 -0800407using fn = Fn<Signature>;
David Tolnay174bd952020-11-02 09:23:12 -0800408template <typename T>
409using is_relocatable = IsRelocatable<T>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800410
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700411
412
413////////////////////////////////////////////////////////////////////////////////
414/// end public API, begin implementation details
415
David Tolnay0f0162f2020-11-16 23:43:37 -0800416#ifndef CXXBRIDGE1_PANIC
417#define CXXBRIDGE1_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700418template <typename Exception>
419void panic [[noreturn]] (const char *msg);
David Tolnay0f0162f2020-11-16 23:43:37 -0800420#endif // CXXBRIDGE1_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700421
David Tolnay0f0162f2020-11-16 23:43:37 -0800422#ifndef CXXBRIDGE1_RUST_FN
423#define CXXBRIDGE1_RUST_FN
David Tolnayf031c322020-11-29 19:41:33 -0800424template <typename Ret, typename... Args>
425Ret Fn<Ret(Args...)>::operator()(Args... args) const noexcept {
David Tolnay75dca2e2020-03-25 20:17:52 -0700426 return (*this->trampoline)(std::move(args)..., this->fn);
427}
428
David Tolnayf031c322020-11-29 19:41:33 -0800429template <typename Ret, typename... Args>
430Fn<Ret(Args...)> Fn<Ret(Args...)>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700431 return *this;
432}
David Tolnay0f0162f2020-11-16 23:43:37 -0800433#endif // CXXBRIDGE1_RUST_FN
David Tolnaya23129c2020-04-08 20:08:21 -0700434
David Tolnay0f0162f2020-11-16 23:43:37 -0800435#ifndef CXXBRIDGE1_RUST_BITCOPY
436#define CXXBRIDGE1_RUST_BITCOPY
David Tolnay48521222020-10-31 14:59:42 -0700437struct unsafe_bitcopy_t final {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700438 explicit unsafe_bitcopy_t() = default;
439};
440
441constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnay0f0162f2020-11-16 23:43:37 -0800442#endif // CXXBRIDGE1_RUST_BITCOPY
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700443
David Tolnay0f0162f2020-11-16 23:43:37 -0800444#ifndef CXXBRIDGE1_RUST_STR
445#define CXXBRIDGE1_RUST_STR
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700446inline const char *Str::data() const noexcept { return this->ptr; }
447
448inline size_t Str::size() const noexcept { return this->len; }
449
450inline size_t Str::length() const noexcept { return this->len; }
David Tolnay0f0162f2020-11-16 23:43:37 -0800451#endif // CXXBRIDGE1_RUST_STR
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700452
David Tolnay0f0162f2020-11-16 23:43:37 -0800453#ifndef CXXBRIDGE1_RUST_SLICE
454#define CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700455template <typename T>
David Tolnay7b16a392020-11-25 20:23:10 -0800456Slice<T>::Slice() noexcept : ptr(reinterpret_cast<T *>(alignof(T))), len(0) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700457
458template <typename T>
David Tolnayce298232020-11-11 10:08:54 -0800459Slice<T>::Slice(T *s, size_t count) noexcept : ptr(s), len(count) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700460
461template <typename T>
David Tolnayce298232020-11-11 10:08:54 -0800462T *Slice<T>::data() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700463 return this->ptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700464}
465
466template <typename T>
467size_t Slice<T>::size() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700468 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700469}
470
471template <typename T>
472size_t Slice<T>::length() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700473 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700474}
David Tolnayac6cb542020-11-25 20:18:55 -0800475
476template <typename T>
477T &Slice<T>::iterator::operator*() const noexcept {
478 return *this->pos;
479}
480
481template <typename T>
482T *Slice<T>::iterator::operator->() const noexcept {
483 return this->pos;
484}
485
486template <typename T>
487typename Slice<T>::iterator &Slice<T>::iterator::operator++() noexcept {
488 ++this->pos;
489 return *this;
490}
491
492template <typename T>
493typename Slice<T>::iterator Slice<T>::iterator::operator++(int) noexcept {
494 auto ret = iterator(*this);
495 ++this->pos;
496 return ret;
497}
498
499template <typename T>
500bool Slice<T>::iterator::operator==(const iterator &other) const noexcept {
501 return this->pos == other.pos;
502}
503
504template <typename T>
505bool Slice<T>::iterator::operator!=(const iterator &other) const noexcept {
506 return this->pos != other.pos;
507}
508
509template <typename T>
510typename Slice<T>::iterator Slice<T>::begin() const noexcept {
511 iterator it;
512 it.pos = this->ptr;
513 return it;
514}
515
516template <typename T>
517typename Slice<T>::iterator Slice<T>::end() const noexcept {
518 iterator it = this->begin();
519 it.pos += this->len;
520 return it;
521}
David Tolnay0f0162f2020-11-16 23:43:37 -0800522#endif // CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700523
David Tolnay0f0162f2020-11-16 23:43:37 -0800524#ifndef CXXBRIDGE1_RUST_BOX
525#define CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700526template <typename T>
527Box<T>::Box(const Box &other) : Box(*other) {}
528
529template <typename T>
530Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
531 other.ptr = nullptr;
532}
533
534template <typename T>
535Box<T>::Box(const T &val) {
536 this->uninit();
537 ::new (this->ptr) T(val);
538}
539
540template <typename T>
541Box<T>::Box(T &&val) {
542 this->uninit();
543 ::new (this->ptr) T(std::move(val));
544}
545
546template <typename T>
547Box<T>::~Box() noexcept {
548 if (this->ptr) {
549 this->drop();
550 }
551}
552
553template <typename T>
554Box<T> &Box<T>::operator=(const Box &other) {
555 if (this != &other) {
556 if (this->ptr) {
557 **this = *other;
558 } else {
559 this->uninit();
560 ::new (this->ptr) T(*other);
561 }
562 }
563 return *this;
564}
565
566template <typename T>
567Box<T> &Box<T>::operator=(Box &&other) noexcept {
568 if (this->ptr) {
569 this->drop();
570 }
571 this->ptr = other.ptr;
572 other.ptr = nullptr;
573 return *this;
574}
575
576template <typename T>
577const T *Box<T>::operator->() const noexcept {
578 return this->ptr;
579}
580
581template <typename T>
582const T &Box<T>::operator*() const noexcept {
583 return *this->ptr;
584}
585
586template <typename T>
587T *Box<T>::operator->() noexcept {
588 return this->ptr;
589}
590
591template <typename T>
592T &Box<T>::operator*() noexcept {
593 return *this->ptr;
594}
595
596template <typename T>
597template <typename... Fields>
598Box<T> Box<T>::in_place(Fields &&... fields) {
599 Box box;
600 box.uninit();
601 ::new (box.ptr) T{std::forward<Fields>(fields)...};
602 return box;
603}
604
605template <typename T>
606Box<T> Box<T>::from_raw(T *raw) noexcept {
607 Box box;
608 box.ptr = raw;
609 return box;
610}
611
612template <typename T>
613T *Box<T>::into_raw() noexcept {
614 T *raw = this->ptr;
615 this->ptr = nullptr;
616 return raw;
617}
618
619template <typename T>
David Tolnay79076c72020-11-23 18:16:55 -0800620Box<T>::Box() noexcept = default;
David Tolnay0f0162f2020-11-16 23:43:37 -0800621#endif // CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700622
David Tolnay0f0162f2020-11-16 23:43:37 -0800623#ifndef CXXBRIDGE1_RUST_VEC
624#define CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700625template <typename T>
David Tolnayf799b372020-11-29 23:57:42 -0800626Vec<T>::Vec(std::initializer_list<T> init) : Vec{} {
627 this->reserve_total(init.size());
628 std::move(init.begin(), init.end(), std::back_inserter(*this));
629}
630
631template <typename T>
David Tolnay15671862020-11-23 18:13:56 -0800632Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700633 new (&other) Vec();
634}
635
636template <typename T>
637Vec<T>::~Vec() noexcept {
638 this->drop();
639}
640
641template <typename T>
642Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
643 if (this != &other) {
644 this->drop();
645 this->repr = other.repr;
646 new (&other) Vec();
647 }
648 return *this;
649}
650
651template <typename T>
652bool Vec<T>::empty() const noexcept {
653 return size() == 0;
654}
655
656template <typename T>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800657T *Vec<T>::data() noexcept {
658 return const_cast<T *>(const_cast<const Vec<T> *>(this)->data());
659}
660
661template <typename T>
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700662const T &Vec<T>::operator[](size_t n) const noexcept {
663 auto data = reinterpret_cast<const char *>(this->data());
664 return *reinterpret_cast<const T *>(data + n * this->stride());
665}
666
667template <typename T>
668const T &Vec<T>::at(size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700669 if (n >= this->size()) {
670 panic<std::out_of_range>("rust::Vec index out of range");
671 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700672 return (*this)[n];
673}
674
675template <typename T>
676const T &Vec<T>::front() const {
677 return (*this)[0];
678}
679
680template <typename T>
681const T &Vec<T>::back() const {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700682 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700683}
684
685template <typename T>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800686void Vec<T>::reserve(size_t new_cap) {
687 this->reserve_total(new_cap);
688}
689
690template <typename T>
691void Vec<T>::push_back(const T &value) {
692 this->emplace_back(value);
693}
694
695template <typename T>
696void Vec<T>::push_back(T &&value) {
697 this->emplace_back(std::move(value));
698}
699
700template <typename T>
701template <typename... Args>
702void Vec<T>::emplace_back(Args &&... args) {
703 auto size = this->size();
704 this->reserve_total(size + 1);
705 ::new (reinterpret_cast<T *>(reinterpret_cast<char *>(this->data()) +
706 size * this->stride()))
707 T(std::forward<Args>(args)...);
708 this->set_len(size + 1);
709}
710
711template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800712T &Vec<T>::iterator::operator*() const noexcept {
713 return *static_cast<T *>(this->pos);
714}
715
716template <typename T>
717T *Vec<T>::iterator::operator->() const noexcept {
718 return static_cast<T *>(this->pos);
719}
720
721template <typename T>
722typename Vec<T>::iterator &Vec<T>::iterator::operator++() noexcept {
David Tolnay248215e2020-11-25 19:38:26 -0800723 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnay960b5112020-11-25 13:18:28 -0800724 return *this;
725}
726
727template <typename T>
728typename Vec<T>::iterator Vec<T>::iterator::operator++(int) noexcept {
729 auto ret = iterator(*this);
David Tolnay248215e2020-11-25 19:38:26 -0800730 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnay960b5112020-11-25 13:18:28 -0800731 return ret;
732}
733
734template <typename T>
735bool Vec<T>::iterator::operator==(const iterator &other) const noexcept {
736 return this->pos == other.pos;
737}
738
739template <typename T>
740bool Vec<T>::iterator::operator!=(const iterator &other) const noexcept {
741 return this->pos != other.pos;
742}
743
744template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800745typename Vec<T>::iterator Vec<T>::begin() noexcept {
746 iterator it;
David Tolnaya5d72c62020-11-25 13:57:16 -0800747 it.pos = const_cast<typename std::remove_const<T>::type *>(this->data());
David Tolnay960b5112020-11-25 13:18:28 -0800748 it.stride = this->stride();
749 return it;
750}
751
752template <typename T>
753typename Vec<T>::iterator Vec<T>::end() noexcept {
754 iterator it = this->begin();
David Tolnay248215e2020-11-25 19:38:26 -0800755 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
David Tolnay960b5112020-11-25 13:18:28 -0800756 return it;
757}
758
759template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700760typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800761 return this->cbegin();
762}
763
764template <typename T>
765typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
766 return this->cend();
767}
768
769template <typename T>
770typename Vec<T>::const_iterator Vec<T>::cbegin() const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700771 const_iterator it;
David Tolnaya5d72c62020-11-25 13:57:16 -0800772 it.pos = const_cast<typename std::remove_const<T>::type *>(this->data());
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700773 it.stride = this->stride();
774 return it;
775}
776
777template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800778typename Vec<T>::const_iterator Vec<T>::cend() const noexcept {
779 const_iterator it = this->cbegin();
David Tolnay248215e2020-11-25 19:38:26 -0800780 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700781 return it;
782}
783
784// Internal API only intended for the cxxbridge code generator.
785template <typename T>
786Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay0f0162f2020-11-16 23:43:37 -0800787#endif // CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700788
David Tolnay0f0162f2020-11-16 23:43:37 -0800789#ifndef CXXBRIDGE1_RELOCATABLE
790#define CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -0800791namespace detail {
792template <typename... Ts>
793struct make_void {
794 using type = void;
795};
796
797template <typename... Ts>
798using void_t = typename make_void<Ts...>::type;
799
800template <typename Void, template <typename...> class, typename...>
801struct detect : std::false_type {};
802template <template <typename...> class T, typename... A>
803struct detect<void_t<T<A...>>, T, A...> : std::true_type {};
804
805template <template <typename...> class T, typename... A>
806using is_detected = detect<void, T, A...>;
807
808template <typename T>
809using detect_IsRelocatable = typename T::IsRelocatable;
810
811template <typename T>
812struct get_IsRelocatable
813 : std::is_same<typename T::IsRelocatable, std::true_type> {};
814} // namespace detail
815
816template <typename T>
817struct IsRelocatable
818 : std::conditional<
819 detail::is_detected<detail::detect_IsRelocatable, T>::value,
820 detail::get_IsRelocatable<T>,
821 std::integral_constant<
822 bool, std::is_trivially_move_constructible<T>::value &&
823 std::is_trivially_destructible<T>::value>>::type {};
David Tolnay0f0162f2020-11-16 23:43:37 -0800824#endif // CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -0800825
David Tolnay0f0162f2020-11-16 23:43:37 -0800826} // namespace cxxbridge1
David Tolnay750755e2020-03-01 13:04:08 -0800827} // namespace rust