blob: 956c46e5d010b862b5301e39c76aa62fba744cd5 [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 Tolnayc4b34222020-12-12 13:06:26 -0800203 Box() = delete;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700204 Box(const Box &);
205 Box(Box &&) noexcept;
206 ~Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400207
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700208 explicit Box(const T &);
209 explicit Box(T &&);
210
211 Box &operator=(const Box &);
212 Box &operator=(Box &&) noexcept;
213
214 const T *operator->() const noexcept;
215 const T &operator*() const noexcept;
216 T *operator->() noexcept;
217 T &operator*() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400218
David Tolnayf262d382020-04-11 22:12:40 -0700219 template <typename... Fields>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700220 static Box in_place(Fields &&...);
David Tolnay7ce59fc2020-04-11 11:46:33 -0700221
David Tolnay7db73692019-10-20 14:51:12 -0400222 // Important: requires that `raw` came from an into_raw call. Do not pass a
223 // pointer from `new` or any other source.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700224 static Box from_raw(T *) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400225
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700226 T *into_raw() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400227
228private:
David Tolnayc4b34222020-12-12 13:06:26 -0800229 class uninit;
David Tolnaye5703162020-12-12 16:26:35 -0800230 class allocation;
David Tolnayc4b34222020-12-12 13:06:26 -0800231 Box(uninit) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400232 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800233 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400234};
David Tolnay0f0162f2020-11-16 23:43:37 -0800235#endif // CXXBRIDGE1_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400236
David Tolnay0f0162f2020-11-16 23:43:37 -0800237#ifndef CXXBRIDGE1_RUST_VEC
David Tolnaye468f052020-11-29 19:39:35 -0800238// https://cxx.rs/binding/vec.html
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700239template <typename T>
240class Vec final {
241public:
David Tolnayc87c2152020-04-24 17:07:41 -0700242 using value_type = T;
243
David Tolnayf97c2d52020-04-25 16:37:48 -0700244 Vec() noexcept;
David Tolnayf799b372020-11-29 23:57:42 -0800245 Vec(std::initializer_list<T>);
David Tolnay9007e462020-12-11 13:59:08 -0800246 Vec(const Vec &);
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700247 Vec(Vec &&) noexcept;
248 ~Vec() noexcept;
David Tolnaycb800572020-04-24 20:30:43 -0700249
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700250 Vec &operator=(Vec &&) noexcept;
David Tolnaydd42c722020-12-11 14:05:26 -0800251 Vec &operator=(const Vec &);
David Tolnayf97c2d52020-04-25 16:37:48 -0700252
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700253 size_t size() const noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700254 bool empty() const noexcept;
David Tolnay219c0792020-04-24 20:31:37 -0700255 const T *data() const noexcept;
David Tolnayfb6b73c2020-11-10 14:32:16 -0800256 T *data() noexcept;
David Tolnaydc62d712020-12-11 13:51:53 -0800257 size_t capacity() const noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700258
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700259 const T &operator[](size_t n) const noexcept;
260 const T &at(size_t n) const;
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700261 const T &front() const;
262 const T &back() const;
263
David Tolnay908d5e52020-11-30 00:30:59 -0800264 T &operator[](size_t n) noexcept;
265 T &at(size_t n);
266 T &front();
267 T &back();
268
David Tolnayfb6b73c2020-11-10 14:32:16 -0800269 void reserve(size_t new_cap);
270 void push_back(const T &value);
271 void push_back(T &&value);
David Tolnay4e8c49a2020-11-11 10:00:18 -0800272 template <typename... Args>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800273 void emplace_back(Args &&... args);
274
David Tolnay960b5112020-11-25 13:18:28 -0800275 class iterator;
276 iterator begin() noexcept;
277 iterator end() noexcept;
278
David Tolnaya5d72c62020-11-25 13:57:16 -0800279 using const_iterator = typename Vec<const T>::iterator;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700280 const_iterator begin() const noexcept;
281 const_iterator end() const noexcept;
David Tolnay960b5112020-11-25 13:18:28 -0800282 const_iterator cbegin() const noexcept;
283 const_iterator cend() const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700284
David Tolnay313b10e2020-04-25 16:30:51 -0700285 // Internal API only intended for the cxxbridge code generator.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700286 Vec(unsafe_bitcopy_t, const Vec &) noexcept;
David Tolnay313b10e2020-04-25 16:30:51 -0700287
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700288private:
David Tolnay503d0192020-04-24 22:18:56 -0700289 static size_t stride() noexcept;
David Tolnayfb6b73c2020-11-10 14:32:16 -0800290 void reserve_total(size_t cap) noexcept;
291 void set_len(size_t len) noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700292 void drop() noexcept;
293
294 // Size and alignment statically verified by rust_vec.rs.
295 std::array<uintptr_t, 3> repr;
296};
David Tolnay66f216c2020-11-25 13:21:00 -0800297
298template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800299class Vec<T>::iterator final {
300public:
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500301 using iterator_category = std::random_access_iterator_tag;
David Tolnay960b5112020-11-25 13:18:28 -0800302 using value_type = T;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500303 using difference_type = ptrdiff_t;
David Tolnay960b5112020-11-25 13:18:28 -0800304 using pointer = typename std::add_pointer<T>::type;
305 using reference = typename std::add_lvalue_reference<T>::type;
David Tolnay960b5112020-11-25 13:18:28 -0800306
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500307 reference operator*() const noexcept;
308 pointer operator->() const noexcept;
309 reference operator[](difference_type) const noexcept;
310
David Tolnay960b5112020-11-25 13:18:28 -0800311 iterator &operator++() noexcept;
312 iterator operator++(int) noexcept;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500313 iterator &operator--() noexcept;
314 iterator operator--(int) noexcept;
315
316 iterator &operator+=(difference_type) noexcept;
317 iterator &operator-=(difference_type) noexcept;
318 iterator operator+(difference_type) const noexcept;
319 iterator operator-(difference_type) const noexcept;
320 difference_type operator-(const iterator &) const noexcept;
321
David Tolnay960b5112020-11-25 13:18:28 -0800322 bool operator==(const iterator &) const noexcept;
323 bool operator!=(const iterator &) const noexcept;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500324 bool operator<(const iterator &) const noexcept;
325 bool operator>(const iterator &) const noexcept;
326 bool operator<=(const iterator &) const noexcept;
327 bool operator>=(const iterator &) const noexcept;
David Tolnay960b5112020-11-25 13:18:28 -0800328
329private:
330 friend class Vec;
David Tolnaya5d72c62020-11-25 13:57:16 -0800331 friend class Vec<typename std::remove_const<T>::type>;
David Tolnay960b5112020-11-25 13:18:28 -0800332 void *pos;
333 size_t stride;
334};
David Tolnay0f0162f2020-11-16 23:43:37 -0800335#endif // CXXBRIDGE1_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700336
David Tolnay0f0162f2020-11-16 23:43:37 -0800337#ifndef CXXBRIDGE1_RUST_FN
David Tolnaye468f052020-11-29 19:39:35 -0800338// https://cxx.rs/binding/fn.html
David Tolnayf031c322020-11-29 19:41:33 -0800339template <typename Signature>
David Tolnayf262d382020-04-11 22:12:40 -0700340class Fn;
David Tolnay75dca2e2020-03-25 20:17:52 -0700341
David Tolnayf031c322020-11-29 19:41:33 -0800342template <typename Ret, typename... Args>
343class Fn<Ret(Args...)> final {
David Tolnay75dca2e2020-03-25 20:17:52 -0700344public:
David Tolnayf031c322020-11-29 19:41:33 -0800345 Ret operator()(Args... args) const noexcept;
David Tolnay533d4582020-04-08 20:29:14 -0700346 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700347
348private:
David Tolnayf031c322020-11-29 19:41:33 -0800349 Ret (*trampoline)(Args..., void *fn) noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700350 void *fn;
351};
David Tolnay0f0162f2020-11-16 23:43:37 -0800352#endif // CXXBRIDGE1_RUST_FN
David Tolnay75dca2e2020-03-25 20:17:52 -0700353
David Tolnay0f0162f2020-11-16 23:43:37 -0800354#ifndef CXXBRIDGE1_RUST_ERROR
355#define CXXBRIDGE1_RUST_ERROR
David Tolnaye468f052020-11-29 19:39:35 -0800356// https://cxx.rs/binding/result.html
David Tolnaye4fa8732020-09-08 15:04:56 -0700357class Error final : public std::exception {
David Tolnay1e548172020-03-16 13:37:09 -0700358public:
359 Error(const Error &);
360 Error(Error &&) noexcept;
David Tolnay2714d2c2020-11-23 18:17:43 -0800361 ~Error() noexcept override;
David Tolnay7c6ac712020-10-31 17:22:28 -0700362
363 Error &operator=(const Error &);
David Tolnay15491062020-10-31 17:25:13 -0700364 Error &operator=(Error &&) noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700365
David Tolnay1e548172020-03-16 13:37:09 -0700366 const char *what() const noexcept override;
367
368private:
David Tolnaya0c9bc72020-10-31 14:37:14 -0700369 Error() noexcept = default;
David Tolnay84ddf9e2020-10-31 15:36:48 -0700370 friend impl<Error>;
David Tolnaya0c9bc72020-10-31 14:37:14 -0700371 const char *msg;
372 size_t len;
David Tolnay1e548172020-03-16 13:37:09 -0700373};
David Tolnay0f0162f2020-11-16 23:43:37 -0800374#endif // CXXBRIDGE1_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700375
David Tolnay0f0162f2020-11-16 23:43:37 -0800376#ifndef CXXBRIDGE1_RUST_ISIZE
377#define CXXBRIDGE1_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700378#if defined(_WIN32)
379using isize = SSIZE_T;
380#else
381using isize = ssize_t;
382#endif
David Tolnay0f0162f2020-11-16 23:43:37 -0800383#endif // CXXBRIDGE1_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700384
David Tolnay851677c2020-03-01 23:49:46 -0800385std::ostream &operator<<(std::ostream &, const String &);
386std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400387
David Tolnay365fc7c2020-11-25 16:08:13 -0800388#ifndef CXXBRIDGE1_RUST_OPAQUE
389#define CXXBRIDGE1_RUST_OPAQUE
390// Base class of generated opaque Rust types.
391class Opaque {
David Tolnaya857c322020-11-25 16:27:19 -0800392public:
David Tolnay365fc7c2020-11-25 16:08:13 -0800393 Opaque() = delete;
394 Opaque(const Opaque &) = delete;
395 ~Opaque() = delete;
396};
397#endif // CXXBRIDGE1_RUST_OPAQUE
398
David Tolnay174bd952020-11-02 09:23:12 -0800399// IsRelocatable<T> is used in assertions that a C++ type passed by value
400// between Rust and C++ is soundly relocatable by Rust.
401//
402// There may be legitimate reasons to opt out of the check for support of types
403// that the programmer knows are soundly Rust-movable despite not being
404// recognized as such by the C++ type system due to a move constructor or
405// destructor. To opt out of the relocatability check, do either of the
406// following things in any header used by `include!` in the bridge.
407//
408// --- if you define the type:
409// struct MyType {
410// ...
411// + using IsRelocatable = std::true_type;
412// };
413//
414// --- otherwise:
415// + template <>
416// + struct rust::IsRelocatable<MyType> : std::true_type {};
417template <typename T>
418struct IsRelocatable;
419
David Tolnay3b0c9882020-03-01 14:08:57 -0800420// Snake case aliases for use in code that uses this style for type names.
421using string = String;
422using str = Str;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800423template <typename T>
David Tolnay38c87642020-09-06 22:18:08 -0700424using slice = Slice<T>;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800425template <typename T>
David Tolnayf262d382020-04-11 22:12:40 -0700426using box = Box<T>;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800427template <typename T>
David Tolnay38c87642020-09-06 22:18:08 -0700428using vec = Vec<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700429using error = Error;
David Tolnayf262d382020-04-11 22:12:40 -0700430template <typename Signature>
David Tolnayf031c322020-11-29 19:41:33 -0800431using fn = Fn<Signature>;
David Tolnay174bd952020-11-02 09:23:12 -0800432template <typename T>
433using is_relocatable = IsRelocatable<T>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800434
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700435
436
437////////////////////////////////////////////////////////////////////////////////
438/// end public API, begin implementation details
439
David Tolnay0f0162f2020-11-16 23:43:37 -0800440#ifndef CXXBRIDGE1_PANIC
441#define CXXBRIDGE1_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700442template <typename Exception>
443void panic [[noreturn]] (const char *msg);
David Tolnay0f0162f2020-11-16 23:43:37 -0800444#endif // CXXBRIDGE1_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700445
David Tolnay0f0162f2020-11-16 23:43:37 -0800446#ifndef CXXBRIDGE1_RUST_FN
447#define CXXBRIDGE1_RUST_FN
David Tolnayf031c322020-11-29 19:41:33 -0800448template <typename Ret, typename... Args>
449Ret Fn<Ret(Args...)>::operator()(Args... args) const noexcept {
David Tolnay75dca2e2020-03-25 20:17:52 -0700450 return (*this->trampoline)(std::move(args)..., this->fn);
451}
452
David Tolnayf031c322020-11-29 19:41:33 -0800453template <typename Ret, typename... Args>
454Fn<Ret(Args...)> Fn<Ret(Args...)>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700455 return *this;
456}
David Tolnay0f0162f2020-11-16 23:43:37 -0800457#endif // CXXBRIDGE1_RUST_FN
David Tolnaya23129c2020-04-08 20:08:21 -0700458
David Tolnay0f0162f2020-11-16 23:43:37 -0800459#ifndef CXXBRIDGE1_RUST_BITCOPY
460#define CXXBRIDGE1_RUST_BITCOPY
David Tolnay48521222020-10-31 14:59:42 -0700461struct unsafe_bitcopy_t final {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700462 explicit unsafe_bitcopy_t() = default;
463};
464
465constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnay0f0162f2020-11-16 23:43:37 -0800466#endif // CXXBRIDGE1_RUST_BITCOPY
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700467
David Tolnay0f0162f2020-11-16 23:43:37 -0800468#ifndef CXXBRIDGE1_RUST_STR
469#define CXXBRIDGE1_RUST_STR
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700470inline const char *Str::data() const noexcept { return this->ptr; }
471
472inline size_t Str::size() const noexcept { return this->len; }
473
474inline size_t Str::length() const noexcept { return this->len; }
David Tolnay0f0162f2020-11-16 23:43:37 -0800475#endif // CXXBRIDGE1_RUST_STR
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700476
David Tolnay0f0162f2020-11-16 23:43:37 -0800477#ifndef CXXBRIDGE1_RUST_SLICE
478#define CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700479template <typename T>
David Tolnay7b16a392020-11-25 20:23:10 -0800480Slice<T>::Slice() noexcept : ptr(reinterpret_cast<T *>(alignof(T))), len(0) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700481
482template <typename T>
David Tolnayce298232020-11-11 10:08:54 -0800483Slice<T>::Slice(T *s, size_t count) noexcept : ptr(s), len(count) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700484
485template <typename T>
David Tolnayce298232020-11-11 10:08:54 -0800486T *Slice<T>::data() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700487 return this->ptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700488}
489
490template <typename T>
491size_t Slice<T>::size() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700492 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700493}
494
495template <typename T>
496size_t Slice<T>::length() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700497 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700498}
David Tolnayac6cb542020-11-25 20:18:55 -0800499
500template <typename T>
501T &Slice<T>::iterator::operator*() const noexcept {
502 return *this->pos;
503}
504
505template <typename T>
506T *Slice<T>::iterator::operator->() const noexcept {
507 return this->pos;
508}
509
510template <typename T>
511typename Slice<T>::iterator &Slice<T>::iterator::operator++() noexcept {
512 ++this->pos;
513 return *this;
514}
515
516template <typename T>
517typename Slice<T>::iterator Slice<T>::iterator::operator++(int) noexcept {
518 auto ret = iterator(*this);
519 ++this->pos;
520 return ret;
521}
522
523template <typename T>
524bool Slice<T>::iterator::operator==(const iterator &other) const noexcept {
525 return this->pos == other.pos;
526}
527
528template <typename T>
529bool Slice<T>::iterator::operator!=(const iterator &other) const noexcept {
530 return this->pos != other.pos;
531}
532
533template <typename T>
534typename Slice<T>::iterator Slice<T>::begin() const noexcept {
535 iterator it;
536 it.pos = this->ptr;
537 return it;
538}
539
540template <typename T>
541typename Slice<T>::iterator Slice<T>::end() const noexcept {
542 iterator it = this->begin();
543 it.pos += this->len;
544 return it;
545}
David Tolnay0f0162f2020-11-16 23:43:37 -0800546#endif // CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700547
David Tolnay0f0162f2020-11-16 23:43:37 -0800548#ifndef CXXBRIDGE1_RUST_BOX
549#define CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700550template <typename T>
David Tolnayc4b34222020-12-12 13:06:26 -0800551class Box<T>::uninit {};
552
553template <typename T>
David Tolnaye5703162020-12-12 16:26:35 -0800554class Box<T>::allocation {
555public:
David Tolnaye7d662d2020-12-12 16:38:22 -0800556 allocation() noexcept : ptr(alloc()) {}
557 ~allocation() noexcept {
558 if (this->ptr) {
559 dealloc(this->ptr);
560 }
561 }
David Tolnaye5703162020-12-12 16:26:35 -0800562 static T *alloc() noexcept;
563 static void dealloc(T *) noexcept;
564 T *ptr;
565};
566
567template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700568Box<T>::Box(const Box &other) : Box(*other) {}
569
570template <typename T>
571Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
572 other.ptr = nullptr;
573}
574
575template <typename T>
David Tolnaye7d662d2020-12-12 16:38:22 -0800576Box<T>::Box(const T &val) {
577 allocation alloc;
578 ::new (alloc.ptr) T(val);
579 this->ptr = alloc.ptr;
580 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700581}
582
583template <typename T>
David Tolnaye7d662d2020-12-12 16:38:22 -0800584Box<T>::Box(T &&val) {
585 allocation alloc;
586 ::new (alloc.ptr) T(std::move(val));
587 this->ptr = alloc.ptr;
588 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700589}
590
591template <typename T>
592Box<T>::~Box() noexcept {
593 if (this->ptr) {
594 this->drop();
595 }
596}
597
598template <typename T>
599Box<T> &Box<T>::operator=(const Box &other) {
600 if (this != &other) {
601 if (this->ptr) {
602 **this = *other;
603 } else {
David Tolnaye7d662d2020-12-12 16:38:22 -0800604 allocation alloc;
605 ::new (alloc.ptr) T(*other);
606 this->ptr = alloc.ptr;
607 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700608 }
609 }
610 return *this;
611}
612
613template <typename T>
614Box<T> &Box<T>::operator=(Box &&other) noexcept {
615 if (this->ptr) {
616 this->drop();
617 }
618 this->ptr = other.ptr;
619 other.ptr = nullptr;
620 return *this;
621}
622
623template <typename T>
624const T *Box<T>::operator->() const noexcept {
625 return this->ptr;
626}
627
628template <typename T>
629const T &Box<T>::operator*() const noexcept {
630 return *this->ptr;
631}
632
633template <typename T>
634T *Box<T>::operator->() noexcept {
635 return this->ptr;
636}
637
638template <typename T>
639T &Box<T>::operator*() noexcept {
640 return *this->ptr;
641}
642
643template <typename T>
644template <typename... Fields>
645Box<T> Box<T>::in_place(Fields &&... fields) {
David Tolnaye7d662d2020-12-12 16:38:22 -0800646 allocation alloc;
647 auto ptr = alloc.ptr;
648 ::new (ptr) T{std::forward<Fields>(fields)...};
649 alloc.ptr = nullptr;
650 return from_raw(ptr);
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700651}
652
653template <typename T>
654Box<T> Box<T>::from_raw(T *raw) noexcept {
David Tolnayc4b34222020-12-12 13:06:26 -0800655 Box box = uninit{};
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700656 box.ptr = raw;
657 return box;
658}
659
660template <typename T>
661T *Box<T>::into_raw() noexcept {
662 T *raw = this->ptr;
663 this->ptr = nullptr;
664 return raw;
665}
666
667template <typename T>
David Tolnayc4b34222020-12-12 13:06:26 -0800668Box<T>::Box(uninit) noexcept {}
David Tolnay0f0162f2020-11-16 23:43:37 -0800669#endif // CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700670
David Tolnay0f0162f2020-11-16 23:43:37 -0800671#ifndef CXXBRIDGE1_RUST_VEC
672#define CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700673template <typename T>
David Tolnayf799b372020-11-29 23:57:42 -0800674Vec<T>::Vec(std::initializer_list<T> init) : Vec{} {
675 this->reserve_total(init.size());
676 std::move(init.begin(), init.end(), std::back_inserter(*this));
677}
678
679template <typename T>
David Tolnay9007e462020-12-11 13:59:08 -0800680Vec<T>::Vec(const Vec &other) : Vec() {
681 this->reserve_total(other.size());
682 std::copy(other.begin(), other.end(), std::back_inserter(*this));
683}
684
685template <typename T>
David Tolnay15671862020-11-23 18:13:56 -0800686Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700687 new (&other) Vec();
688}
689
690template <typename T>
691Vec<T>::~Vec() noexcept {
692 this->drop();
693}
694
695template <typename T>
696Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
697 if (this != &other) {
698 this->drop();
699 this->repr = other.repr;
700 new (&other) Vec();
701 }
702 return *this;
703}
704
705template <typename T>
David Tolnaydd42c722020-12-11 14:05:26 -0800706Vec<T> &Vec<T>::operator=(const Vec &other) {
707 if (this != &other) {
708 this->drop();
709 new (this) Vec(other);
710 }
711 return *this;
712}
713
714template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700715bool Vec<T>::empty() const noexcept {
716 return size() == 0;
717}
718
719template <typename T>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800720T *Vec<T>::data() noexcept {
721 return const_cast<T *>(const_cast<const Vec<T> *>(this)->data());
722}
723
724template <typename T>
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700725const T &Vec<T>::operator[](size_t n) const noexcept {
726 auto data = reinterpret_cast<const char *>(this->data());
727 return *reinterpret_cast<const T *>(data + n * this->stride());
728}
729
730template <typename T>
731const T &Vec<T>::at(size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700732 if (n >= this->size()) {
733 panic<std::out_of_range>("rust::Vec index out of range");
734 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700735 return (*this)[n];
736}
737
738template <typename T>
739const T &Vec<T>::front() const {
740 return (*this)[0];
741}
742
743template <typename T>
744const T &Vec<T>::back() const {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700745 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700746}
747
748template <typename T>
David Tolnay908d5e52020-11-30 00:30:59 -0800749T &Vec<T>::operator[](size_t n) noexcept {
750 auto data = reinterpret_cast<char *>(this->data());
751 return *reinterpret_cast<T *>(data + n * this->stride());
752}
753
754template <typename T>
755T &Vec<T>::at(size_t n) {
756 if (n >= this->size()) {
757 panic<std::out_of_range>("rust::Vec index out of range");
758 }
759 return (*this)[n];
760}
761
762template <typename T>
763T &Vec<T>::front() {
764 return (*this)[0];
765}
766
767template <typename T>
768T &Vec<T>::back() {
769 return (*this)[this->size() - 1];
770}
771
772template <typename T>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800773void Vec<T>::reserve(size_t new_cap) {
774 this->reserve_total(new_cap);
775}
776
777template <typename T>
778void Vec<T>::push_back(const T &value) {
779 this->emplace_back(value);
780}
781
782template <typename T>
783void Vec<T>::push_back(T &&value) {
784 this->emplace_back(std::move(value));
785}
786
787template <typename T>
788template <typename... Args>
789void Vec<T>::emplace_back(Args &&... args) {
790 auto size = this->size();
791 this->reserve_total(size + 1);
792 ::new (reinterpret_cast<T *>(reinterpret_cast<char *>(this->data()) +
793 size * this->stride()))
794 T(std::forward<Args>(args)...);
795 this->set_len(size + 1);
796}
797
798template <typename T>
David Tolnay300072b2020-12-03 12:12:24 -0800799typename Vec<T>::iterator::reference
800Vec<T>::iterator::operator*() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800801 return *static_cast<T *>(this->pos);
802}
803
804template <typename T>
David Tolnay300072b2020-12-03 12:12:24 -0800805typename Vec<T>::iterator::pointer
806Vec<T>::iterator::operator->() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800807 return static_cast<T *>(this->pos);
808}
809
810template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500811typename Vec<T>::iterator::reference Vec<T>::iterator::operator[](
David Tolnay665178e2020-12-12 08:49:59 -0800812 typename Vec<T>::iterator::difference_type n) const noexcept {
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500813 auto pos = static_cast<char *>(this->pos) + this->stride * n;
814 return *static_cast<T *>(pos);
815}
816
817template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800818typename Vec<T>::iterator &Vec<T>::iterator::operator++() noexcept {
David Tolnay248215e2020-11-25 19:38:26 -0800819 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnay960b5112020-11-25 13:18:28 -0800820 return *this;
821}
822
823template <typename T>
824typename Vec<T>::iterator Vec<T>::iterator::operator++(int) noexcept {
825 auto ret = iterator(*this);
David Tolnay248215e2020-11-25 19:38:26 -0800826 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnay960b5112020-11-25 13:18:28 -0800827 return ret;
828}
829
830template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500831typename Vec<T>::iterator &Vec<T>::iterator::operator--() noexcept {
832 this->pos = static_cast<char *>(this->pos) - this->stride;
833 return *this;
834}
835
836template <typename T>
837typename Vec<T>::iterator Vec<T>::iterator::operator--(int) noexcept {
838 auto ret = iterator(*this);
839 this->pos = static_cast<char *>(this->pos) - this->stride;
840 return ret;
841}
842
843template <typename T>
David Tolnay665178e2020-12-12 08:49:59 -0800844typename Vec<T>::iterator &Vec<T>::iterator::operator+=(
845 typename Vec<T>::iterator::difference_type n) noexcept {
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500846 this->pos = static_cast<char *>(this->pos) + this->stride * n;
847 return *this;
848}
849
850template <typename T>
David Tolnay665178e2020-12-12 08:49:59 -0800851typename Vec<T>::iterator &Vec<T>::iterator::operator-=(
852 typename Vec<T>::iterator::difference_type n) noexcept {
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500853 this->pos = static_cast<char *>(this->pos) - this->stride * n;
854 return *this;
855}
856
857template <typename T>
858typename Vec<T>::iterator Vec<T>::iterator::operator+(
David Tolnay665178e2020-12-12 08:49:59 -0800859 typename Vec<T>::iterator::difference_type n) const noexcept {
David Tolnay300072b2020-12-03 12:12:24 -0800860 auto ret = iterator(*this);
861 ret.pos = static_cast<char *>(this->pos) + this->stride * n;
862 return ret;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500863}
864
865template <typename T>
866typename Vec<T>::iterator Vec<T>::iterator::operator-(
David Tolnay665178e2020-12-12 08:49:59 -0800867 typename Vec<T>::iterator::difference_type n) const noexcept {
David Tolnay300072b2020-12-03 12:12:24 -0800868 auto ret = iterator(*this);
869 ret.pos = static_cast<char *>(this->pos) - this->stride * n;
870 return ret;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500871}
872
873template <typename T>
874typename Vec<T>::iterator::difference_type
875Vec<T>::iterator::operator-(const iterator &other) const noexcept {
876 auto diff = std::distance(static_cast<char *>(other.pos),
877 static_cast<char *>(this->pos));
David Tolnay300072b2020-12-03 12:12:24 -0800878 return diff / this->stride;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500879}
880
881template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800882bool Vec<T>::iterator::operator==(const iterator &other) const noexcept {
883 return this->pos == other.pos;
884}
885
886template <typename T>
887bool Vec<T>::iterator::operator!=(const iterator &other) const noexcept {
888 return this->pos != other.pos;
889}
890
891template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500892bool Vec<T>::iterator::operator>(const iterator &other) const noexcept {
893 return this->pos > other.pos;
894}
895
896template <typename T>
897bool 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>
907bool Vec<T>::iterator::operator<=(const iterator &other) const noexcept {
908 return this->pos <= other.pos;
909}
910
911template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800912typename Vec<T>::iterator Vec<T>::begin() noexcept {
913 iterator it;
David Tolnaya5d72c62020-11-25 13:57:16 -0800914 it.pos = const_cast<typename std::remove_const<T>::type *>(this->data());
David Tolnay960b5112020-11-25 13:18:28 -0800915 it.stride = this->stride();
916 return it;
917}
918
919template <typename T>
920typename Vec<T>::iterator Vec<T>::end() noexcept {
921 iterator it = this->begin();
David Tolnay248215e2020-11-25 19:38:26 -0800922 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
David Tolnay960b5112020-11-25 13:18:28 -0800923 return it;
924}
925
926template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700927typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800928 return this->cbegin();
929}
930
931template <typename T>
932typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
933 return this->cend();
934}
935
936template <typename T>
937typename Vec<T>::const_iterator Vec<T>::cbegin() const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700938 const_iterator it;
David Tolnaya5d72c62020-11-25 13:57:16 -0800939 it.pos = const_cast<typename std::remove_const<T>::type *>(this->data());
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700940 it.stride = this->stride();
941 return it;
942}
943
944template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800945typename Vec<T>::const_iterator Vec<T>::cend() const noexcept {
946 const_iterator it = this->cbegin();
David Tolnay248215e2020-11-25 19:38:26 -0800947 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700948 return it;
949}
950
951// Internal API only intended for the cxxbridge code generator.
952template <typename T>
953Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay0f0162f2020-11-16 23:43:37 -0800954#endif // CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700955
David Tolnay0f0162f2020-11-16 23:43:37 -0800956#ifndef CXXBRIDGE1_RELOCATABLE
957#define CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -0800958namespace detail {
959template <typename... Ts>
960struct make_void {
961 using type = void;
962};
963
964template <typename... Ts>
965using void_t = typename make_void<Ts...>::type;
966
967template <typename Void, template <typename...> class, typename...>
968struct detect : std::false_type {};
969template <template <typename...> class T, typename... A>
970struct detect<void_t<T<A...>>, T, A...> : std::true_type {};
971
972template <template <typename...> class T, typename... A>
973using is_detected = detect<void, T, A...>;
974
975template <typename T>
976using detect_IsRelocatable = typename T::IsRelocatable;
977
978template <typename T>
979struct get_IsRelocatable
980 : std::is_same<typename T::IsRelocatable, std::true_type> {};
981} // namespace detail
982
983template <typename T>
984struct IsRelocatable
985 : std::conditional<
986 detail::is_detected<detail::detect_IsRelocatable, T>::value,
987 detail::get_IsRelocatable<T>,
988 std::integral_constant<
989 bool, std::is_trivially_move_constructible<T>::value &&
990 std::is_trivially_destructible<T>::value>>::type {};
David Tolnay0f0162f2020-11-16 23:43:37 -0800991#endif // CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -0800992
David Tolnay0f0162f2020-11-16 23:43:37 -0800993} // namespace cxxbridge1
David Tolnay750755e2020-03-01 13:04:08 -0800994} // namespace rust