blob: e1d7fa0beb1742e5cea82b181ce4430cf6cfb183 [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 Tolnay74dd66f2020-12-12 22:03:47 -080018#else
19#include <sys/types.h>
David Tolnay59b5ba12020-04-10 11:32:19 -070020#endif
David Tolnay7db73692019-10-20 14:51:12 -040021
David Tolnay750755e2020-03-01 13:04:08 -080022namespace rust {
David Tolnay0f0162f2020-11-16 23:43:37 -080023inline namespace cxxbridge1 {
David Tolnay7db73692019-10-20 14:51:12 -040024
David Tolnay2a2b9ad2020-05-12 20:07:26 -070025struct unsafe_bitcopy_t;
David Tolnayd1e2efc2020-03-03 22:25:43 -080026
David Tolnay84ddf9e2020-10-31 15:36:48 -070027namespace {
28template <typename T>
29class impl;
30}
31
David Tolnay0f0162f2020-11-16 23:43:37 -080032#ifndef CXXBRIDGE1_RUST_STRING
33#define CXXBRIDGE1_RUST_STRING
David Tolnaye468f052020-11-29 19:39:35 -080034// https://cxx.rs/binding/string.html
David Tolnay56082162020-03-01 12:57:33 -080035class String final {
David Tolnay7db73692019-10-20 14:51:12 -040036public:
David Tolnay56082162020-03-01 12:57:33 -080037 String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080038 String(const String &) noexcept;
39 String(String &&) noexcept;
David Tolnay56082162020-03-01 12:57:33 -080040 ~String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080041
42 String(const std::string &);
43 String(const char *);
David Tolnaybe3cbf72020-12-12 22:12:07 -080044 String(const char *, std::size_t);
David Tolnayd9c4ac92020-03-01 20:33:58 -080045
46 String &operator=(const String &) noexcept;
47 String &operator=(String &&) noexcept;
48
David Tolnay404d6892020-03-01 20:19:41 -080049 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040050
51 // Note: no null terminator.
52 const char *data() const noexcept;
David Tolnaybe3cbf72020-12-12 22:12:07 -080053 std::size_t size() const noexcept;
54 std::size_t length() const noexcept;
David Tolnay7db73692019-10-20 14:51:12 -040055
David Tolnaycca2e612020-12-18 12:48:22 -080056 const char *c_str() noexcept;
57
David Tolnayff7f5fb2020-11-25 20:50:32 -080058 using iterator = char *;
59 iterator begin() noexcept;
60 iterator end() noexcept;
61
62 using const_iterator = const char *;
63 const_iterator begin() const noexcept;
64 const_iterator end() const noexcept;
65 const_iterator cbegin() const noexcept;
66 const_iterator cend() const noexcept;
67
David Tolnayff86dce2020-11-29 19:45:13 -080068 bool operator==(const String &) const noexcept;
69 bool operator!=(const String &) const noexcept;
70 bool operator<(const String &) const noexcept;
71 bool operator<=(const String &) const noexcept;
72 bool operator>(const String &) const noexcept;
73 bool operator>=(const String &) const noexcept;
74
David Tolnayd1e2efc2020-03-03 22:25:43 -080075 // Internal API only intended for the cxxbridge code generator.
76 String(unsafe_bitcopy_t, const String &) noexcept;
77
David Tolnay7db73692019-10-20 14:51:12 -040078private:
79 // Size and alignment statically verified by rust_string.rs.
David Tolnaybe3cbf72020-12-12 22:12:07 -080080 std::array<std::uintptr_t, 3> repr;
David Tolnay7db73692019-10-20 14:51:12 -040081};
David Tolnay0f0162f2020-11-16 23:43:37 -080082#endif // CXXBRIDGE1_RUST_STRING
David Tolnay7db73692019-10-20 14:51:12 -040083
David Tolnay0f0162f2020-11-16 23:43:37 -080084#ifndef CXXBRIDGE1_RUST_STR
David Tolnaye468f052020-11-29 19:39:35 -080085// https://cxx.rs/binding/str.html
David Tolnay09dbe752020-03-01 13:00:40 -080086class Str final {
David Tolnay7db73692019-10-20 14:51:12 -040087public:
David Tolnay09dbe752020-03-01 13:00:40 -080088 Str() noexcept;
David Tolnay828e5132020-11-29 20:40:40 -080089 Str(const String &) noexcept;
David Tolnay851677c2020-03-01 23:49:46 -080090 Str(const std::string &);
91 Str(const char *);
David Tolnaybe3cbf72020-12-12 22:12:07 -080092 Str(const char *, std::size_t);
David Tolnayd9c4ac92020-03-01 20:33:58 -080093
David Tolnay2d7f1172020-10-31 17:58:31 -070094 Str &operator=(const Str &) noexcept = default;
David Tolnayd9c4ac92020-03-01 20:33:58 -080095
David Tolnay404d6892020-03-01 20:19:41 -080096 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040097
98 // Note: no null terminator.
99 const char *data() const noexcept;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800100 std::size_t size() const noexcept;
101 std::size_t length() const noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400102
David Tolnayde9a5b12020-10-31 12:15:43 -0700103 // Important in order for System V ABI to pass in registers.
104 Str(const Str &) noexcept = default;
105 ~Str() noexcept = default;
106
David Tolnayff7f5fb2020-11-25 20:50:32 -0800107 using iterator = const char *;
108 using const_iterator = const char *;
109 const_iterator begin() const noexcept;
110 const_iterator end() const noexcept;
111 const_iterator cbegin() const noexcept;
112 const_iterator cend() const noexcept;
113
David Tolnayff86dce2020-11-29 19:45:13 -0800114 bool operator==(const Str &) const noexcept;
115 bool operator!=(const Str &) const noexcept;
116 bool operator<(const Str &) const noexcept;
117 bool operator<=(const Str &) const noexcept;
118 bool operator>(const Str &) const noexcept;
119 bool operator>=(const Str &) const noexcept;
120
David Tolnay5df1f062020-10-31 12:31:10 -0700121private:
David Tolnay0356d332020-10-31 19:46:41 -0700122 friend impl<Str>;
David Tolnay7db73692019-10-20 14:51:12 -0400123 // Not necessarily ABI compatible with &str. Codegen will translate to
124 // cxx::rust_str::RustStr which matches this layout.
David Tolnay5df1f062020-10-31 12:31:10 -0700125 const char *ptr;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800126 std::size_t len;
David Tolnay7db73692019-10-20 14:51:12 -0400127};
David Tolnay0f0162f2020-11-16 23:43:37 -0800128#endif // CXXBRIDGE1_RUST_STR
David Tolnay7db73692019-10-20 14:51:12 -0400129
David Tolnay0f0162f2020-11-16 23:43:37 -0800130#ifndef CXXBRIDGE1_RUST_SLICE
David Tolnayc5629f02020-11-23 18:32:46 -0800131namespace detail {
David Tolnayee9b9ee2020-11-25 08:28:50 -0800132template <bool>
David Tolnayc5629f02020-11-23 18:32:46 -0800133struct copy_assignable_if {};
David Tolnayce298232020-11-11 10:08:54 -0800134
David Tolnayc5629f02020-11-23 18:32:46 -0800135template <>
136struct copy_assignable_if<false> {
137 copy_assignable_if() noexcept = default;
138 copy_assignable_if(const copy_assignable_if &) noexcept = default;
139 copy_assignable_if &operator=(const copy_assignable_if &) noexcept = delete;
140 copy_assignable_if &operator=(copy_assignable_if &&) noexcept = default;
141};
142} // namespace detail
143
David Tolnaye468f052020-11-29 19:39:35 -0800144// https://cxx.rs/binding/slice.html
David Tolnayc5629f02020-11-23 18:32:46 -0800145template <typename T>
146class Slice final
147 : private detail::copy_assignable_if<std::is_const<T>::value> {
David Tolnayefe81052020-04-14 16:28:24 -0700148public:
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700149 Slice() noexcept;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800150 Slice(T *, std::size_t count) noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700151
David Tolnay2d7f1172020-10-31 17:58:31 -0700152 Slice &operator=(const Slice<T> &) noexcept = default;
David Tolnayc5629f02020-11-23 18:32:46 -0800153 Slice &operator=(Slice<T> &&) noexcept = default;
David Tolnayefe81052020-04-14 16:28:24 -0700154
David Tolnayce298232020-11-11 10:08:54 -0800155 T *data() const noexcept;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800156 std::size_t size() const noexcept;
157 std::size_t length() const noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700158
David Tolnayde9a5b12020-10-31 12:15:43 -0700159 // Important in order for System V ABI to pass in registers.
160 Slice(const Slice<T> &) noexcept = default;
161 ~Slice() noexcept = default;
162
David Tolnayac6cb542020-11-25 20:18:55 -0800163 class iterator;
164 iterator begin() const noexcept;
165 iterator end() const noexcept;
166
David Tolnayefe81052020-04-14 16:28:24 -0700167private:
David Tolnay36aa9e02020-10-31 23:08:21 -0700168 friend impl<Slice>;
169 // Not necessarily ABI compatible with &[T]. Codegen will translate to
David Tolnay5515a9e2020-11-25 19:07:54 -0800170 // cxx::rust_slice::RustSlice which matches this layout.
David Tolnayce298232020-11-11 10:08:54 -0800171 T *ptr;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800172 std::size_t len;
David Tolnayefe81052020-04-14 16:28:24 -0700173};
David Tolnayac6cb542020-11-25 20:18:55 -0800174
175template <typename T>
176class Slice<T>::iterator final {
177public:
David Tolnaybe3cbf72020-12-12 22:12:07 -0800178 using difference_type = std::ptrdiff_t;
David Tolnayac6cb542020-11-25 20:18:55 -0800179 using value_type = T;
180 using pointer = typename std::add_pointer<T>::type;
181 using reference = typename std::add_lvalue_reference<T>::type;
182 using iterator_category = std::forward_iterator_tag;
183
184 T &operator*() const noexcept;
185 T *operator->() const noexcept;
186 iterator &operator++() noexcept;
187 iterator operator++(int) noexcept;
188 bool operator==(const iterator &) const noexcept;
189 bool operator!=(const iterator &) const noexcept;
190
191private:
192 friend class Slice;
193 T *pos;
194};
David Tolnay0f0162f2020-11-16 23:43:37 -0800195#endif // CXXBRIDGE1_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -0700196
David Tolnay0f0162f2020-11-16 23:43:37 -0800197#ifndef CXXBRIDGE1_RUST_BOX
David Tolnaye468f052020-11-29 19:39:35 -0800198// https://cxx.rs/binding/box.html
David Tolnayf262d382020-04-11 22:12:40 -0700199template <typename T>
200class Box final {
David Tolnay7db73692019-10-20 14:51:12 -0400201public:
David Tolnayf6292372020-03-01 21:09:11 -0800202 using value_type = T;
David Tolnay9706a512020-04-24 17:09:01 -0700203 using const_pointer =
204 typename std::add_pointer<typename std::add_const<T>::type>::type;
205 using pointer = typename std::add_pointer<T>::type;
David Tolnayf6292372020-03-01 21:09:11 -0800206
David Tolnayc4b34222020-12-12 13:06:26 -0800207 Box() = delete;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700208 Box(const Box &);
209 Box(Box &&) noexcept;
210 ~Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400211
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700212 explicit Box(const T &);
213 explicit Box(T &&);
214
215 Box &operator=(const Box &);
216 Box &operator=(Box &&) noexcept;
217
218 const T *operator->() const noexcept;
219 const T &operator*() const noexcept;
220 T *operator->() noexcept;
221 T &operator*() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400222
David Tolnayf262d382020-04-11 22:12:40 -0700223 template <typename... Fields>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700224 static Box in_place(Fields &&...);
David Tolnay7ce59fc2020-04-11 11:46:33 -0700225
David Tolnay7db73692019-10-20 14:51:12 -0400226 // Important: requires that `raw` came from an into_raw call. Do not pass a
227 // pointer from `new` or any other source.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700228 static Box from_raw(T *) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400229
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700230 T *into_raw() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400231
232private:
David Tolnayc4b34222020-12-12 13:06:26 -0800233 class uninit;
David Tolnaye5703162020-12-12 16:26:35 -0800234 class allocation;
David Tolnayc4b34222020-12-12 13:06:26 -0800235 Box(uninit) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400236 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800237 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400238};
David Tolnay0f0162f2020-11-16 23:43:37 -0800239#endif // CXXBRIDGE1_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400240
David Tolnay0f0162f2020-11-16 23:43:37 -0800241#ifndef CXXBRIDGE1_RUST_VEC
David Tolnaye468f052020-11-29 19:39:35 -0800242// https://cxx.rs/binding/vec.html
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700243template <typename T>
244class Vec final {
245public:
David Tolnayc87c2152020-04-24 17:07:41 -0700246 using value_type = T;
247
David Tolnayf97c2d52020-04-25 16:37:48 -0700248 Vec() noexcept;
David Tolnayf799b372020-11-29 23:57:42 -0800249 Vec(std::initializer_list<T>);
David Tolnay9007e462020-12-11 13:59:08 -0800250 Vec(const Vec &);
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700251 Vec(Vec &&) noexcept;
252 ~Vec() noexcept;
David Tolnaycb800572020-04-24 20:30:43 -0700253
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700254 Vec &operator=(Vec &&) noexcept;
David Tolnaydd42c722020-12-11 14:05:26 -0800255 Vec &operator=(const Vec &);
David Tolnayf97c2d52020-04-25 16:37:48 -0700256
David Tolnaybe3cbf72020-12-12 22:12:07 -0800257 std::size_t size() const noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700258 bool empty() const noexcept;
David Tolnay219c0792020-04-24 20:31:37 -0700259 const T *data() const noexcept;
David Tolnayfb6b73c2020-11-10 14:32:16 -0800260 T *data() noexcept;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800261 std::size_t capacity() const noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700262
David Tolnaybe3cbf72020-12-12 22:12:07 -0800263 const T &operator[](std::size_t n) const noexcept;
264 const T &at(std::size_t n) const;
David Tolnayd4fff5d2020-12-21 14:04:09 -0800265 const T &front() const noexcept;
266 const T &back() const noexcept;
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700267
David Tolnaybe3cbf72020-12-12 22:12:07 -0800268 T &operator[](std::size_t n) noexcept;
269 T &at(std::size_t n);
David Tolnayd4fff5d2020-12-21 14:04:09 -0800270 T &front() noexcept;
271 T &back() noexcept;
David Tolnay908d5e52020-11-30 00:30:59 -0800272
David Tolnaybe3cbf72020-12-12 22:12:07 -0800273 void reserve(std::size_t new_cap);
David Tolnayfb6b73c2020-11-10 14:32:16 -0800274 void push_back(const T &value);
275 void push_back(T &&value);
David Tolnay4e8c49a2020-11-11 10:00:18 -0800276 template <typename... Args>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800277 void emplace_back(Args &&... args);
278
David Tolnay960b5112020-11-25 13:18:28 -0800279 class iterator;
280 iterator begin() noexcept;
281 iterator end() noexcept;
282
David Tolnaya5d72c62020-11-25 13:57:16 -0800283 using const_iterator = typename Vec<const T>::iterator;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700284 const_iterator begin() const noexcept;
285 const_iterator end() const noexcept;
David Tolnay960b5112020-11-25 13:18:28 -0800286 const_iterator cbegin() const noexcept;
287 const_iterator cend() const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700288
David Tolnay313b10e2020-04-25 16:30:51 -0700289 // Internal API only intended for the cxxbridge code generator.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700290 Vec(unsafe_bitcopy_t, const Vec &) noexcept;
David Tolnay313b10e2020-04-25 16:30:51 -0700291
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700292private:
David Tolnaybe3cbf72020-12-12 22:12:07 -0800293 static std::size_t stride() noexcept;
294 void reserve_total(std::size_t cap) noexcept;
295 void set_len(std::size_t len) noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700296 void drop() noexcept;
297
298 // Size and alignment statically verified by rust_vec.rs.
David Tolnaybe3cbf72020-12-12 22:12:07 -0800299 std::array<std::uintptr_t, 3> repr;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700300};
David Tolnay66f216c2020-11-25 13:21:00 -0800301
302template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800303class Vec<T>::iterator final {
304public:
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500305 using iterator_category = std::random_access_iterator_tag;
David Tolnay960b5112020-11-25 13:18:28 -0800306 using value_type = T;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800307 using difference_type = std::ptrdiff_t;
David Tolnay960b5112020-11-25 13:18:28 -0800308 using pointer = typename std::add_pointer<T>::type;
309 using reference = typename std::add_lvalue_reference<T>::type;
David Tolnay960b5112020-11-25 13:18:28 -0800310
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500311 reference operator*() const noexcept;
312 pointer operator->() const noexcept;
313 reference operator[](difference_type) const noexcept;
314
David Tolnay960b5112020-11-25 13:18:28 -0800315 iterator &operator++() noexcept;
316 iterator operator++(int) noexcept;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500317 iterator &operator--() noexcept;
318 iterator operator--(int) noexcept;
319
320 iterator &operator+=(difference_type) noexcept;
321 iterator &operator-=(difference_type) noexcept;
322 iterator operator+(difference_type) const noexcept;
323 iterator operator-(difference_type) const noexcept;
324 difference_type operator-(const iterator &) const noexcept;
325
David Tolnay960b5112020-11-25 13:18:28 -0800326 bool operator==(const iterator &) const noexcept;
327 bool operator!=(const iterator &) const noexcept;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500328 bool operator<(const iterator &) const noexcept;
329 bool operator>(const iterator &) const noexcept;
330 bool operator<=(const iterator &) const noexcept;
331 bool operator>=(const iterator &) const noexcept;
David Tolnay960b5112020-11-25 13:18:28 -0800332
333private:
334 friend class Vec;
David Tolnaya5d72c62020-11-25 13:57:16 -0800335 friend class Vec<typename std::remove_const<T>::type>;
David Tolnay960b5112020-11-25 13:18:28 -0800336 void *pos;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800337 std::size_t stride;
David Tolnay960b5112020-11-25 13:18:28 -0800338};
David Tolnay0f0162f2020-11-16 23:43:37 -0800339#endif // CXXBRIDGE1_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700340
David Tolnay0f0162f2020-11-16 23:43:37 -0800341#ifndef CXXBRIDGE1_RUST_FN
David Tolnaye468f052020-11-29 19:39:35 -0800342// https://cxx.rs/binding/fn.html
David Tolnayf031c322020-11-29 19:41:33 -0800343template <typename Signature>
David Tolnayf262d382020-04-11 22:12:40 -0700344class Fn;
David Tolnay75dca2e2020-03-25 20:17:52 -0700345
David Tolnayf031c322020-11-29 19:41:33 -0800346template <typename Ret, typename... Args>
347class Fn<Ret(Args...)> final {
David Tolnay75dca2e2020-03-25 20:17:52 -0700348public:
David Tolnayf031c322020-11-29 19:41:33 -0800349 Ret operator()(Args... args) const noexcept;
David Tolnay533d4582020-04-08 20:29:14 -0700350 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700351
352private:
David Tolnayf031c322020-11-29 19:41:33 -0800353 Ret (*trampoline)(Args..., void *fn) noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700354 void *fn;
355};
David Tolnay0f0162f2020-11-16 23:43:37 -0800356#endif // CXXBRIDGE1_RUST_FN
David Tolnay75dca2e2020-03-25 20:17:52 -0700357
David Tolnay0f0162f2020-11-16 23:43:37 -0800358#ifndef CXXBRIDGE1_RUST_ERROR
359#define CXXBRIDGE1_RUST_ERROR
David Tolnaye468f052020-11-29 19:39:35 -0800360// https://cxx.rs/binding/result.html
David Tolnaye4fa8732020-09-08 15:04:56 -0700361class Error final : public std::exception {
David Tolnay1e548172020-03-16 13:37:09 -0700362public:
363 Error(const Error &);
364 Error(Error &&) noexcept;
David Tolnay2714d2c2020-11-23 18:17:43 -0800365 ~Error() noexcept override;
David Tolnay7c6ac712020-10-31 17:22:28 -0700366
367 Error &operator=(const Error &);
David Tolnay15491062020-10-31 17:25:13 -0700368 Error &operator=(Error &&) noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700369
David Tolnay1e548172020-03-16 13:37:09 -0700370 const char *what() const noexcept override;
371
372private:
David Tolnaya0c9bc72020-10-31 14:37:14 -0700373 Error() noexcept = default;
David Tolnay84ddf9e2020-10-31 15:36:48 -0700374 friend impl<Error>;
David Tolnaya0c9bc72020-10-31 14:37:14 -0700375 const char *msg;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800376 std::size_t len;
David Tolnay1e548172020-03-16 13:37:09 -0700377};
David Tolnay0f0162f2020-11-16 23:43:37 -0800378#endif // CXXBRIDGE1_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700379
David Tolnay0f0162f2020-11-16 23:43:37 -0800380#ifndef CXXBRIDGE1_RUST_ISIZE
381#define CXXBRIDGE1_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700382#if defined(_WIN32)
383using isize = SSIZE_T;
384#else
385using isize = ssize_t;
386#endif
David Tolnay0f0162f2020-11-16 23:43:37 -0800387#endif // CXXBRIDGE1_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700388
David Tolnay851677c2020-03-01 23:49:46 -0800389std::ostream &operator<<(std::ostream &, const String &);
390std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400391
David Tolnay365fc7c2020-11-25 16:08:13 -0800392#ifndef CXXBRIDGE1_RUST_OPAQUE
393#define CXXBRIDGE1_RUST_OPAQUE
394// Base class of generated opaque Rust types.
395class Opaque {
David Tolnaya857c322020-11-25 16:27:19 -0800396public:
David Tolnay365fc7c2020-11-25 16:08:13 -0800397 Opaque() = delete;
398 Opaque(const Opaque &) = delete;
399 ~Opaque() = delete;
400};
401#endif // CXXBRIDGE1_RUST_OPAQUE
402
David Tolnay174bd952020-11-02 09:23:12 -0800403// IsRelocatable<T> is used in assertions that a C++ type passed by value
404// between Rust and C++ is soundly relocatable by Rust.
405//
406// There may be legitimate reasons to opt out of the check for support of types
407// that the programmer knows are soundly Rust-movable despite not being
408// recognized as such by the C++ type system due to a move constructor or
409// destructor. To opt out of the relocatability check, do either of the
410// following things in any header used by `include!` in the bridge.
411//
412// --- if you define the type:
413// struct MyType {
414// ...
415// + using IsRelocatable = std::true_type;
416// };
417//
418// --- otherwise:
419// + template <>
420// + struct rust::IsRelocatable<MyType> : std::true_type {};
421template <typename T>
422struct IsRelocatable;
423
David Tolnaybe3cbf72020-12-12 22:12:07 -0800424using u8 = std::uint8_t;
425using u16 = std::uint16_t;
426using u32 = std::uint32_t;
427using u64 = std::uint64_t;
428using usize = std::size_t; // see static asserts in cxx.cc
429using i8 = std::int8_t;
430using i16 = std::int16_t;
431using i32 = std::int32_t;
432using i64 = std::int64_t;
David Tolnay1e784a32020-12-12 21:34:47 -0800433using f32 = float;
434using f64 = double;
435
David Tolnay3b0c9882020-03-01 14:08:57 -0800436// Snake case aliases for use in code that uses this style for type names.
437using string = String;
438using str = Str;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800439template <typename T>
David Tolnay38c87642020-09-06 22:18:08 -0700440using slice = Slice<T>;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800441template <typename T>
David Tolnayf262d382020-04-11 22:12:40 -0700442using box = Box<T>;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800443template <typename T>
David Tolnay38c87642020-09-06 22:18:08 -0700444using vec = Vec<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700445using error = Error;
David Tolnayf262d382020-04-11 22:12:40 -0700446template <typename Signature>
David Tolnayf031c322020-11-29 19:41:33 -0800447using fn = Fn<Signature>;
David Tolnay174bd952020-11-02 09:23:12 -0800448template <typename T>
449using is_relocatable = IsRelocatable<T>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800450
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700451
452
453////////////////////////////////////////////////////////////////////////////////
454/// end public API, begin implementation details
455
David Tolnay0f0162f2020-11-16 23:43:37 -0800456#ifndef CXXBRIDGE1_PANIC
457#define CXXBRIDGE1_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700458template <typename Exception>
459void panic [[noreturn]] (const char *msg);
David Tolnay0f0162f2020-11-16 23:43:37 -0800460#endif // CXXBRIDGE1_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700461
David Tolnay0f0162f2020-11-16 23:43:37 -0800462#ifndef CXXBRIDGE1_RUST_FN
463#define CXXBRIDGE1_RUST_FN
David Tolnayf031c322020-11-29 19:41:33 -0800464template <typename Ret, typename... Args>
465Ret Fn<Ret(Args...)>::operator()(Args... args) const noexcept {
David Tolnay75dca2e2020-03-25 20:17:52 -0700466 return (*this->trampoline)(std::move(args)..., this->fn);
467}
468
David Tolnayf031c322020-11-29 19:41:33 -0800469template <typename Ret, typename... Args>
470Fn<Ret(Args...)> Fn<Ret(Args...)>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700471 return *this;
472}
David Tolnay0f0162f2020-11-16 23:43:37 -0800473#endif // CXXBRIDGE1_RUST_FN
David Tolnaya23129c2020-04-08 20:08:21 -0700474
David Tolnay0f0162f2020-11-16 23:43:37 -0800475#ifndef CXXBRIDGE1_RUST_BITCOPY
476#define CXXBRIDGE1_RUST_BITCOPY
David Tolnay48521222020-10-31 14:59:42 -0700477struct unsafe_bitcopy_t final {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700478 explicit unsafe_bitcopy_t() = default;
479};
480
481constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnay0f0162f2020-11-16 23:43:37 -0800482#endif // CXXBRIDGE1_RUST_BITCOPY
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700483
David Tolnay0f0162f2020-11-16 23:43:37 -0800484#ifndef CXXBRIDGE1_RUST_STR
485#define CXXBRIDGE1_RUST_STR
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700486inline const char *Str::data() const noexcept { return this->ptr; }
487
David Tolnaybe3cbf72020-12-12 22:12:07 -0800488inline std::size_t Str::size() const noexcept { return this->len; }
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700489
David Tolnaybe3cbf72020-12-12 22:12:07 -0800490inline std::size_t Str::length() const noexcept { return this->len; }
David Tolnay0f0162f2020-11-16 23:43:37 -0800491#endif // CXXBRIDGE1_RUST_STR
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700492
David Tolnay0f0162f2020-11-16 23:43:37 -0800493#ifndef CXXBRIDGE1_RUST_SLICE
494#define CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700495template <typename T>
David Tolnay7b16a392020-11-25 20:23:10 -0800496Slice<T>::Slice() noexcept : ptr(reinterpret_cast<T *>(alignof(T))), len(0) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700497
498template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800499Slice<T>::Slice(T *s, std::size_t count) noexcept : ptr(s), len(count) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700500
501template <typename T>
David Tolnayce298232020-11-11 10:08:54 -0800502T *Slice<T>::data() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700503 return this->ptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700504}
505
506template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800507std::size_t Slice<T>::size() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700508 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700509}
510
511template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800512std::size_t Slice<T>::length() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700513 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700514}
David Tolnayac6cb542020-11-25 20:18:55 -0800515
516template <typename T>
517T &Slice<T>::iterator::operator*() const noexcept {
518 return *this->pos;
519}
520
521template <typename T>
522T *Slice<T>::iterator::operator->() const noexcept {
523 return this->pos;
524}
525
526template <typename T>
527typename Slice<T>::iterator &Slice<T>::iterator::operator++() noexcept {
528 ++this->pos;
529 return *this;
530}
531
532template <typename T>
533typename Slice<T>::iterator Slice<T>::iterator::operator++(int) noexcept {
534 auto ret = iterator(*this);
535 ++this->pos;
536 return ret;
537}
538
539template <typename T>
540bool Slice<T>::iterator::operator==(const iterator &other) const noexcept {
541 return this->pos == other.pos;
542}
543
544template <typename T>
545bool Slice<T>::iterator::operator!=(const iterator &other) const noexcept {
546 return this->pos != other.pos;
547}
548
549template <typename T>
550typename Slice<T>::iterator Slice<T>::begin() const noexcept {
551 iterator it;
552 it.pos = this->ptr;
553 return it;
554}
555
556template <typename T>
557typename Slice<T>::iterator Slice<T>::end() const noexcept {
558 iterator it = this->begin();
559 it.pos += this->len;
560 return it;
561}
David Tolnay0f0162f2020-11-16 23:43:37 -0800562#endif // CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700563
David Tolnay0f0162f2020-11-16 23:43:37 -0800564#ifndef CXXBRIDGE1_RUST_BOX
565#define CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700566template <typename T>
David Tolnayc4b34222020-12-12 13:06:26 -0800567class Box<T>::uninit {};
568
569template <typename T>
David Tolnaye5703162020-12-12 16:26:35 -0800570class Box<T>::allocation {
David Tolnayf448e202020-12-12 16:42:45 -0800571 static T *alloc() noexcept;
572 static void dealloc(T *) noexcept;
David Tolnay2e637d92020-12-12 21:08:01 -0800573
David Tolnaye5703162020-12-12 16:26:35 -0800574public:
David Tolnaye7d662d2020-12-12 16:38:22 -0800575 allocation() noexcept : ptr(alloc()) {}
576 ~allocation() noexcept {
577 if (this->ptr) {
578 dealloc(this->ptr);
579 }
580 }
David Tolnaye5703162020-12-12 16:26:35 -0800581 T *ptr;
582};
583
584template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700585Box<T>::Box(const Box &other) : Box(*other) {}
586
587template <typename T>
588Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
589 other.ptr = nullptr;
590}
591
592template <typename T>
David Tolnaye7d662d2020-12-12 16:38:22 -0800593Box<T>::Box(const T &val) {
594 allocation alloc;
595 ::new (alloc.ptr) T(val);
596 this->ptr = alloc.ptr;
597 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700598}
599
600template <typename T>
David Tolnaye7d662d2020-12-12 16:38:22 -0800601Box<T>::Box(T &&val) {
602 allocation alloc;
603 ::new (alloc.ptr) T(std::move(val));
604 this->ptr = alloc.ptr;
605 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700606}
607
608template <typename T>
609Box<T>::~Box() noexcept {
610 if (this->ptr) {
611 this->drop();
612 }
613}
614
615template <typename T>
616Box<T> &Box<T>::operator=(const Box &other) {
David Tolnay439ed0b2020-12-12 17:01:04 -0800617 if (this->ptr) {
618 **this = *other;
619 } else {
620 allocation alloc;
621 ::new (alloc.ptr) T(*other);
622 this->ptr = alloc.ptr;
623 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700624 }
625 return *this;
626}
627
628template <typename T>
629Box<T> &Box<T>::operator=(Box &&other) noexcept {
630 if (this->ptr) {
631 this->drop();
632 }
633 this->ptr = other.ptr;
634 other.ptr = nullptr;
635 return *this;
636}
637
638template <typename T>
639const T *Box<T>::operator->() const noexcept {
640 return this->ptr;
641}
642
643template <typename T>
644const T &Box<T>::operator*() const noexcept {
645 return *this->ptr;
646}
647
648template <typename T>
649T *Box<T>::operator->() noexcept {
650 return this->ptr;
651}
652
653template <typename T>
654T &Box<T>::operator*() noexcept {
655 return *this->ptr;
656}
657
658template <typename T>
659template <typename... Fields>
660Box<T> Box<T>::in_place(Fields &&... fields) {
David Tolnaye7d662d2020-12-12 16:38:22 -0800661 allocation alloc;
662 auto ptr = alloc.ptr;
663 ::new (ptr) T{std::forward<Fields>(fields)...};
664 alloc.ptr = nullptr;
665 return from_raw(ptr);
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700666}
667
668template <typename T>
669Box<T> Box<T>::from_raw(T *raw) noexcept {
David Tolnayc4b34222020-12-12 13:06:26 -0800670 Box box = uninit{};
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700671 box.ptr = raw;
672 return box;
673}
674
675template <typename T>
676T *Box<T>::into_raw() noexcept {
677 T *raw = this->ptr;
678 this->ptr = nullptr;
679 return raw;
680}
681
682template <typename T>
David Tolnayc4b34222020-12-12 13:06:26 -0800683Box<T>::Box(uninit) noexcept {}
David Tolnay0f0162f2020-11-16 23:43:37 -0800684#endif // CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700685
David Tolnay0f0162f2020-11-16 23:43:37 -0800686#ifndef CXXBRIDGE1_RUST_VEC
687#define CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700688template <typename T>
David Tolnayf799b372020-11-29 23:57:42 -0800689Vec<T>::Vec(std::initializer_list<T> init) : Vec{} {
690 this->reserve_total(init.size());
691 std::move(init.begin(), init.end(), std::back_inserter(*this));
692}
693
694template <typename T>
David Tolnay9007e462020-12-11 13:59:08 -0800695Vec<T>::Vec(const Vec &other) : Vec() {
696 this->reserve_total(other.size());
697 std::copy(other.begin(), other.end(), std::back_inserter(*this));
698}
699
700template <typename T>
David Tolnay15671862020-11-23 18:13:56 -0800701Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700702 new (&other) Vec();
703}
704
705template <typename T>
706Vec<T>::~Vec() noexcept {
707 this->drop();
708}
709
710template <typename T>
711Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
712 if (this != &other) {
713 this->drop();
714 this->repr = other.repr;
715 new (&other) Vec();
716 }
717 return *this;
718}
719
720template <typename T>
David Tolnaydd42c722020-12-11 14:05:26 -0800721Vec<T> &Vec<T>::operator=(const Vec &other) {
722 if (this != &other) {
723 this->drop();
724 new (this) Vec(other);
725 }
726 return *this;
727}
728
729template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700730bool Vec<T>::empty() const noexcept {
731 return size() == 0;
732}
733
734template <typename T>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800735T *Vec<T>::data() noexcept {
736 return const_cast<T *>(const_cast<const Vec<T> *>(this)->data());
737}
738
739template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800740const T &Vec<T>::operator[](std::size_t n) const noexcept {
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700741 auto data = reinterpret_cast<const char *>(this->data());
742 return *reinterpret_cast<const T *>(data + n * this->stride());
743}
744
745template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800746const T &Vec<T>::at(std::size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700747 if (n >= this->size()) {
748 panic<std::out_of_range>("rust::Vec index out of range");
749 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700750 return (*this)[n];
751}
752
753template <typename T>
David Tolnayd4fff5d2020-12-21 14:04:09 -0800754const T &Vec<T>::front() const noexcept {
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700755 return (*this)[0];
756}
757
758template <typename T>
David Tolnayd4fff5d2020-12-21 14:04:09 -0800759const T &Vec<T>::back() const noexcept {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700760 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700761}
762
763template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800764T &Vec<T>::operator[](std::size_t n) noexcept {
David Tolnay908d5e52020-11-30 00:30:59 -0800765 auto data = reinterpret_cast<char *>(this->data());
766 return *reinterpret_cast<T *>(data + n * this->stride());
767}
768
769template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800770T &Vec<T>::at(std::size_t n) {
David Tolnay908d5e52020-11-30 00:30:59 -0800771 if (n >= this->size()) {
772 panic<std::out_of_range>("rust::Vec index out of range");
773 }
774 return (*this)[n];
775}
776
777template <typename T>
David Tolnayd4fff5d2020-12-21 14:04:09 -0800778T &Vec<T>::front() noexcept {
David Tolnay908d5e52020-11-30 00:30:59 -0800779 return (*this)[0];
780}
781
782template <typename T>
David Tolnayd4fff5d2020-12-21 14:04:09 -0800783T &Vec<T>::back() noexcept {
David Tolnay908d5e52020-11-30 00:30:59 -0800784 return (*this)[this->size() - 1];
785}
786
787template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800788void Vec<T>::reserve(std::size_t new_cap) {
David Tolnayfb6b73c2020-11-10 14:32:16 -0800789 this->reserve_total(new_cap);
790}
791
792template <typename T>
793void Vec<T>::push_back(const T &value) {
794 this->emplace_back(value);
795}
796
797template <typename T>
798void Vec<T>::push_back(T &&value) {
799 this->emplace_back(std::move(value));
800}
801
802template <typename T>
803template <typename... Args>
804void Vec<T>::emplace_back(Args &&... args) {
805 auto size = this->size();
806 this->reserve_total(size + 1);
807 ::new (reinterpret_cast<T *>(reinterpret_cast<char *>(this->data()) +
808 size * this->stride()))
809 T(std::forward<Args>(args)...);
810 this->set_len(size + 1);
811}
812
813template <typename T>
David Tolnay300072b2020-12-03 12:12:24 -0800814typename Vec<T>::iterator::reference
815Vec<T>::iterator::operator*() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800816 return *static_cast<T *>(this->pos);
817}
818
819template <typename T>
David Tolnay300072b2020-12-03 12:12:24 -0800820typename Vec<T>::iterator::pointer
821Vec<T>::iterator::operator->() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800822 return static_cast<T *>(this->pos);
823}
824
825template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500826typename Vec<T>::iterator::reference Vec<T>::iterator::operator[](
David Tolnay665178e2020-12-12 08:49:59 -0800827 typename Vec<T>::iterator::difference_type n) const noexcept {
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500828 auto pos = static_cast<char *>(this->pos) + this->stride * n;
829 return *static_cast<T *>(pos);
830}
831
832template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800833typename Vec<T>::iterator &Vec<T>::iterator::operator++() noexcept {
David Tolnay248215e2020-11-25 19:38:26 -0800834 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnay960b5112020-11-25 13:18:28 -0800835 return *this;
836}
837
838template <typename T>
839typename Vec<T>::iterator Vec<T>::iterator::operator++(int) noexcept {
840 auto ret = iterator(*this);
David Tolnay248215e2020-11-25 19:38:26 -0800841 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnay960b5112020-11-25 13:18:28 -0800842 return ret;
843}
844
845template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500846typename Vec<T>::iterator &Vec<T>::iterator::operator--() noexcept {
847 this->pos = static_cast<char *>(this->pos) - this->stride;
848 return *this;
849}
850
851template <typename T>
852typename Vec<T>::iterator Vec<T>::iterator::operator--(int) noexcept {
853 auto ret = iterator(*this);
854 this->pos = static_cast<char *>(this->pos) - this->stride;
855 return ret;
856}
857
858template <typename T>
David Tolnay665178e2020-12-12 08:49:59 -0800859typename Vec<T>::iterator &Vec<T>::iterator::operator+=(
860 typename Vec<T>::iterator::difference_type n) noexcept {
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500861 this->pos = static_cast<char *>(this->pos) + this->stride * n;
862 return *this;
863}
864
865template <typename T>
David Tolnay665178e2020-12-12 08:49:59 -0800866typename Vec<T>::iterator &Vec<T>::iterator::operator-=(
867 typename Vec<T>::iterator::difference_type n) noexcept {
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500868 this->pos = static_cast<char *>(this->pos) - this->stride * n;
869 return *this;
870}
871
872template <typename T>
873typename Vec<T>::iterator Vec<T>::iterator::operator+(
David Tolnay665178e2020-12-12 08:49:59 -0800874 typename Vec<T>::iterator::difference_type n) const noexcept {
David Tolnay300072b2020-12-03 12:12:24 -0800875 auto ret = iterator(*this);
876 ret.pos = static_cast<char *>(this->pos) + this->stride * n;
877 return ret;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500878}
879
880template <typename T>
881typename Vec<T>::iterator Vec<T>::iterator::operator-(
David Tolnay665178e2020-12-12 08:49:59 -0800882 typename Vec<T>::iterator::difference_type n) const noexcept {
David Tolnay300072b2020-12-03 12:12:24 -0800883 auto ret = iterator(*this);
884 ret.pos = static_cast<char *>(this->pos) - this->stride * n;
885 return ret;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500886}
887
888template <typename T>
889typename Vec<T>::iterator::difference_type
890Vec<T>::iterator::operator-(const iterator &other) const noexcept {
891 auto diff = std::distance(static_cast<char *>(other.pos),
892 static_cast<char *>(this->pos));
David Tolnay300072b2020-12-03 12:12:24 -0800893 return diff / this->stride;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500894}
895
896template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800897bool Vec<T>::iterator::operator==(const iterator &other) const noexcept {
898 return this->pos == other.pos;
899}
900
901template <typename T>
902bool Vec<T>::iterator::operator!=(const iterator &other) const noexcept {
903 return this->pos != other.pos;
904}
905
906template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500907bool Vec<T>::iterator::operator>(const iterator &other) const noexcept {
908 return this->pos > other.pos;
909}
910
911template <typename T>
912bool Vec<T>::iterator::operator<(const iterator &other) const noexcept {
913 return this->pos < other.pos;
914}
915
916template <typename T>
917bool Vec<T>::iterator::operator>=(const iterator &other) const noexcept {
918 return this->pos >= other.pos;
919}
920
921template <typename T>
922bool Vec<T>::iterator::operator<=(const iterator &other) const noexcept {
923 return this->pos <= other.pos;
924}
925
926template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800927typename Vec<T>::iterator Vec<T>::begin() noexcept {
928 iterator it;
David Tolnaya5d72c62020-11-25 13:57:16 -0800929 it.pos = const_cast<typename std::remove_const<T>::type *>(this->data());
David Tolnay960b5112020-11-25 13:18:28 -0800930 it.stride = this->stride();
931 return it;
932}
933
934template <typename T>
935typename Vec<T>::iterator Vec<T>::end() noexcept {
936 iterator it = this->begin();
David Tolnay248215e2020-11-25 19:38:26 -0800937 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
David Tolnay960b5112020-11-25 13:18:28 -0800938 return it;
939}
940
941template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700942typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800943 return this->cbegin();
944}
945
946template <typename T>
947typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
948 return this->cend();
949}
950
951template <typename T>
952typename Vec<T>::const_iterator Vec<T>::cbegin() const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700953 const_iterator it;
David Tolnaya5d72c62020-11-25 13:57:16 -0800954 it.pos = const_cast<typename std::remove_const<T>::type *>(this->data());
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700955 it.stride = this->stride();
956 return it;
957}
958
959template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800960typename Vec<T>::const_iterator Vec<T>::cend() const noexcept {
961 const_iterator it = this->cbegin();
David Tolnay248215e2020-11-25 19:38:26 -0800962 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700963 return it;
964}
965
966// Internal API only intended for the cxxbridge code generator.
967template <typename T>
968Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay0f0162f2020-11-16 23:43:37 -0800969#endif // CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700970
David Tolnay0f0162f2020-11-16 23:43:37 -0800971#ifndef CXXBRIDGE1_RELOCATABLE
972#define CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -0800973namespace detail {
974template <typename... Ts>
975struct make_void {
976 using type = void;
977};
978
979template <typename... Ts>
980using void_t = typename make_void<Ts...>::type;
981
982template <typename Void, template <typename...> class, typename...>
983struct detect : std::false_type {};
984template <template <typename...> class T, typename... A>
985struct detect<void_t<T<A...>>, T, A...> : std::true_type {};
986
987template <template <typename...> class T, typename... A>
988using is_detected = detect<void, T, A...>;
989
990template <typename T>
991using detect_IsRelocatable = typename T::IsRelocatable;
992
993template <typename T>
994struct get_IsRelocatable
995 : std::is_same<typename T::IsRelocatable, std::true_type> {};
996} // namespace detail
997
998template <typename T>
999struct IsRelocatable
1000 : std::conditional<
1001 detail::is_detected<detail::detect_IsRelocatable, T>::value,
1002 detail::get_IsRelocatable<T>,
1003 std::integral_constant<
1004 bool, std::is_trivially_move_constructible<T>::value &&
1005 std::is_trivially_destructible<T>::value>>::type {};
David Tolnay0f0162f2020-11-16 23:43:37 -08001006#endif // CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -08001007
David Tolnay0f0162f2020-11-16 23:43:37 -08001008} // namespace cxxbridge1
David Tolnay750755e2020-03-01 13:04:08 -08001009} // namespace rust