blob: 86de118aa710829b0b7504b9437dce5ed2e3d890 [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 Tolnaydc62d712020-12-11 13:51:53 -0800253 size_t capacity() const noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700254
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700255 const T &operator[](size_t n) const noexcept;
256 const T &at(size_t n) const;
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700257 const T &front() const;
258 const T &back() const;
259
David Tolnay908d5e52020-11-30 00:30:59 -0800260 T &operator[](size_t n) noexcept;
261 T &at(size_t n);
262 T &front();
263 T &back();
264
David Tolnayfb6b73c2020-11-10 14:32:16 -0800265 void reserve(size_t new_cap);
266 void push_back(const T &value);
267 void push_back(T &&value);
David Tolnay4e8c49a2020-11-11 10:00:18 -0800268 template <typename... Args>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800269 void emplace_back(Args &&... args);
270
David Tolnay960b5112020-11-25 13:18:28 -0800271 class iterator;
272 iterator begin() noexcept;
273 iterator end() noexcept;
274
David Tolnaya5d72c62020-11-25 13:57:16 -0800275 using const_iterator = typename Vec<const T>::iterator;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700276 const_iterator begin() const noexcept;
277 const_iterator end() const noexcept;
David Tolnay960b5112020-11-25 13:18:28 -0800278 const_iterator cbegin() const noexcept;
279 const_iterator cend() const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700280
David Tolnay313b10e2020-04-25 16:30:51 -0700281 // Internal API only intended for the cxxbridge code generator.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700282 Vec(unsafe_bitcopy_t, const Vec &) noexcept;
David Tolnay313b10e2020-04-25 16:30:51 -0700283
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700284private:
David Tolnay503d0192020-04-24 22:18:56 -0700285 static size_t stride() noexcept;
David Tolnayfb6b73c2020-11-10 14:32:16 -0800286 void reserve_total(size_t cap) noexcept;
287 void set_len(size_t len) noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700288 void drop() noexcept;
289
290 // Size and alignment statically verified by rust_vec.rs.
291 std::array<uintptr_t, 3> repr;
292};
David Tolnay66f216c2020-11-25 13:21:00 -0800293
294template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800295class Vec<T>::iterator final {
296public:
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500297 using iterator_category = std::random_access_iterator_tag;
David Tolnay960b5112020-11-25 13:18:28 -0800298 using value_type = T;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500299 using difference_type = ptrdiff_t;
David Tolnay960b5112020-11-25 13:18:28 -0800300 using pointer = typename std::add_pointer<T>::type;
301 using reference = typename std::add_lvalue_reference<T>::type;
David Tolnay960b5112020-11-25 13:18:28 -0800302
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500303 reference operator*() const noexcept;
304 pointer operator->() const noexcept;
305 reference operator[](difference_type) const noexcept;
306
David Tolnay960b5112020-11-25 13:18:28 -0800307 iterator &operator++() noexcept;
308 iterator operator++(int) noexcept;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500309 iterator &operator--() noexcept;
310 iterator operator--(int) noexcept;
311
312 iterator &operator+=(difference_type) noexcept;
313 iterator &operator-=(difference_type) noexcept;
314 iterator operator+(difference_type) const noexcept;
315 iterator operator-(difference_type) const noexcept;
316 difference_type operator-(const iterator &) const noexcept;
317
David Tolnay960b5112020-11-25 13:18:28 -0800318 bool operator==(const iterator &) const noexcept;
319 bool operator!=(const iterator &) const noexcept;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500320 bool operator<(const iterator &) const noexcept;
321 bool operator>(const iterator &) const noexcept;
322 bool operator<=(const iterator &) const noexcept;
323 bool operator>=(const iterator &) const noexcept;
David Tolnay960b5112020-11-25 13:18:28 -0800324
325private:
326 friend class Vec;
David Tolnaya5d72c62020-11-25 13:57:16 -0800327 friend class Vec<typename std::remove_const<T>::type>;
David Tolnay960b5112020-11-25 13:18:28 -0800328 void *pos;
329 size_t stride;
330};
David Tolnay0f0162f2020-11-16 23:43:37 -0800331#endif // CXXBRIDGE1_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700332
David Tolnay0f0162f2020-11-16 23:43:37 -0800333#ifndef CXXBRIDGE1_RUST_FN
David Tolnaye468f052020-11-29 19:39:35 -0800334// https://cxx.rs/binding/fn.html
David Tolnayf031c322020-11-29 19:41:33 -0800335template <typename Signature>
David Tolnayf262d382020-04-11 22:12:40 -0700336class Fn;
David Tolnay75dca2e2020-03-25 20:17:52 -0700337
David Tolnayf031c322020-11-29 19:41:33 -0800338template <typename Ret, typename... Args>
339class Fn<Ret(Args...)> final {
David Tolnay75dca2e2020-03-25 20:17:52 -0700340public:
David Tolnayf031c322020-11-29 19:41:33 -0800341 Ret operator()(Args... args) const noexcept;
David Tolnay533d4582020-04-08 20:29:14 -0700342 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700343
344private:
David Tolnayf031c322020-11-29 19:41:33 -0800345 Ret (*trampoline)(Args..., void *fn) noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700346 void *fn;
347};
David Tolnay0f0162f2020-11-16 23:43:37 -0800348#endif // CXXBRIDGE1_RUST_FN
David Tolnay75dca2e2020-03-25 20:17:52 -0700349
David Tolnay0f0162f2020-11-16 23:43:37 -0800350#ifndef CXXBRIDGE1_RUST_ERROR
351#define CXXBRIDGE1_RUST_ERROR
David Tolnaye468f052020-11-29 19:39:35 -0800352// https://cxx.rs/binding/result.html
David Tolnaye4fa8732020-09-08 15:04:56 -0700353class Error final : public std::exception {
David Tolnay1e548172020-03-16 13:37:09 -0700354public:
355 Error(const Error &);
356 Error(Error &&) noexcept;
David Tolnay2714d2c2020-11-23 18:17:43 -0800357 ~Error() noexcept override;
David Tolnay7c6ac712020-10-31 17:22:28 -0700358
359 Error &operator=(const Error &);
David Tolnay15491062020-10-31 17:25:13 -0700360 Error &operator=(Error &&) noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700361
David Tolnay1e548172020-03-16 13:37:09 -0700362 const char *what() const noexcept override;
363
364private:
David Tolnaya0c9bc72020-10-31 14:37:14 -0700365 Error() noexcept = default;
David Tolnay84ddf9e2020-10-31 15:36:48 -0700366 friend impl<Error>;
David Tolnaya0c9bc72020-10-31 14:37:14 -0700367 const char *msg;
368 size_t len;
David Tolnay1e548172020-03-16 13:37:09 -0700369};
David Tolnay0f0162f2020-11-16 23:43:37 -0800370#endif // CXXBRIDGE1_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700371
David Tolnay0f0162f2020-11-16 23:43:37 -0800372#ifndef CXXBRIDGE1_RUST_ISIZE
373#define CXXBRIDGE1_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700374#if defined(_WIN32)
375using isize = SSIZE_T;
376#else
377using isize = ssize_t;
378#endif
David Tolnay0f0162f2020-11-16 23:43:37 -0800379#endif // CXXBRIDGE1_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700380
David Tolnay851677c2020-03-01 23:49:46 -0800381std::ostream &operator<<(std::ostream &, const String &);
382std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400383
David Tolnay365fc7c2020-11-25 16:08:13 -0800384#ifndef CXXBRIDGE1_RUST_OPAQUE
385#define CXXBRIDGE1_RUST_OPAQUE
386// Base class of generated opaque Rust types.
387class Opaque {
David Tolnaya857c322020-11-25 16:27:19 -0800388public:
David Tolnay365fc7c2020-11-25 16:08:13 -0800389 Opaque() = delete;
390 Opaque(const Opaque &) = delete;
391 ~Opaque() = delete;
392};
393#endif // CXXBRIDGE1_RUST_OPAQUE
394
David Tolnay174bd952020-11-02 09:23:12 -0800395// IsRelocatable<T> is used in assertions that a C++ type passed by value
396// between Rust and C++ is soundly relocatable by Rust.
397//
398// There may be legitimate reasons to opt out of the check for support of types
399// that the programmer knows are soundly Rust-movable despite not being
400// recognized as such by the C++ type system due to a move constructor or
401// destructor. To opt out of the relocatability check, do either of the
402// following things in any header used by `include!` in the bridge.
403//
404// --- if you define the type:
405// struct MyType {
406// ...
407// + using IsRelocatable = std::true_type;
408// };
409//
410// --- otherwise:
411// + template <>
412// + struct rust::IsRelocatable<MyType> : std::true_type {};
413template <typename T>
414struct IsRelocatable;
415
David Tolnay3b0c9882020-03-01 14:08:57 -0800416// Snake case aliases for use in code that uses this style for type names.
417using string = String;
418using str = Str;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800419template <typename T>
David Tolnay38c87642020-09-06 22:18:08 -0700420using slice = Slice<T>;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800421template <typename T>
David Tolnayf262d382020-04-11 22:12:40 -0700422using box = Box<T>;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800423template <typename T>
David Tolnay38c87642020-09-06 22:18:08 -0700424using vec = Vec<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700425using error = Error;
David Tolnayf262d382020-04-11 22:12:40 -0700426template <typename Signature>
David Tolnayf031c322020-11-29 19:41:33 -0800427using fn = Fn<Signature>;
David Tolnay174bd952020-11-02 09:23:12 -0800428template <typename T>
429using is_relocatable = IsRelocatable<T>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800430
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700431
432
433////////////////////////////////////////////////////////////////////////////////
434/// end public API, begin implementation details
435
David Tolnay0f0162f2020-11-16 23:43:37 -0800436#ifndef CXXBRIDGE1_PANIC
437#define CXXBRIDGE1_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700438template <typename Exception>
439void panic [[noreturn]] (const char *msg);
David Tolnay0f0162f2020-11-16 23:43:37 -0800440#endif // CXXBRIDGE1_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700441
David Tolnay0f0162f2020-11-16 23:43:37 -0800442#ifndef CXXBRIDGE1_RUST_FN
443#define CXXBRIDGE1_RUST_FN
David Tolnayf031c322020-11-29 19:41:33 -0800444template <typename Ret, typename... Args>
445Ret Fn<Ret(Args...)>::operator()(Args... args) const noexcept {
David Tolnay75dca2e2020-03-25 20:17:52 -0700446 return (*this->trampoline)(std::move(args)..., this->fn);
447}
448
David Tolnayf031c322020-11-29 19:41:33 -0800449template <typename Ret, typename... Args>
450Fn<Ret(Args...)> Fn<Ret(Args...)>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700451 return *this;
452}
David Tolnay0f0162f2020-11-16 23:43:37 -0800453#endif // CXXBRIDGE1_RUST_FN
David Tolnaya23129c2020-04-08 20:08:21 -0700454
David Tolnay0f0162f2020-11-16 23:43:37 -0800455#ifndef CXXBRIDGE1_RUST_BITCOPY
456#define CXXBRIDGE1_RUST_BITCOPY
David Tolnay48521222020-10-31 14:59:42 -0700457struct unsafe_bitcopy_t final {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700458 explicit unsafe_bitcopy_t() = default;
459};
460
461constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnay0f0162f2020-11-16 23:43:37 -0800462#endif // CXXBRIDGE1_RUST_BITCOPY
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700463
David Tolnay0f0162f2020-11-16 23:43:37 -0800464#ifndef CXXBRIDGE1_RUST_STR
465#define CXXBRIDGE1_RUST_STR
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700466inline const char *Str::data() const noexcept { return this->ptr; }
467
468inline size_t Str::size() const noexcept { return this->len; }
469
470inline size_t Str::length() const noexcept { return this->len; }
David Tolnay0f0162f2020-11-16 23:43:37 -0800471#endif // CXXBRIDGE1_RUST_STR
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700472
David Tolnay0f0162f2020-11-16 23:43:37 -0800473#ifndef CXXBRIDGE1_RUST_SLICE
474#define CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700475template <typename T>
David Tolnay7b16a392020-11-25 20:23:10 -0800476Slice<T>::Slice() noexcept : ptr(reinterpret_cast<T *>(alignof(T))), len(0) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700477
478template <typename T>
David Tolnayce298232020-11-11 10:08:54 -0800479Slice<T>::Slice(T *s, size_t count) noexcept : ptr(s), len(count) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700480
481template <typename T>
David Tolnayce298232020-11-11 10:08:54 -0800482T *Slice<T>::data() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700483 return this->ptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700484}
485
486template <typename T>
487size_t Slice<T>::size() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700488 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700489}
490
491template <typename T>
492size_t Slice<T>::length() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700493 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700494}
David Tolnayac6cb542020-11-25 20:18:55 -0800495
496template <typename T>
497T &Slice<T>::iterator::operator*() const noexcept {
498 return *this->pos;
499}
500
501template <typename T>
502T *Slice<T>::iterator::operator->() const noexcept {
503 return this->pos;
504}
505
506template <typename T>
507typename Slice<T>::iterator &Slice<T>::iterator::operator++() noexcept {
508 ++this->pos;
509 return *this;
510}
511
512template <typename T>
513typename Slice<T>::iterator Slice<T>::iterator::operator++(int) noexcept {
514 auto ret = iterator(*this);
515 ++this->pos;
516 return ret;
517}
518
519template <typename T>
520bool Slice<T>::iterator::operator==(const iterator &other) const noexcept {
521 return this->pos == other.pos;
522}
523
524template <typename T>
525bool Slice<T>::iterator::operator!=(const iterator &other) const noexcept {
526 return this->pos != other.pos;
527}
528
529template <typename T>
530typename Slice<T>::iterator Slice<T>::begin() const noexcept {
531 iterator it;
532 it.pos = this->ptr;
533 return it;
534}
535
536template <typename T>
537typename Slice<T>::iterator Slice<T>::end() const noexcept {
538 iterator it = this->begin();
539 it.pos += this->len;
540 return it;
541}
David Tolnay0f0162f2020-11-16 23:43:37 -0800542#endif // CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700543
David Tolnay0f0162f2020-11-16 23:43:37 -0800544#ifndef CXXBRIDGE1_RUST_BOX
545#define CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700546template <typename T>
547Box<T>::Box(const Box &other) : Box(*other) {}
548
549template <typename T>
550Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
551 other.ptr = nullptr;
552}
553
554template <typename T>
555Box<T>::Box(const T &val) {
556 this->uninit();
557 ::new (this->ptr) T(val);
558}
559
560template <typename T>
561Box<T>::Box(T &&val) {
562 this->uninit();
563 ::new (this->ptr) T(std::move(val));
564}
565
566template <typename T>
567Box<T>::~Box() noexcept {
568 if (this->ptr) {
569 this->drop();
570 }
571}
572
573template <typename T>
574Box<T> &Box<T>::operator=(const Box &other) {
575 if (this != &other) {
576 if (this->ptr) {
577 **this = *other;
578 } else {
579 this->uninit();
580 ::new (this->ptr) T(*other);
581 }
582 }
583 return *this;
584}
585
586template <typename T>
587Box<T> &Box<T>::operator=(Box &&other) noexcept {
588 if (this->ptr) {
589 this->drop();
590 }
591 this->ptr = other.ptr;
592 other.ptr = nullptr;
593 return *this;
594}
595
596template <typename T>
597const T *Box<T>::operator->() const noexcept {
598 return this->ptr;
599}
600
601template <typename T>
602const T &Box<T>::operator*() const noexcept {
603 return *this->ptr;
604}
605
606template <typename T>
607T *Box<T>::operator->() noexcept {
608 return this->ptr;
609}
610
611template <typename T>
612T &Box<T>::operator*() noexcept {
613 return *this->ptr;
614}
615
616template <typename T>
617template <typename... Fields>
618Box<T> Box<T>::in_place(Fields &&... fields) {
619 Box box;
620 box.uninit();
621 ::new (box.ptr) T{std::forward<Fields>(fields)...};
622 return box;
623}
624
625template <typename T>
626Box<T> Box<T>::from_raw(T *raw) noexcept {
627 Box box;
628 box.ptr = raw;
629 return box;
630}
631
632template <typename T>
633T *Box<T>::into_raw() noexcept {
634 T *raw = this->ptr;
635 this->ptr = nullptr;
636 return raw;
637}
638
639template <typename T>
David Tolnay79076c72020-11-23 18:16:55 -0800640Box<T>::Box() noexcept = default;
David Tolnay0f0162f2020-11-16 23:43:37 -0800641#endif // CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700642
David Tolnay0f0162f2020-11-16 23:43:37 -0800643#ifndef CXXBRIDGE1_RUST_VEC
644#define CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700645template <typename T>
David Tolnayf799b372020-11-29 23:57:42 -0800646Vec<T>::Vec(std::initializer_list<T> init) : Vec{} {
647 this->reserve_total(init.size());
648 std::move(init.begin(), init.end(), std::back_inserter(*this));
649}
650
651template <typename T>
David Tolnay15671862020-11-23 18:13:56 -0800652Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700653 new (&other) Vec();
654}
655
656template <typename T>
657Vec<T>::~Vec() noexcept {
658 this->drop();
659}
660
661template <typename T>
662Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
663 if (this != &other) {
664 this->drop();
665 this->repr = other.repr;
666 new (&other) Vec();
667 }
668 return *this;
669}
670
671template <typename T>
672bool Vec<T>::empty() const noexcept {
673 return size() == 0;
674}
675
676template <typename T>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800677T *Vec<T>::data() noexcept {
678 return const_cast<T *>(const_cast<const Vec<T> *>(this)->data());
679}
680
681template <typename T>
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700682const T &Vec<T>::operator[](size_t n) const noexcept {
683 auto data = reinterpret_cast<const char *>(this->data());
684 return *reinterpret_cast<const T *>(data + n * this->stride());
685}
686
687template <typename T>
688const T &Vec<T>::at(size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700689 if (n >= this->size()) {
690 panic<std::out_of_range>("rust::Vec index out of range");
691 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700692 return (*this)[n];
693}
694
695template <typename T>
696const T &Vec<T>::front() const {
697 return (*this)[0];
698}
699
700template <typename T>
701const T &Vec<T>::back() const {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700702 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700703}
704
705template <typename T>
David Tolnay908d5e52020-11-30 00:30:59 -0800706T &Vec<T>::operator[](size_t n) noexcept {
707 auto data = reinterpret_cast<char *>(this->data());
708 return *reinterpret_cast<T *>(data + n * this->stride());
709}
710
711template <typename T>
712T &Vec<T>::at(size_t n) {
713 if (n >= this->size()) {
714 panic<std::out_of_range>("rust::Vec index out of range");
715 }
716 return (*this)[n];
717}
718
719template <typename T>
720T &Vec<T>::front() {
721 return (*this)[0];
722}
723
724template <typename T>
725T &Vec<T>::back() {
726 return (*this)[this->size() - 1];
727}
728
729template <typename T>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800730void Vec<T>::reserve(size_t new_cap) {
731 this->reserve_total(new_cap);
732}
733
734template <typename T>
735void Vec<T>::push_back(const T &value) {
736 this->emplace_back(value);
737}
738
739template <typename T>
740void Vec<T>::push_back(T &&value) {
741 this->emplace_back(std::move(value));
742}
743
744template <typename T>
745template <typename... Args>
746void Vec<T>::emplace_back(Args &&... args) {
747 auto size = this->size();
748 this->reserve_total(size + 1);
749 ::new (reinterpret_cast<T *>(reinterpret_cast<char *>(this->data()) +
750 size * this->stride()))
751 T(std::forward<Args>(args)...);
752 this->set_len(size + 1);
753}
754
755template <typename T>
David Tolnay300072b2020-12-03 12:12:24 -0800756typename Vec<T>::iterator::reference
757Vec<T>::iterator::operator*() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800758 return *static_cast<T *>(this->pos);
759}
760
761template <typename T>
David Tolnay300072b2020-12-03 12:12:24 -0800762typename Vec<T>::iterator::pointer
763Vec<T>::iterator::operator->() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800764 return static_cast<T *>(this->pos);
765}
766
767template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500768typename Vec<T>::iterator::reference Vec<T>::iterator::operator[](
769 Vec<T>::iterator::difference_type n) const noexcept {
770 auto pos = static_cast<char *>(this->pos) + this->stride * n;
771 return *static_cast<T *>(pos);
772}
773
774template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800775typename Vec<T>::iterator &Vec<T>::iterator::operator++() noexcept {
David Tolnay248215e2020-11-25 19:38:26 -0800776 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnay960b5112020-11-25 13:18:28 -0800777 return *this;
778}
779
780template <typename T>
781typename Vec<T>::iterator Vec<T>::iterator::operator++(int) noexcept {
782 auto ret = iterator(*this);
David Tolnay248215e2020-11-25 19:38:26 -0800783 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnay960b5112020-11-25 13:18:28 -0800784 return ret;
785}
786
787template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500788typename Vec<T>::iterator &Vec<T>::iterator::operator--() noexcept {
789 this->pos = static_cast<char *>(this->pos) - this->stride;
790 return *this;
791}
792
793template <typename T>
794typename Vec<T>::iterator Vec<T>::iterator::operator--(int) noexcept {
795 auto ret = iterator(*this);
796 this->pos = static_cast<char *>(this->pos) - this->stride;
797 return ret;
798}
799
800template <typename T>
801typename Vec<T>::iterator &
802Vec<T>::iterator::operator+=(Vec<T>::iterator::difference_type n) noexcept {
803 this->pos = static_cast<char *>(this->pos) + this->stride * n;
804 return *this;
805}
806
807template <typename T>
808typename Vec<T>::iterator &
809Vec<T>::iterator::operator-=(Vec<T>::iterator::difference_type n) noexcept {
810 this->pos = static_cast<char *>(this->pos) - this->stride * n;
811 return *this;
812}
813
814template <typename T>
815typename Vec<T>::iterator Vec<T>::iterator::operator+(
816 Vec<T>::iterator::difference_type n) const noexcept {
David Tolnay300072b2020-12-03 12:12:24 -0800817 auto ret = iterator(*this);
818 ret.pos = static_cast<char *>(this->pos) + this->stride * n;
819 return ret;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500820}
821
822template <typename T>
823typename Vec<T>::iterator Vec<T>::iterator::operator-(
824 Vec<T>::iterator::difference_type n) const noexcept {
David Tolnay300072b2020-12-03 12:12:24 -0800825 auto ret = iterator(*this);
826 ret.pos = static_cast<char *>(this->pos) - this->stride * n;
827 return ret;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500828}
829
830template <typename T>
831typename Vec<T>::iterator::difference_type
832Vec<T>::iterator::operator-(const iterator &other) const noexcept {
833 auto diff = std::distance(static_cast<char *>(other.pos),
834 static_cast<char *>(this->pos));
David Tolnay300072b2020-12-03 12:12:24 -0800835 return diff / this->stride;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500836}
837
838template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800839bool Vec<T>::iterator::operator==(const iterator &other) const noexcept {
840 return this->pos == other.pos;
841}
842
843template <typename T>
844bool Vec<T>::iterator::operator!=(const iterator &other) const noexcept {
845 return this->pos != other.pos;
846}
847
848template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500849bool Vec<T>::iterator::operator>(const iterator &other) const noexcept {
850 return this->pos > other.pos;
851}
852
853template <typename T>
854bool Vec<T>::iterator::operator<(const iterator &other) const noexcept {
855 return this->pos < other.pos;
856}
857
858template <typename T>
859bool Vec<T>::iterator::operator>=(const iterator &other) const noexcept {
860 return this->pos >= other.pos;
861}
862
863template <typename T>
864bool Vec<T>::iterator::operator<=(const iterator &other) const noexcept {
865 return this->pos <= other.pos;
866}
867
868template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800869typename Vec<T>::iterator Vec<T>::begin() noexcept {
870 iterator it;
David Tolnaya5d72c62020-11-25 13:57:16 -0800871 it.pos = const_cast<typename std::remove_const<T>::type *>(this->data());
David Tolnay960b5112020-11-25 13:18:28 -0800872 it.stride = this->stride();
873 return it;
874}
875
876template <typename T>
877typename Vec<T>::iterator Vec<T>::end() noexcept {
878 iterator it = this->begin();
David Tolnay248215e2020-11-25 19:38:26 -0800879 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
David Tolnay960b5112020-11-25 13:18:28 -0800880 return it;
881}
882
883template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700884typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800885 return this->cbegin();
886}
887
888template <typename T>
889typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
890 return this->cend();
891}
892
893template <typename T>
894typename Vec<T>::const_iterator Vec<T>::cbegin() const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700895 const_iterator it;
David Tolnaya5d72c62020-11-25 13:57:16 -0800896 it.pos = const_cast<typename std::remove_const<T>::type *>(this->data());
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700897 it.stride = this->stride();
898 return it;
899}
900
901template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800902typename Vec<T>::const_iterator Vec<T>::cend() const noexcept {
903 const_iterator it = this->cbegin();
David Tolnay248215e2020-11-25 19:38:26 -0800904 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700905 return it;
906}
907
908// Internal API only intended for the cxxbridge code generator.
909template <typename T>
910Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay0f0162f2020-11-16 23:43:37 -0800911#endif // CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700912
David Tolnay0f0162f2020-11-16 23:43:37 -0800913#ifndef CXXBRIDGE1_RELOCATABLE
914#define CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -0800915namespace detail {
916template <typename... Ts>
917struct make_void {
918 using type = void;
919};
920
921template <typename... Ts>
922using void_t = typename make_void<Ts...>::type;
923
924template <typename Void, template <typename...> class, typename...>
925struct detect : std::false_type {};
926template <template <typename...> class T, typename... A>
927struct detect<void_t<T<A...>>, T, A...> : std::true_type {};
928
929template <template <typename...> class T, typename... A>
930using is_detected = detect<void, T, A...>;
931
932template <typename T>
933using detect_IsRelocatable = typename T::IsRelocatable;
934
935template <typename T>
936struct get_IsRelocatable
937 : std::is_same<typename T::IsRelocatable, std::true_type> {};
938} // namespace detail
939
940template <typename T>
941struct IsRelocatable
942 : std::conditional<
943 detail::is_detected<detail::detect_IsRelocatable, T>::value,
944 detail::get_IsRelocatable<T>,
945 std::integral_constant<
946 bool, std::is_trivially_move_constructible<T>::value &&
947 std::is_trivially_destructible<T>::value>>::type {};
David Tolnay0f0162f2020-11-16 23:43:37 -0800948#endif // CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -0800949
David Tolnay0f0162f2020-11-16 23:43:37 -0800950} // namespace cxxbridge1
David Tolnay750755e2020-03-01 13:04:08 -0800951} // namespace rust