blob: f41dde7ce96f040b3e9b5842b283b53ea19627df [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 Tolnayff7f5fb2020-11-25 20:50:32 -080056 using iterator = char *;
57 iterator begin() noexcept;
58 iterator end() noexcept;
59
60 using const_iterator = const char *;
61 const_iterator begin() const noexcept;
62 const_iterator end() const noexcept;
63 const_iterator cbegin() const noexcept;
64 const_iterator cend() const noexcept;
65
David Tolnayff86dce2020-11-29 19:45:13 -080066 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 bool operator>(const String &) const noexcept;
71 bool operator>=(const String &) const noexcept;
72
David Tolnayd1e2efc2020-03-03 22:25:43 -080073 // Internal API only intended for the cxxbridge code generator.
74 String(unsafe_bitcopy_t, const String &) noexcept;
75
David Tolnay7db73692019-10-20 14:51:12 -040076private:
77 // Size and alignment statically verified by rust_string.rs.
David Tolnaybe3cbf72020-12-12 22:12:07 -080078 std::array<std::uintptr_t, 3> repr;
David Tolnay7db73692019-10-20 14:51:12 -040079};
David Tolnay0f0162f2020-11-16 23:43:37 -080080#endif // CXXBRIDGE1_RUST_STRING
David Tolnay7db73692019-10-20 14:51:12 -040081
David Tolnay0f0162f2020-11-16 23:43:37 -080082#ifndef CXXBRIDGE1_RUST_STR
David Tolnaye468f052020-11-29 19:39:35 -080083// https://cxx.rs/binding/str.html
David Tolnay09dbe752020-03-01 13:00:40 -080084class Str final {
David Tolnay7db73692019-10-20 14:51:12 -040085public:
David Tolnay09dbe752020-03-01 13:00:40 -080086 Str() noexcept;
David Tolnay828e5132020-11-29 20:40:40 -080087 Str(const String &) noexcept;
David Tolnay851677c2020-03-01 23:49:46 -080088 Str(const std::string &);
89 Str(const char *);
David Tolnaybe3cbf72020-12-12 22:12:07 -080090 Str(const char *, std::size_t);
David Tolnayd9c4ac92020-03-01 20:33:58 -080091
David Tolnay2d7f1172020-10-31 17:58:31 -070092 Str &operator=(const Str &) noexcept = default;
David Tolnayd9c4ac92020-03-01 20:33:58 -080093
David Tolnay404d6892020-03-01 20:19:41 -080094 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040095
96 // Note: no null terminator.
97 const char *data() const noexcept;
David Tolnaybe3cbf72020-12-12 22:12:07 -080098 std::size_t size() const noexcept;
99 std::size_t length() const noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400100
David Tolnayde9a5b12020-10-31 12:15:43 -0700101 // Important in order for System V ABI to pass in registers.
102 Str(const Str &) noexcept = default;
103 ~Str() noexcept = default;
104
David Tolnayff7f5fb2020-11-25 20:50:32 -0800105 using iterator = const char *;
106 using const_iterator = const char *;
107 const_iterator begin() const noexcept;
108 const_iterator end() const noexcept;
109 const_iterator cbegin() const noexcept;
110 const_iterator cend() const noexcept;
111
David Tolnayff86dce2020-11-29 19:45:13 -0800112 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 bool operator>(const Str &) const noexcept;
117 bool operator>=(const Str &) const noexcept;
118
David Tolnay5df1f062020-10-31 12:31:10 -0700119private:
David Tolnay0356d332020-10-31 19:46:41 -0700120 friend impl<Str>;
David Tolnay7db73692019-10-20 14:51:12 -0400121 // Not necessarily ABI compatible with &str. Codegen will translate to
122 // cxx::rust_str::RustStr which matches this layout.
David Tolnay5df1f062020-10-31 12:31:10 -0700123 const char *ptr;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800124 std::size_t len;
David Tolnay7db73692019-10-20 14:51:12 -0400125};
David Tolnay0f0162f2020-11-16 23:43:37 -0800126#endif // CXXBRIDGE1_RUST_STR
David Tolnay7db73692019-10-20 14:51:12 -0400127
David Tolnay0f0162f2020-11-16 23:43:37 -0800128#ifndef CXXBRIDGE1_RUST_SLICE
David Tolnayc5629f02020-11-23 18:32:46 -0800129namespace detail {
David Tolnayee9b9ee2020-11-25 08:28:50 -0800130template <bool>
David Tolnayc5629f02020-11-23 18:32:46 -0800131struct copy_assignable_if {};
David Tolnayce298232020-11-11 10:08:54 -0800132
David Tolnayc5629f02020-11-23 18:32:46 -0800133template <>
134struct copy_assignable_if<false> {
135 copy_assignable_if() noexcept = default;
136 copy_assignable_if(const copy_assignable_if &) noexcept = default;
137 copy_assignable_if &operator=(const copy_assignable_if &) noexcept = delete;
138 copy_assignable_if &operator=(copy_assignable_if &&) noexcept = default;
139};
140} // namespace detail
141
David Tolnaye468f052020-11-29 19:39:35 -0800142// https://cxx.rs/binding/slice.html
David Tolnayc5629f02020-11-23 18:32:46 -0800143template <typename T>
144class Slice final
145 : private detail::copy_assignable_if<std::is_const<T>::value> {
David Tolnayefe81052020-04-14 16:28:24 -0700146public:
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700147 Slice() noexcept;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800148 Slice(T *, std::size_t count) noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700149
David Tolnay2d7f1172020-10-31 17:58:31 -0700150 Slice &operator=(const Slice<T> &) noexcept = default;
David Tolnayc5629f02020-11-23 18:32:46 -0800151 Slice &operator=(Slice<T> &&) noexcept = default;
David Tolnayefe81052020-04-14 16:28:24 -0700152
David Tolnayce298232020-11-11 10:08:54 -0800153 T *data() const noexcept;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800154 std::size_t size() const noexcept;
155 std::size_t length() const noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700156
David Tolnayde9a5b12020-10-31 12:15:43 -0700157 // Important in order for System V ABI to pass in registers.
158 Slice(const Slice<T> &) noexcept = default;
159 ~Slice() noexcept = default;
160
David Tolnayac6cb542020-11-25 20:18:55 -0800161 class iterator;
162 iterator begin() const noexcept;
163 iterator end() const noexcept;
164
David Tolnayefe81052020-04-14 16:28:24 -0700165private:
David Tolnay36aa9e02020-10-31 23:08:21 -0700166 friend impl<Slice>;
167 // Not necessarily ABI compatible with &[T]. Codegen will translate to
David Tolnay5515a9e2020-11-25 19:07:54 -0800168 // cxx::rust_slice::RustSlice which matches this layout.
David Tolnayce298232020-11-11 10:08:54 -0800169 T *ptr;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800170 std::size_t len;
David Tolnayefe81052020-04-14 16:28:24 -0700171};
David Tolnayac6cb542020-11-25 20:18:55 -0800172
173template <typename T>
174class Slice<T>::iterator final {
175public:
David Tolnaybe3cbf72020-12-12 22:12:07 -0800176 using difference_type = std::ptrdiff_t;
David Tolnayac6cb542020-11-25 20:18:55 -0800177 using value_type = T;
178 using pointer = typename std::add_pointer<T>::type;
179 using reference = typename std::add_lvalue_reference<T>::type;
180 using iterator_category = std::forward_iterator_tag;
181
182 T &operator*() const noexcept;
183 T *operator->() const noexcept;
184 iterator &operator++() noexcept;
185 iterator operator++(int) noexcept;
186 bool operator==(const iterator &) const noexcept;
187 bool operator!=(const iterator &) const noexcept;
188
189private:
190 friend class Slice;
191 T *pos;
192};
David Tolnay0f0162f2020-11-16 23:43:37 -0800193#endif // CXXBRIDGE1_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -0700194
David Tolnay0f0162f2020-11-16 23:43:37 -0800195#ifndef CXXBRIDGE1_RUST_BOX
David Tolnaye468f052020-11-29 19:39:35 -0800196// https://cxx.rs/binding/box.html
David Tolnayf262d382020-04-11 22:12:40 -0700197template <typename T>
198class Box final {
David Tolnay7db73692019-10-20 14:51:12 -0400199public:
David Tolnayf6292372020-03-01 21:09:11 -0800200 using value_type = T;
David Tolnay9706a512020-04-24 17:09:01 -0700201 using const_pointer =
202 typename std::add_pointer<typename std::add_const<T>::type>::type;
203 using pointer = typename std::add_pointer<T>::type;
David Tolnayf6292372020-03-01 21:09:11 -0800204
David Tolnayc4b34222020-12-12 13:06:26 -0800205 Box() = delete;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700206 Box(const Box &);
207 Box(Box &&) noexcept;
208 ~Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400209
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700210 explicit Box(const T &);
211 explicit Box(T &&);
212
213 Box &operator=(const Box &);
214 Box &operator=(Box &&) noexcept;
215
216 const T *operator->() const noexcept;
217 const T &operator*() const noexcept;
218 T *operator->() noexcept;
219 T &operator*() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400220
David Tolnayf262d382020-04-11 22:12:40 -0700221 template <typename... Fields>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700222 static Box in_place(Fields &&...);
David Tolnay7ce59fc2020-04-11 11:46:33 -0700223
David Tolnay7db73692019-10-20 14:51:12 -0400224 // Important: requires that `raw` came from an into_raw call. Do not pass a
225 // pointer from `new` or any other source.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700226 static Box from_raw(T *) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400227
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700228 T *into_raw() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400229
230private:
David Tolnayc4b34222020-12-12 13:06:26 -0800231 class uninit;
David Tolnaye5703162020-12-12 16:26:35 -0800232 class allocation;
David Tolnayc4b34222020-12-12 13:06:26 -0800233 Box(uninit) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400234 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800235 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400236};
David Tolnay0f0162f2020-11-16 23:43:37 -0800237#endif // CXXBRIDGE1_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400238
David Tolnay0f0162f2020-11-16 23:43:37 -0800239#ifndef CXXBRIDGE1_RUST_VEC
David Tolnaye468f052020-11-29 19:39:35 -0800240// https://cxx.rs/binding/vec.html
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700241template <typename T>
242class Vec final {
243public:
David Tolnayc87c2152020-04-24 17:07:41 -0700244 using value_type = T;
245
David Tolnayf97c2d52020-04-25 16:37:48 -0700246 Vec() noexcept;
David Tolnayf799b372020-11-29 23:57:42 -0800247 Vec(std::initializer_list<T>);
David Tolnay9007e462020-12-11 13:59:08 -0800248 Vec(const Vec &);
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700249 Vec(Vec &&) noexcept;
250 ~Vec() noexcept;
David Tolnaycb800572020-04-24 20:30:43 -0700251
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700252 Vec &operator=(Vec &&) noexcept;
David Tolnaydd42c722020-12-11 14:05:26 -0800253 Vec &operator=(const Vec &);
David Tolnayf97c2d52020-04-25 16:37:48 -0700254
David Tolnaybe3cbf72020-12-12 22:12:07 -0800255 std::size_t size() const noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700256 bool empty() const noexcept;
David Tolnay219c0792020-04-24 20:31:37 -0700257 const T *data() const noexcept;
David Tolnayfb6b73c2020-11-10 14:32:16 -0800258 T *data() noexcept;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800259 std::size_t capacity() const noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700260
David Tolnaybe3cbf72020-12-12 22:12:07 -0800261 const T &operator[](std::size_t n) const noexcept;
262 const T &at(std::size_t n) const;
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700263 const T &front() const;
264 const T &back() const;
265
David Tolnaybe3cbf72020-12-12 22:12:07 -0800266 T &operator[](std::size_t n) noexcept;
267 T &at(std::size_t n);
David Tolnay908d5e52020-11-30 00:30:59 -0800268 T &front();
269 T &back();
270
David Tolnaybe3cbf72020-12-12 22:12:07 -0800271 void reserve(std::size_t new_cap);
David Tolnayfb6b73c2020-11-10 14:32:16 -0800272 void push_back(const T &value);
273 void push_back(T &&value);
David Tolnay4e8c49a2020-11-11 10:00:18 -0800274 template <typename... Args>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800275 void emplace_back(Args &&... args);
276
David Tolnay960b5112020-11-25 13:18:28 -0800277 class iterator;
278 iterator begin() noexcept;
279 iterator end() noexcept;
280
David Tolnaya5d72c62020-11-25 13:57:16 -0800281 using const_iterator = typename Vec<const T>::iterator;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700282 const_iterator begin() const noexcept;
283 const_iterator end() const noexcept;
David Tolnay960b5112020-11-25 13:18:28 -0800284 const_iterator cbegin() const noexcept;
285 const_iterator cend() const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700286
David Tolnay313b10e2020-04-25 16:30:51 -0700287 // Internal API only intended for the cxxbridge code generator.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700288 Vec(unsafe_bitcopy_t, const Vec &) noexcept;
David Tolnay313b10e2020-04-25 16:30:51 -0700289
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700290private:
David Tolnaybe3cbf72020-12-12 22:12:07 -0800291 static std::size_t stride() noexcept;
292 void reserve_total(std::size_t cap) noexcept;
293 void set_len(std::size_t len) noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700294 void drop() noexcept;
295
296 // Size and alignment statically verified by rust_vec.rs.
David Tolnaybe3cbf72020-12-12 22:12:07 -0800297 std::array<std::uintptr_t, 3> repr;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700298};
David Tolnay66f216c2020-11-25 13:21:00 -0800299
300template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800301class Vec<T>::iterator final {
302public:
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500303 using iterator_category = std::random_access_iterator_tag;
David Tolnay960b5112020-11-25 13:18:28 -0800304 using value_type = T;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800305 using difference_type = std::ptrdiff_t;
David Tolnay960b5112020-11-25 13:18:28 -0800306 using pointer = typename std::add_pointer<T>::type;
307 using reference = typename std::add_lvalue_reference<T>::type;
David Tolnay960b5112020-11-25 13:18:28 -0800308
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500309 reference operator*() const noexcept;
310 pointer operator->() const noexcept;
311 reference operator[](difference_type) const noexcept;
312
David Tolnay960b5112020-11-25 13:18:28 -0800313 iterator &operator++() noexcept;
314 iterator operator++(int) noexcept;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500315 iterator &operator--() noexcept;
316 iterator operator--(int) noexcept;
317
318 iterator &operator+=(difference_type) noexcept;
319 iterator &operator-=(difference_type) noexcept;
320 iterator operator+(difference_type) const noexcept;
321 iterator operator-(difference_type) const noexcept;
322 difference_type operator-(const iterator &) const noexcept;
323
David Tolnay960b5112020-11-25 13:18:28 -0800324 bool operator==(const iterator &) const noexcept;
325 bool operator!=(const iterator &) const noexcept;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500326 bool operator<(const iterator &) const noexcept;
327 bool operator>(const iterator &) const noexcept;
328 bool operator<=(const iterator &) const noexcept;
329 bool operator>=(const iterator &) const noexcept;
David Tolnay960b5112020-11-25 13:18:28 -0800330
331private:
332 friend class Vec;
David Tolnaya5d72c62020-11-25 13:57:16 -0800333 friend class Vec<typename std::remove_const<T>::type>;
David Tolnay960b5112020-11-25 13:18:28 -0800334 void *pos;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800335 std::size_t stride;
David Tolnay960b5112020-11-25 13:18:28 -0800336};
David Tolnay0f0162f2020-11-16 23:43:37 -0800337#endif // CXXBRIDGE1_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700338
David Tolnay0f0162f2020-11-16 23:43:37 -0800339#ifndef CXXBRIDGE1_RUST_FN
David Tolnaye468f052020-11-29 19:39:35 -0800340// https://cxx.rs/binding/fn.html
David Tolnayf031c322020-11-29 19:41:33 -0800341template <typename Signature>
David Tolnayf262d382020-04-11 22:12:40 -0700342class Fn;
David Tolnay75dca2e2020-03-25 20:17:52 -0700343
David Tolnayf031c322020-11-29 19:41:33 -0800344template <typename Ret, typename... Args>
345class Fn<Ret(Args...)> final {
David Tolnay75dca2e2020-03-25 20:17:52 -0700346public:
David Tolnayf031c322020-11-29 19:41:33 -0800347 Ret operator()(Args... args) const noexcept;
David Tolnay533d4582020-04-08 20:29:14 -0700348 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700349
350private:
David Tolnayf031c322020-11-29 19:41:33 -0800351 Ret (*trampoline)(Args..., void *fn) noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700352 void *fn;
353};
David Tolnay0f0162f2020-11-16 23:43:37 -0800354#endif // CXXBRIDGE1_RUST_FN
David Tolnay75dca2e2020-03-25 20:17:52 -0700355
David Tolnay0f0162f2020-11-16 23:43:37 -0800356#ifndef CXXBRIDGE1_RUST_ERROR
357#define CXXBRIDGE1_RUST_ERROR
David Tolnaye468f052020-11-29 19:39:35 -0800358// https://cxx.rs/binding/result.html
David Tolnaye4fa8732020-09-08 15:04:56 -0700359class Error final : public std::exception {
David Tolnay1e548172020-03-16 13:37:09 -0700360public:
361 Error(const Error &);
362 Error(Error &&) noexcept;
David Tolnay2714d2c2020-11-23 18:17:43 -0800363 ~Error() noexcept override;
David Tolnay7c6ac712020-10-31 17:22:28 -0700364
365 Error &operator=(const Error &);
David Tolnay15491062020-10-31 17:25:13 -0700366 Error &operator=(Error &&) noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700367
David Tolnay1e548172020-03-16 13:37:09 -0700368 const char *what() const noexcept override;
369
370private:
David Tolnaya0c9bc72020-10-31 14:37:14 -0700371 Error() noexcept = default;
David Tolnay84ddf9e2020-10-31 15:36:48 -0700372 friend impl<Error>;
David Tolnaya0c9bc72020-10-31 14:37:14 -0700373 const char *msg;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800374 std::size_t len;
David Tolnay1e548172020-03-16 13:37:09 -0700375};
David Tolnay0f0162f2020-11-16 23:43:37 -0800376#endif // CXXBRIDGE1_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700377
David Tolnay0f0162f2020-11-16 23:43:37 -0800378#ifndef CXXBRIDGE1_RUST_ISIZE
379#define CXXBRIDGE1_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700380#if defined(_WIN32)
381using isize = SSIZE_T;
382#else
383using isize = ssize_t;
384#endif
David Tolnay0f0162f2020-11-16 23:43:37 -0800385#endif // CXXBRIDGE1_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700386
David Tolnay851677c2020-03-01 23:49:46 -0800387std::ostream &operator<<(std::ostream &, const String &);
388std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400389
David Tolnay365fc7c2020-11-25 16:08:13 -0800390#ifndef CXXBRIDGE1_RUST_OPAQUE
391#define CXXBRIDGE1_RUST_OPAQUE
392// Base class of generated opaque Rust types.
393class Opaque {
David Tolnaya857c322020-11-25 16:27:19 -0800394public:
David Tolnay365fc7c2020-11-25 16:08:13 -0800395 Opaque() = delete;
396 Opaque(const Opaque &) = delete;
397 ~Opaque() = delete;
398};
399#endif // CXXBRIDGE1_RUST_OPAQUE
400
David Tolnay174bd952020-11-02 09:23:12 -0800401// IsRelocatable<T> is used in assertions that a C++ type passed by value
402// between Rust and C++ is soundly relocatable by Rust.
403//
404// There may be legitimate reasons to opt out of the check for support of types
405// that the programmer knows are soundly Rust-movable despite not being
406// recognized as such by the C++ type system due to a move constructor or
407// destructor. To opt out of the relocatability check, do either of the
408// following things in any header used by `include!` in the bridge.
409//
410// --- if you define the type:
411// struct MyType {
412// ...
413// + using IsRelocatable = std::true_type;
414// };
415//
416// --- otherwise:
417// + template <>
418// + struct rust::IsRelocatable<MyType> : std::true_type {};
419template <typename T>
420struct IsRelocatable;
421
David Tolnaybe3cbf72020-12-12 22:12:07 -0800422using u8 = std::uint8_t;
423using u16 = std::uint16_t;
424using u32 = std::uint32_t;
425using u64 = std::uint64_t;
426using usize = std::size_t; // see static asserts in cxx.cc
427using i8 = std::int8_t;
428using i16 = std::int16_t;
429using i32 = std::int32_t;
430using i64 = std::int64_t;
David Tolnay1e784a32020-12-12 21:34:47 -0800431using f32 = float;
432using f64 = double;
433
David Tolnay3b0c9882020-03-01 14:08:57 -0800434// Snake case aliases for use in code that uses this style for type names.
435using string = String;
436using str = Str;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800437template <typename T>
David Tolnay38c87642020-09-06 22:18:08 -0700438using slice = Slice<T>;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800439template <typename T>
David Tolnayf262d382020-04-11 22:12:40 -0700440using box = Box<T>;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800441template <typename T>
David Tolnay38c87642020-09-06 22:18:08 -0700442using vec = Vec<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700443using error = Error;
David Tolnayf262d382020-04-11 22:12:40 -0700444template <typename Signature>
David Tolnayf031c322020-11-29 19:41:33 -0800445using fn = Fn<Signature>;
David Tolnay174bd952020-11-02 09:23:12 -0800446template <typename T>
447using is_relocatable = IsRelocatable<T>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800448
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700449
450
451////////////////////////////////////////////////////////////////////////////////
452/// end public API, begin implementation details
453
David Tolnay0f0162f2020-11-16 23:43:37 -0800454#ifndef CXXBRIDGE1_PANIC
455#define CXXBRIDGE1_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700456template <typename Exception>
457void panic [[noreturn]] (const char *msg);
David Tolnay0f0162f2020-11-16 23:43:37 -0800458#endif // CXXBRIDGE1_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700459
David Tolnay0f0162f2020-11-16 23:43:37 -0800460#ifndef CXXBRIDGE1_RUST_FN
461#define CXXBRIDGE1_RUST_FN
David Tolnayf031c322020-11-29 19:41:33 -0800462template <typename Ret, typename... Args>
463Ret Fn<Ret(Args...)>::operator()(Args... args) const noexcept {
David Tolnay75dca2e2020-03-25 20:17:52 -0700464 return (*this->trampoline)(std::move(args)..., this->fn);
465}
466
David Tolnayf031c322020-11-29 19:41:33 -0800467template <typename Ret, typename... Args>
468Fn<Ret(Args...)> Fn<Ret(Args...)>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700469 return *this;
470}
David Tolnay0f0162f2020-11-16 23:43:37 -0800471#endif // CXXBRIDGE1_RUST_FN
David Tolnaya23129c2020-04-08 20:08:21 -0700472
David Tolnay0f0162f2020-11-16 23:43:37 -0800473#ifndef CXXBRIDGE1_RUST_BITCOPY
474#define CXXBRIDGE1_RUST_BITCOPY
David Tolnay48521222020-10-31 14:59:42 -0700475struct unsafe_bitcopy_t final {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700476 explicit unsafe_bitcopy_t() = default;
477};
478
479constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnay0f0162f2020-11-16 23:43:37 -0800480#endif // CXXBRIDGE1_RUST_BITCOPY
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700481
David Tolnay0f0162f2020-11-16 23:43:37 -0800482#ifndef CXXBRIDGE1_RUST_STR
483#define CXXBRIDGE1_RUST_STR
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700484inline const char *Str::data() const noexcept { return this->ptr; }
485
David Tolnaybe3cbf72020-12-12 22:12:07 -0800486inline std::size_t Str::size() const noexcept { return this->len; }
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700487
David Tolnaybe3cbf72020-12-12 22:12:07 -0800488inline std::size_t Str::length() const noexcept { return this->len; }
David Tolnay0f0162f2020-11-16 23:43:37 -0800489#endif // CXXBRIDGE1_RUST_STR
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700490
David Tolnay0f0162f2020-11-16 23:43:37 -0800491#ifndef CXXBRIDGE1_RUST_SLICE
492#define CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700493template <typename T>
David Tolnay7b16a392020-11-25 20:23:10 -0800494Slice<T>::Slice() noexcept : ptr(reinterpret_cast<T *>(alignof(T))), len(0) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700495
496template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800497Slice<T>::Slice(T *s, std::size_t count) noexcept : ptr(s), len(count) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700498
499template <typename T>
David Tolnayce298232020-11-11 10:08:54 -0800500T *Slice<T>::data() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700501 return this->ptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700502}
503
504template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800505std::size_t Slice<T>::size() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700506 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700507}
508
509template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800510std::size_t Slice<T>::length() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700511 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700512}
David Tolnayac6cb542020-11-25 20:18:55 -0800513
514template <typename T>
515T &Slice<T>::iterator::operator*() const noexcept {
516 return *this->pos;
517}
518
519template <typename T>
520T *Slice<T>::iterator::operator->() const noexcept {
521 return this->pos;
522}
523
524template <typename T>
525typename Slice<T>::iterator &Slice<T>::iterator::operator++() noexcept {
526 ++this->pos;
527 return *this;
528}
529
530template <typename T>
531typename Slice<T>::iterator Slice<T>::iterator::operator++(int) noexcept {
532 auto ret = iterator(*this);
533 ++this->pos;
534 return ret;
535}
536
537template <typename T>
538bool Slice<T>::iterator::operator==(const iterator &other) const noexcept {
539 return this->pos == other.pos;
540}
541
542template <typename T>
543bool Slice<T>::iterator::operator!=(const iterator &other) const noexcept {
544 return this->pos != other.pos;
545}
546
547template <typename T>
548typename Slice<T>::iterator Slice<T>::begin() const noexcept {
549 iterator it;
550 it.pos = this->ptr;
551 return it;
552}
553
554template <typename T>
555typename Slice<T>::iterator Slice<T>::end() const noexcept {
556 iterator it = this->begin();
557 it.pos += this->len;
558 return it;
559}
David Tolnay0f0162f2020-11-16 23:43:37 -0800560#endif // CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700561
David Tolnay0f0162f2020-11-16 23:43:37 -0800562#ifndef CXXBRIDGE1_RUST_BOX
563#define CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700564template <typename T>
David Tolnayc4b34222020-12-12 13:06:26 -0800565class Box<T>::uninit {};
566
567template <typename T>
David Tolnaye5703162020-12-12 16:26:35 -0800568class Box<T>::allocation {
David Tolnayf448e202020-12-12 16:42:45 -0800569 static T *alloc() noexcept;
570 static void dealloc(T *) noexcept;
David Tolnay2e637d92020-12-12 21:08:01 -0800571
David Tolnaye5703162020-12-12 16:26:35 -0800572public:
David Tolnaye7d662d2020-12-12 16:38:22 -0800573 allocation() noexcept : ptr(alloc()) {}
574 ~allocation() noexcept {
575 if (this->ptr) {
576 dealloc(this->ptr);
577 }
578 }
David Tolnaye5703162020-12-12 16:26:35 -0800579 T *ptr;
580};
581
582template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700583Box<T>::Box(const Box &other) : Box(*other) {}
584
585template <typename T>
586Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
587 other.ptr = nullptr;
588}
589
590template <typename T>
David Tolnaye7d662d2020-12-12 16:38:22 -0800591Box<T>::Box(const T &val) {
592 allocation alloc;
593 ::new (alloc.ptr) T(val);
594 this->ptr = alloc.ptr;
595 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700596}
597
598template <typename T>
David Tolnaye7d662d2020-12-12 16:38:22 -0800599Box<T>::Box(T &&val) {
600 allocation alloc;
601 ::new (alloc.ptr) T(std::move(val));
602 this->ptr = alloc.ptr;
603 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700604}
605
606template <typename T>
607Box<T>::~Box() noexcept {
608 if (this->ptr) {
609 this->drop();
610 }
611}
612
613template <typename T>
614Box<T> &Box<T>::operator=(const Box &other) {
David Tolnay439ed0b2020-12-12 17:01:04 -0800615 if (this->ptr) {
616 **this = *other;
617 } else {
618 allocation alloc;
619 ::new (alloc.ptr) T(*other);
620 this->ptr = alloc.ptr;
621 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700622 }
623 return *this;
624}
625
626template <typename T>
627Box<T> &Box<T>::operator=(Box &&other) noexcept {
628 if (this->ptr) {
629 this->drop();
630 }
631 this->ptr = other.ptr;
632 other.ptr = nullptr;
633 return *this;
634}
635
636template <typename T>
637const T *Box<T>::operator->() const noexcept {
638 return this->ptr;
639}
640
641template <typename T>
642const T &Box<T>::operator*() const noexcept {
643 return *this->ptr;
644}
645
646template <typename T>
647T *Box<T>::operator->() noexcept {
648 return this->ptr;
649}
650
651template <typename T>
652T &Box<T>::operator*() noexcept {
653 return *this->ptr;
654}
655
656template <typename T>
657template <typename... Fields>
658Box<T> Box<T>::in_place(Fields &&... fields) {
David Tolnaye7d662d2020-12-12 16:38:22 -0800659 allocation alloc;
660 auto ptr = alloc.ptr;
661 ::new (ptr) T{std::forward<Fields>(fields)...};
662 alloc.ptr = nullptr;
663 return from_raw(ptr);
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700664}
665
666template <typename T>
667Box<T> Box<T>::from_raw(T *raw) noexcept {
David Tolnayc4b34222020-12-12 13:06:26 -0800668 Box box = uninit{};
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700669 box.ptr = raw;
670 return box;
671}
672
673template <typename T>
674T *Box<T>::into_raw() noexcept {
675 T *raw = this->ptr;
676 this->ptr = nullptr;
677 return raw;
678}
679
680template <typename T>
David Tolnayc4b34222020-12-12 13:06:26 -0800681Box<T>::Box(uninit) noexcept {}
David Tolnay0f0162f2020-11-16 23:43:37 -0800682#endif // CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700683
David Tolnay0f0162f2020-11-16 23:43:37 -0800684#ifndef CXXBRIDGE1_RUST_VEC
685#define CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700686template <typename T>
David Tolnayf799b372020-11-29 23:57:42 -0800687Vec<T>::Vec(std::initializer_list<T> init) : Vec{} {
688 this->reserve_total(init.size());
689 std::move(init.begin(), init.end(), std::back_inserter(*this));
690}
691
692template <typename T>
David Tolnay9007e462020-12-11 13:59:08 -0800693Vec<T>::Vec(const Vec &other) : Vec() {
694 this->reserve_total(other.size());
695 std::copy(other.begin(), other.end(), std::back_inserter(*this));
696}
697
698template <typename T>
David Tolnay15671862020-11-23 18:13:56 -0800699Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700700 new (&other) Vec();
701}
702
703template <typename T>
704Vec<T>::~Vec() noexcept {
705 this->drop();
706}
707
708template <typename T>
709Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
710 if (this != &other) {
711 this->drop();
712 this->repr = other.repr;
713 new (&other) Vec();
714 }
715 return *this;
716}
717
718template <typename T>
David Tolnaydd42c722020-12-11 14:05:26 -0800719Vec<T> &Vec<T>::operator=(const Vec &other) {
720 if (this != &other) {
721 this->drop();
722 new (this) Vec(other);
723 }
724 return *this;
725}
726
727template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700728bool Vec<T>::empty() const noexcept {
729 return size() == 0;
730}
731
732template <typename T>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800733T *Vec<T>::data() noexcept {
734 return const_cast<T *>(const_cast<const Vec<T> *>(this)->data());
735}
736
737template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800738const T &Vec<T>::operator[](std::size_t n) const noexcept {
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700739 auto data = reinterpret_cast<const char *>(this->data());
740 return *reinterpret_cast<const T *>(data + n * this->stride());
741}
742
743template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800744const T &Vec<T>::at(std::size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700745 if (n >= this->size()) {
746 panic<std::out_of_range>("rust::Vec index out of range");
747 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700748 return (*this)[n];
749}
750
751template <typename T>
752const T &Vec<T>::front() const {
753 return (*this)[0];
754}
755
756template <typename T>
757const T &Vec<T>::back() const {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700758 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700759}
760
761template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800762T &Vec<T>::operator[](std::size_t n) noexcept {
David Tolnay908d5e52020-11-30 00:30:59 -0800763 auto data = reinterpret_cast<char *>(this->data());
764 return *reinterpret_cast<T *>(data + n * this->stride());
765}
766
767template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800768T &Vec<T>::at(std::size_t n) {
David Tolnay908d5e52020-11-30 00:30:59 -0800769 if (n >= this->size()) {
770 panic<std::out_of_range>("rust::Vec index out of range");
771 }
772 return (*this)[n];
773}
774
775template <typename T>
776T &Vec<T>::front() {
777 return (*this)[0];
778}
779
780template <typename T>
781T &Vec<T>::back() {
782 return (*this)[this->size() - 1];
783}
784
785template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800786void Vec<T>::reserve(std::size_t new_cap) {
David Tolnayfb6b73c2020-11-10 14:32:16 -0800787 this->reserve_total(new_cap);
788}
789
790template <typename T>
791void Vec<T>::push_back(const T &value) {
792 this->emplace_back(value);
793}
794
795template <typename T>
796void Vec<T>::push_back(T &&value) {
797 this->emplace_back(std::move(value));
798}
799
800template <typename T>
801template <typename... Args>
802void Vec<T>::emplace_back(Args &&... args) {
803 auto size = this->size();
804 this->reserve_total(size + 1);
805 ::new (reinterpret_cast<T *>(reinterpret_cast<char *>(this->data()) +
806 size * this->stride()))
807 T(std::forward<Args>(args)...);
808 this->set_len(size + 1);
809}
810
811template <typename T>
David Tolnay300072b2020-12-03 12:12:24 -0800812typename Vec<T>::iterator::reference
813Vec<T>::iterator::operator*() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800814 return *static_cast<T *>(this->pos);
815}
816
817template <typename T>
David Tolnay300072b2020-12-03 12:12:24 -0800818typename Vec<T>::iterator::pointer
819Vec<T>::iterator::operator->() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800820 return static_cast<T *>(this->pos);
821}
822
823template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500824typename Vec<T>::iterator::reference Vec<T>::iterator::operator[](
David Tolnay665178e2020-12-12 08:49:59 -0800825 typename Vec<T>::iterator::difference_type n) const noexcept {
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500826 auto pos = static_cast<char *>(this->pos) + this->stride * n;
827 return *static_cast<T *>(pos);
828}
829
830template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800831typename Vec<T>::iterator &Vec<T>::iterator::operator++() noexcept {
David Tolnay248215e2020-11-25 19:38:26 -0800832 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnay960b5112020-11-25 13:18:28 -0800833 return *this;
834}
835
836template <typename T>
837typename Vec<T>::iterator Vec<T>::iterator::operator++(int) noexcept {
838 auto ret = iterator(*this);
David Tolnay248215e2020-11-25 19:38:26 -0800839 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnay960b5112020-11-25 13:18:28 -0800840 return ret;
841}
842
843template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500844typename Vec<T>::iterator &Vec<T>::iterator::operator--() noexcept {
845 this->pos = static_cast<char *>(this->pos) - this->stride;
846 return *this;
847}
848
849template <typename T>
850typename Vec<T>::iterator Vec<T>::iterator::operator--(int) noexcept {
851 auto ret = iterator(*this);
852 this->pos = static_cast<char *>(this->pos) - this->stride;
853 return ret;
854}
855
856template <typename T>
David Tolnay665178e2020-12-12 08:49:59 -0800857typename Vec<T>::iterator &Vec<T>::iterator::operator+=(
858 typename Vec<T>::iterator::difference_type n) noexcept {
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500859 this->pos = static_cast<char *>(this->pos) + this->stride * n;
860 return *this;
861}
862
863template <typename T>
David Tolnay665178e2020-12-12 08:49:59 -0800864typename Vec<T>::iterator &Vec<T>::iterator::operator-=(
865 typename Vec<T>::iterator::difference_type n) noexcept {
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500866 this->pos = static_cast<char *>(this->pos) - this->stride * n;
867 return *this;
868}
869
870template <typename T>
871typename Vec<T>::iterator Vec<T>::iterator::operator+(
David Tolnay665178e2020-12-12 08:49:59 -0800872 typename Vec<T>::iterator::difference_type n) const noexcept {
David Tolnay300072b2020-12-03 12:12:24 -0800873 auto ret = iterator(*this);
874 ret.pos = static_cast<char *>(this->pos) + this->stride * n;
875 return ret;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500876}
877
878template <typename T>
879typename Vec<T>::iterator Vec<T>::iterator::operator-(
David Tolnay665178e2020-12-12 08:49:59 -0800880 typename Vec<T>::iterator::difference_type n) const noexcept {
David Tolnay300072b2020-12-03 12:12:24 -0800881 auto ret = iterator(*this);
882 ret.pos = static_cast<char *>(this->pos) - this->stride * n;
883 return ret;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500884}
885
886template <typename T>
887typename Vec<T>::iterator::difference_type
888Vec<T>::iterator::operator-(const iterator &other) const noexcept {
889 auto diff = std::distance(static_cast<char *>(other.pos),
890 static_cast<char *>(this->pos));
David Tolnay300072b2020-12-03 12:12:24 -0800891 return diff / this->stride;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500892}
893
894template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800895bool Vec<T>::iterator::operator==(const iterator &other) const noexcept {
896 return this->pos == other.pos;
897}
898
899template <typename T>
900bool Vec<T>::iterator::operator!=(const iterator &other) const noexcept {
901 return this->pos != other.pos;
902}
903
904template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500905bool Vec<T>::iterator::operator>(const iterator &other) const noexcept {
906 return this->pos > other.pos;
907}
908
909template <typename T>
910bool Vec<T>::iterator::operator<(const iterator &other) const noexcept {
911 return this->pos < other.pos;
912}
913
914template <typename T>
915bool Vec<T>::iterator::operator>=(const iterator &other) const noexcept {
916 return this->pos >= other.pos;
917}
918
919template <typename T>
920bool Vec<T>::iterator::operator<=(const iterator &other) const noexcept {
921 return this->pos <= other.pos;
922}
923
924template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800925typename Vec<T>::iterator Vec<T>::begin() noexcept {
926 iterator it;
David Tolnaya5d72c62020-11-25 13:57:16 -0800927 it.pos = const_cast<typename std::remove_const<T>::type *>(this->data());
David Tolnay960b5112020-11-25 13:18:28 -0800928 it.stride = this->stride();
929 return it;
930}
931
932template <typename T>
933typename Vec<T>::iterator Vec<T>::end() noexcept {
934 iterator it = this->begin();
David Tolnay248215e2020-11-25 19:38:26 -0800935 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
David Tolnay960b5112020-11-25 13:18:28 -0800936 return it;
937}
938
939template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700940typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800941 return this->cbegin();
942}
943
944template <typename T>
945typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
946 return this->cend();
947}
948
949template <typename T>
950typename Vec<T>::const_iterator Vec<T>::cbegin() const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700951 const_iterator it;
David Tolnaya5d72c62020-11-25 13:57:16 -0800952 it.pos = const_cast<typename std::remove_const<T>::type *>(this->data());
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700953 it.stride = this->stride();
954 return it;
955}
956
957template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800958typename Vec<T>::const_iterator Vec<T>::cend() const noexcept {
959 const_iterator it = this->cbegin();
David Tolnay248215e2020-11-25 19:38:26 -0800960 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700961 return it;
962}
963
964// Internal API only intended for the cxxbridge code generator.
965template <typename T>
966Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay0f0162f2020-11-16 23:43:37 -0800967#endif // CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700968
David Tolnay0f0162f2020-11-16 23:43:37 -0800969#ifndef CXXBRIDGE1_RELOCATABLE
970#define CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -0800971namespace detail {
972template <typename... Ts>
973struct make_void {
974 using type = void;
975};
976
977template <typename... Ts>
978using void_t = typename make_void<Ts...>::type;
979
980template <typename Void, template <typename...> class, typename...>
981struct detect : std::false_type {};
982template <template <typename...> class T, typename... A>
983struct detect<void_t<T<A...>>, T, A...> : std::true_type {};
984
985template <template <typename...> class T, typename... A>
986using is_detected = detect<void, T, A...>;
987
988template <typename T>
989using detect_IsRelocatable = typename T::IsRelocatable;
990
991template <typename T>
992struct get_IsRelocatable
993 : std::is_same<typename T::IsRelocatable, std::true_type> {};
994} // namespace detail
995
996template <typename T>
997struct IsRelocatable
998 : std::conditional<
999 detail::is_detected<detail::detect_IsRelocatable, T>::value,
1000 detail::get_IsRelocatable<T>,
1001 std::integral_constant<
1002 bool, std::is_trivially_move_constructible<T>::value &&
1003 std::is_trivially_destructible<T>::value>>::type {};
David Tolnay0f0162f2020-11-16 23:43:37 -08001004#endif // CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -08001005
David Tolnay0f0162f2020-11-16 23:43:37 -08001006} // namespace cxxbridge1
David Tolnay750755e2020-03-01 13:04:08 -08001007} // namespace rust