blob: eadc313e95ac796c447305c9fc8a69bcd64468b5 [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 Tolnayfa5a4a62020-12-21 14:08:08 -08004#include <cassert>
David Tolnay30430f12020-03-19 20:49:00 -07005#include <cstddef>
David Tolnay7db73692019-10-20 14:51:12 -04006#include <cstdint>
David Tolnayb7a7cb62020-03-17 21:18:40 -07007#include <exception>
David Tolnayf799b372020-11-29 23:57:42 -08008#include <initializer_list>
David Tolnay001102a2020-03-01 20:05:04 -08009#include <iosfwd>
David Tolnayd1df4c72020-11-25 20:38:05 -080010#include <iterator>
David Tolnay0ecd05a2020-07-29 16:32:03 -070011#include <new>
Stephen Crane9e48d5b2020-08-21 12:17:02 -070012#include <stdexcept>
David Tolnay7db73692019-10-20 14:51:12 -040013#include <string>
David Tolnayf6292372020-03-01 21:09:11 -080014#include <type_traits>
David Tolnay4791f1c2020-03-17 21:53:16 -070015#include <utility>
David Tolnay37dd7e12020-04-25 12:51:59 -070016#include <vector>
David Tolnay59b5ba12020-04-10 11:32:19 -070017#if defined(_WIN32)
David Tolnayda38b7c2020-09-16 11:50:04 -040018#include <basetsd.h>
David Tolnay74dd66f2020-12-12 22:03:47 -080019#else
20#include <sys/types.h>
David Tolnay59b5ba12020-04-10 11:32:19 -070021#endif
David Tolnay7db73692019-10-20 14:51:12 -040022
David Tolnay750755e2020-03-01 13:04:08 -080023namespace rust {
David Tolnay0f0162f2020-11-16 23:43:37 -080024inline namespace cxxbridge1 {
David Tolnay7db73692019-10-20 14:51:12 -040025
David Tolnay2a2b9ad2020-05-12 20:07:26 -070026struct unsafe_bitcopy_t;
David Tolnayd1e2efc2020-03-03 22:25:43 -080027
David Tolnay84ddf9e2020-10-31 15:36:48 -070028namespace {
29template <typename T>
30class impl;
31}
32
David Tolnay0f0162f2020-11-16 23:43:37 -080033#ifndef CXXBRIDGE1_RUST_STRING
34#define CXXBRIDGE1_RUST_STRING
David Tolnaye468f052020-11-29 19:39:35 -080035// https://cxx.rs/binding/string.html
David Tolnay56082162020-03-01 12:57:33 -080036class String final {
David Tolnay7db73692019-10-20 14:51:12 -040037public:
David Tolnay56082162020-03-01 12:57:33 -080038 String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080039 String(const String &) noexcept;
40 String(String &&) noexcept;
David Tolnay56082162020-03-01 12:57:33 -080041 ~String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080042
43 String(const std::string &);
44 String(const char *);
David Tolnaybe3cbf72020-12-12 22:12:07 -080045 String(const char *, std::size_t);
David Tolnayd9c4ac92020-03-01 20:33:58 -080046
47 String &operator=(const String &) noexcept;
48 String &operator=(String &&) noexcept;
49
David Tolnay404d6892020-03-01 20:19:41 -080050 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040051
52 // Note: no null terminator.
53 const char *data() const noexcept;
David Tolnaybe3cbf72020-12-12 22:12:07 -080054 std::size_t size() const noexcept;
55 std::size_t length() const noexcept;
David Tolnay7db73692019-10-20 14:51:12 -040056
David Tolnaycca2e612020-12-18 12:48:22 -080057 const char *c_str() noexcept;
58
David Tolnayff7f5fb2020-11-25 20:50:32 -080059 using iterator = char *;
60 iterator begin() noexcept;
61 iterator end() noexcept;
62
63 using const_iterator = const char *;
64 const_iterator begin() const noexcept;
65 const_iterator end() const noexcept;
66 const_iterator cbegin() const noexcept;
67 const_iterator cend() const noexcept;
68
David Tolnayff86dce2020-11-29 19:45:13 -080069 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 bool operator>=(const String &) const noexcept;
75
David Tolnayd1e2efc2020-03-03 22:25:43 -080076 // Internal API only intended for the cxxbridge code generator.
77 String(unsafe_bitcopy_t, const String &) noexcept;
78
David Tolnay7db73692019-10-20 14:51:12 -040079private:
80 // Size and alignment statically verified by rust_string.rs.
David Tolnaybe3cbf72020-12-12 22:12:07 -080081 std::array<std::uintptr_t, 3> repr;
David Tolnay7db73692019-10-20 14:51:12 -040082};
David Tolnay0f0162f2020-11-16 23:43:37 -080083#endif // CXXBRIDGE1_RUST_STRING
David Tolnay7db73692019-10-20 14:51:12 -040084
David Tolnay0f0162f2020-11-16 23:43:37 -080085#ifndef CXXBRIDGE1_RUST_STR
David Tolnaye468f052020-11-29 19:39:35 -080086// https://cxx.rs/binding/str.html
David Tolnay09dbe752020-03-01 13:00:40 -080087class Str final {
David Tolnay7db73692019-10-20 14:51:12 -040088public:
David Tolnay09dbe752020-03-01 13:00:40 -080089 Str() noexcept;
David Tolnay828e5132020-11-29 20:40:40 -080090 Str(const String &) noexcept;
David Tolnay851677c2020-03-01 23:49:46 -080091 Str(const std::string &);
92 Str(const char *);
David Tolnaybe3cbf72020-12-12 22:12:07 -080093 Str(const char *, std::size_t);
David Tolnayd9c4ac92020-03-01 20:33:58 -080094
David Tolnay2d7f1172020-10-31 17:58:31 -070095 Str &operator=(const Str &) noexcept = default;
David Tolnayd9c4ac92020-03-01 20:33:58 -080096
David Tolnay404d6892020-03-01 20:19:41 -080097 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040098
99 // Note: no null terminator.
100 const char *data() const noexcept;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800101 std::size_t size() const noexcept;
102 std::size_t length() const noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400103
David Tolnayde9a5b12020-10-31 12:15:43 -0700104 // Important in order for System V ABI to pass in registers.
105 Str(const Str &) noexcept = default;
106 ~Str() noexcept = default;
107
David Tolnayff7f5fb2020-11-25 20:50:32 -0800108 using iterator = const char *;
109 using const_iterator = const char *;
110 const_iterator begin() const noexcept;
111 const_iterator end() const noexcept;
112 const_iterator cbegin() const noexcept;
113 const_iterator cend() const noexcept;
114
David Tolnayff86dce2020-11-29 19:45:13 -0800115 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 bool operator>=(const Str &) const noexcept;
121
David Tolnay5df1f062020-10-31 12:31:10 -0700122private:
David Tolnay0356d332020-10-31 19:46:41 -0700123 friend impl<Str>;
David Tolnay7db73692019-10-20 14:51:12 -0400124 // Not necessarily ABI compatible with &str. Codegen will translate to
125 // cxx::rust_str::RustStr which matches this layout.
David Tolnay5df1f062020-10-31 12:31:10 -0700126 const char *ptr;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800127 std::size_t len;
David Tolnay7db73692019-10-20 14:51:12 -0400128};
David Tolnay0f0162f2020-11-16 23:43:37 -0800129#endif // CXXBRIDGE1_RUST_STR
David Tolnay7db73692019-10-20 14:51:12 -0400130
David Tolnay0f0162f2020-11-16 23:43:37 -0800131#ifndef CXXBRIDGE1_RUST_SLICE
David Tolnayc5629f02020-11-23 18:32:46 -0800132namespace detail {
David Tolnayee9b9ee2020-11-25 08:28:50 -0800133template <bool>
David Tolnayc5629f02020-11-23 18:32:46 -0800134struct copy_assignable_if {};
David Tolnayce298232020-11-11 10:08:54 -0800135
David Tolnayc5629f02020-11-23 18:32:46 -0800136template <>
137struct copy_assignable_if<false> {
138 copy_assignable_if() noexcept = default;
139 copy_assignable_if(const copy_assignable_if &) noexcept = default;
140 copy_assignable_if &operator=(const copy_assignable_if &) noexcept = delete;
141 copy_assignable_if &operator=(copy_assignable_if &&) noexcept = default;
142};
143} // namespace detail
144
David Tolnaye468f052020-11-29 19:39:35 -0800145// https://cxx.rs/binding/slice.html
David Tolnayc5629f02020-11-23 18:32:46 -0800146template <typename T>
147class Slice final
148 : private detail::copy_assignable_if<std::is_const<T>::value> {
David Tolnayefe81052020-04-14 16:28:24 -0700149public:
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700150 Slice() noexcept;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800151 Slice(T *, std::size_t count) noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700152
David Tolnay2d7f1172020-10-31 17:58:31 -0700153 Slice &operator=(const Slice<T> &) noexcept = default;
David Tolnayc5629f02020-11-23 18:32:46 -0800154 Slice &operator=(Slice<T> &&) noexcept = default;
David Tolnayefe81052020-04-14 16:28:24 -0700155
David Tolnayce298232020-11-11 10:08:54 -0800156 T *data() const noexcept;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800157 std::size_t size() const noexcept;
158 std::size_t length() const noexcept;
David Tolnay78dd5352020-12-26 23:44:20 -0800159 bool empty() const noexcept;
160
161 T &operator[](std::size_t n) const noexcept;
162 T &at(std::size_t n) const;
163 T &front() const noexcept;
164 T &back() const noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700165
David Tolnayde9a5b12020-10-31 12:15:43 -0700166 // Important in order for System V ABI to pass in registers.
167 Slice(const Slice<T> &) noexcept = default;
168 ~Slice() noexcept = default;
169
David Tolnayac6cb542020-11-25 20:18:55 -0800170 class iterator;
171 iterator begin() const noexcept;
172 iterator end() const noexcept;
173
David Tolnayefe81052020-04-14 16:28:24 -0700174private:
David Tolnay36aa9e02020-10-31 23:08:21 -0700175 friend impl<Slice>;
176 // Not necessarily ABI compatible with &[T]. Codegen will translate to
David Tolnay5515a9e2020-11-25 19:07:54 -0800177 // cxx::rust_slice::RustSlice which matches this layout.
David Tolnay5ce110e2020-12-27 02:45:53 -0800178 void *ptr;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800179 std::size_t len;
David Tolnayefe81052020-04-14 16:28:24 -0700180};
David Tolnayac6cb542020-11-25 20:18:55 -0800181
182template <typename T>
183class Slice<T>::iterator final {
184public:
David Tolnayc30e4472020-12-27 00:25:42 -0800185 using iterator_category = std::random_access_iterator_tag;
David Tolnayac6cb542020-11-25 20:18:55 -0800186 using value_type = T;
David Tolnayc30e4472020-12-27 00:25:42 -0800187 using difference_type = std::ptrdiff_t;
David Tolnayac6cb542020-11-25 20:18:55 -0800188 using pointer = typename std::add_pointer<T>::type;
189 using reference = typename std::add_lvalue_reference<T>::type;
David Tolnayac6cb542020-11-25 20:18:55 -0800190
David Tolnaydb388c92020-12-27 00:28:26 -0800191 reference operator*() const noexcept;
192 pointer operator->() const noexcept;
David Tolnayc30e4472020-12-27 00:25:42 -0800193 reference operator[](difference_type) const noexcept;
194
David Tolnayac6cb542020-11-25 20:18:55 -0800195 iterator &operator++() noexcept;
196 iterator operator++(int) noexcept;
David Tolnayc30e4472020-12-27 00:25:42 -0800197 iterator &operator--() noexcept;
198 iterator operator--(int) noexcept;
199
200 iterator &operator+=(difference_type) noexcept;
201 iterator &operator-=(difference_type) noexcept;
202 iterator operator+(difference_type) const noexcept;
203 iterator operator-(difference_type) const noexcept;
204 difference_type operator-(const iterator &) const noexcept;
205
David Tolnayac6cb542020-11-25 20:18:55 -0800206 bool operator==(const iterator &) const noexcept;
207 bool operator!=(const iterator &) const noexcept;
David Tolnayc30e4472020-12-27 00:25:42 -0800208 bool operator<(const iterator &) const noexcept;
209 bool operator<=(const iterator &) const noexcept;
210 bool operator>(const iterator &) const noexcept;
211 bool operator>=(const iterator &) const noexcept;
David Tolnayac6cb542020-11-25 20:18:55 -0800212
213private:
214 friend class Slice;
David Tolnaya1ddbf82020-12-27 00:56:35 -0800215 void *pos;
216 std::size_t stride;
David Tolnayac6cb542020-11-25 20:18:55 -0800217};
David Tolnay0f0162f2020-11-16 23:43:37 -0800218#endif // CXXBRIDGE1_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -0700219
David Tolnay0f0162f2020-11-16 23:43:37 -0800220#ifndef CXXBRIDGE1_RUST_BOX
David Tolnaye468f052020-11-29 19:39:35 -0800221// https://cxx.rs/binding/box.html
David Tolnayf262d382020-04-11 22:12:40 -0700222template <typename T>
223class Box final {
David Tolnay7db73692019-10-20 14:51:12 -0400224public:
David Tolnayf6292372020-03-01 21:09:11 -0800225 using value_type = T;
David Tolnay9706a512020-04-24 17:09:01 -0700226 using const_pointer =
227 typename std::add_pointer<typename std::add_const<T>::type>::type;
228 using pointer = typename std::add_pointer<T>::type;
David Tolnayf6292372020-03-01 21:09:11 -0800229
David Tolnayc4b34222020-12-12 13:06:26 -0800230 Box() = delete;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700231 Box(const Box &);
232 Box(Box &&) noexcept;
233 ~Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400234
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700235 explicit Box(const T &);
236 explicit Box(T &&);
237
238 Box &operator=(const Box &);
239 Box &operator=(Box &&) noexcept;
240
241 const T *operator->() const noexcept;
242 const T &operator*() const noexcept;
243 T *operator->() noexcept;
244 T &operator*() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400245
David Tolnayf262d382020-04-11 22:12:40 -0700246 template <typename... Fields>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700247 static Box in_place(Fields &&...);
David Tolnay7ce59fc2020-04-11 11:46:33 -0700248
David Tolnay7db73692019-10-20 14:51:12 -0400249 // Important: requires that `raw` came from an into_raw call. Do not pass a
250 // pointer from `new` or any other source.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700251 static Box from_raw(T *) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400252
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700253 T *into_raw() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400254
255private:
David Tolnayc4b34222020-12-12 13:06:26 -0800256 class uninit;
David Tolnaye5703162020-12-12 16:26:35 -0800257 class allocation;
David Tolnayc4b34222020-12-12 13:06:26 -0800258 Box(uninit) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400259 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800260 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400261};
David Tolnay0f0162f2020-11-16 23:43:37 -0800262#endif // CXXBRIDGE1_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400263
David Tolnay0f0162f2020-11-16 23:43:37 -0800264#ifndef CXXBRIDGE1_RUST_VEC
David Tolnaye468f052020-11-29 19:39:35 -0800265// https://cxx.rs/binding/vec.html
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700266template <typename T>
267class Vec final {
268public:
David Tolnayc87c2152020-04-24 17:07:41 -0700269 using value_type = T;
270
David Tolnayf97c2d52020-04-25 16:37:48 -0700271 Vec() noexcept;
David Tolnayf799b372020-11-29 23:57:42 -0800272 Vec(std::initializer_list<T>);
David Tolnay9007e462020-12-11 13:59:08 -0800273 Vec(const Vec &);
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700274 Vec(Vec &&) noexcept;
275 ~Vec() noexcept;
David Tolnaycb800572020-04-24 20:30:43 -0700276
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700277 Vec &operator=(Vec &&) noexcept;
David Tolnaydd42c722020-12-11 14:05:26 -0800278 Vec &operator=(const Vec &);
David Tolnayf97c2d52020-04-25 16:37:48 -0700279
David Tolnaybe3cbf72020-12-12 22:12:07 -0800280 std::size_t size() const noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700281 bool empty() const noexcept;
David Tolnay219c0792020-04-24 20:31:37 -0700282 const T *data() const noexcept;
David Tolnayfb6b73c2020-11-10 14:32:16 -0800283 T *data() noexcept;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800284 std::size_t capacity() const noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700285
David Tolnaybe3cbf72020-12-12 22:12:07 -0800286 const T &operator[](std::size_t n) const noexcept;
287 const T &at(std::size_t n) const;
David Tolnayd4fff5d2020-12-21 14:04:09 -0800288 const T &front() const noexcept;
289 const T &back() const noexcept;
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700290
David Tolnaybe3cbf72020-12-12 22:12:07 -0800291 T &operator[](std::size_t n) noexcept;
292 T &at(std::size_t n);
David Tolnayd4fff5d2020-12-21 14:04:09 -0800293 T &front() noexcept;
294 T &back() noexcept;
David Tolnay908d5e52020-11-30 00:30:59 -0800295
David Tolnaybe3cbf72020-12-12 22:12:07 -0800296 void reserve(std::size_t new_cap);
David Tolnayfb6b73c2020-11-10 14:32:16 -0800297 void push_back(const T &value);
298 void push_back(T &&value);
David Tolnay4e8c49a2020-11-11 10:00:18 -0800299 template <typename... Args>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800300 void emplace_back(Args &&... args);
301
David Tolnay960b5112020-11-25 13:18:28 -0800302 class iterator;
303 iterator begin() noexcept;
304 iterator end() noexcept;
305
David Tolnaya5d72c62020-11-25 13:57:16 -0800306 using const_iterator = typename Vec<const T>::iterator;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700307 const_iterator begin() const noexcept;
308 const_iterator end() const noexcept;
David Tolnay960b5112020-11-25 13:18:28 -0800309 const_iterator cbegin() const noexcept;
310 const_iterator cend() const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700311
David Tolnay313b10e2020-04-25 16:30:51 -0700312 // Internal API only intended for the cxxbridge code generator.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700313 Vec(unsafe_bitcopy_t, const Vec &) noexcept;
David Tolnay313b10e2020-04-25 16:30:51 -0700314
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700315private:
David Tolnaybe3cbf72020-12-12 22:12:07 -0800316 void reserve_total(std::size_t cap) noexcept;
317 void set_len(std::size_t len) noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700318 void drop() noexcept;
319
320 // Size and alignment statically verified by rust_vec.rs.
David Tolnaybe3cbf72020-12-12 22:12:07 -0800321 std::array<std::uintptr_t, 3> repr;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700322};
David Tolnay66f216c2020-11-25 13:21:00 -0800323
324template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800325class Vec<T>::iterator final {
326public:
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500327 using iterator_category = std::random_access_iterator_tag;
David Tolnay960b5112020-11-25 13:18:28 -0800328 using value_type = T;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800329 using difference_type = std::ptrdiff_t;
David Tolnay960b5112020-11-25 13:18:28 -0800330 using pointer = typename std::add_pointer<T>::type;
331 using reference = typename std::add_lvalue_reference<T>::type;
David Tolnay960b5112020-11-25 13:18:28 -0800332
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500333 reference operator*() const noexcept;
334 pointer operator->() const noexcept;
335 reference operator[](difference_type) const noexcept;
336
David Tolnay960b5112020-11-25 13:18:28 -0800337 iterator &operator++() noexcept;
338 iterator operator++(int) noexcept;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500339 iterator &operator--() noexcept;
340 iterator operator--(int) noexcept;
341
342 iterator &operator+=(difference_type) noexcept;
343 iterator &operator-=(difference_type) noexcept;
344 iterator operator+(difference_type) const noexcept;
345 iterator operator-(difference_type) const noexcept;
346 difference_type operator-(const iterator &) const noexcept;
347
David Tolnay960b5112020-11-25 13:18:28 -0800348 bool operator==(const iterator &) const noexcept;
349 bool operator!=(const iterator &) const noexcept;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500350 bool operator<(const iterator &) const noexcept;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500351 bool operator<=(const iterator &) const noexcept;
David Tolnay0c9c3e62020-12-27 00:52:25 -0800352 bool operator>(const iterator &) const noexcept;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500353 bool operator>=(const iterator &) const noexcept;
David Tolnay960b5112020-11-25 13:18:28 -0800354
355private:
356 friend class Vec;
David Tolnaya5d72c62020-11-25 13:57:16 -0800357 friend class Vec<typename std::remove_const<T>::type>;
David Tolnay960b5112020-11-25 13:18:28 -0800358 void *pos;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800359 std::size_t stride;
David Tolnay960b5112020-11-25 13:18:28 -0800360};
David Tolnay0f0162f2020-11-16 23:43:37 -0800361#endif // CXXBRIDGE1_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700362
David Tolnay0f0162f2020-11-16 23:43:37 -0800363#ifndef CXXBRIDGE1_RUST_FN
David Tolnaye468f052020-11-29 19:39:35 -0800364// https://cxx.rs/binding/fn.html
David Tolnayf031c322020-11-29 19:41:33 -0800365template <typename Signature>
David Tolnayf262d382020-04-11 22:12:40 -0700366class Fn;
David Tolnay75dca2e2020-03-25 20:17:52 -0700367
David Tolnayf031c322020-11-29 19:41:33 -0800368template <typename Ret, typename... Args>
369class Fn<Ret(Args...)> final {
David Tolnay75dca2e2020-03-25 20:17:52 -0700370public:
David Tolnayf031c322020-11-29 19:41:33 -0800371 Ret operator()(Args... args) const noexcept;
David Tolnay533d4582020-04-08 20:29:14 -0700372 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700373
374private:
David Tolnayf031c322020-11-29 19:41:33 -0800375 Ret (*trampoline)(Args..., void *fn) noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700376 void *fn;
377};
David Tolnay0f0162f2020-11-16 23:43:37 -0800378#endif // CXXBRIDGE1_RUST_FN
David Tolnay75dca2e2020-03-25 20:17:52 -0700379
David Tolnay0f0162f2020-11-16 23:43:37 -0800380#ifndef CXXBRIDGE1_RUST_ERROR
381#define CXXBRIDGE1_RUST_ERROR
David Tolnaye468f052020-11-29 19:39:35 -0800382// https://cxx.rs/binding/result.html
David Tolnaye4fa8732020-09-08 15:04:56 -0700383class Error final : public std::exception {
David Tolnay1e548172020-03-16 13:37:09 -0700384public:
385 Error(const Error &);
386 Error(Error &&) noexcept;
David Tolnay2714d2c2020-11-23 18:17:43 -0800387 ~Error() noexcept override;
David Tolnay7c6ac712020-10-31 17:22:28 -0700388
389 Error &operator=(const Error &);
David Tolnay15491062020-10-31 17:25:13 -0700390 Error &operator=(Error &&) noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700391
David Tolnay1e548172020-03-16 13:37:09 -0700392 const char *what() const noexcept override;
393
394private:
David Tolnaya0c9bc72020-10-31 14:37:14 -0700395 Error() noexcept = default;
David Tolnay84ddf9e2020-10-31 15:36:48 -0700396 friend impl<Error>;
David Tolnaya0c9bc72020-10-31 14:37:14 -0700397 const char *msg;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800398 std::size_t len;
David Tolnay1e548172020-03-16 13:37:09 -0700399};
David Tolnay0f0162f2020-11-16 23:43:37 -0800400#endif // CXXBRIDGE1_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700401
David Tolnay0f0162f2020-11-16 23:43:37 -0800402#ifndef CXXBRIDGE1_RUST_ISIZE
403#define CXXBRIDGE1_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700404#if defined(_WIN32)
405using isize = SSIZE_T;
406#else
407using isize = ssize_t;
408#endif
David Tolnay0f0162f2020-11-16 23:43:37 -0800409#endif // CXXBRIDGE1_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700410
David Tolnay851677c2020-03-01 23:49:46 -0800411std::ostream &operator<<(std::ostream &, const String &);
412std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400413
David Tolnay365fc7c2020-11-25 16:08:13 -0800414#ifndef CXXBRIDGE1_RUST_OPAQUE
415#define CXXBRIDGE1_RUST_OPAQUE
416// Base class of generated opaque Rust types.
417class Opaque {
David Tolnaya857c322020-11-25 16:27:19 -0800418public:
David Tolnay365fc7c2020-11-25 16:08:13 -0800419 Opaque() = delete;
420 Opaque(const Opaque &) = delete;
421 ~Opaque() = delete;
422};
423#endif // CXXBRIDGE1_RUST_OPAQUE
424
David Tolnayee6ecfc2020-12-26 21:54:37 -0800425template <typename T>
426std::size_t size_of();
427template <typename T>
428std::size_t align_of();
429
David Tolnay174bd952020-11-02 09:23:12 -0800430// IsRelocatable<T> is used in assertions that a C++ type passed by value
431// between Rust and C++ is soundly relocatable by Rust.
432//
433// There may be legitimate reasons to opt out of the check for support of types
434// that the programmer knows are soundly Rust-movable despite not being
435// recognized as such by the C++ type system due to a move constructor or
436// destructor. To opt out of the relocatability check, do either of the
437// following things in any header used by `include!` in the bridge.
438//
439// --- if you define the type:
440// struct MyType {
441// ...
442// + using IsRelocatable = std::true_type;
443// };
444//
445// --- otherwise:
446// + template <>
447// + struct rust::IsRelocatable<MyType> : std::true_type {};
448template <typename T>
449struct IsRelocatable;
450
David Tolnaybe3cbf72020-12-12 22:12:07 -0800451using u8 = std::uint8_t;
452using u16 = std::uint16_t;
453using u32 = std::uint32_t;
454using u64 = std::uint64_t;
455using usize = std::size_t; // see static asserts in cxx.cc
456using i8 = std::int8_t;
457using i16 = std::int16_t;
458using i32 = std::int32_t;
459using i64 = std::int64_t;
David Tolnay1e784a32020-12-12 21:34:47 -0800460using f32 = float;
461using f64 = double;
462
David Tolnay3b0c9882020-03-01 14:08:57 -0800463// Snake case aliases for use in code that uses this style for type names.
464using string = String;
465using str = Str;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800466template <typename T>
David Tolnay38c87642020-09-06 22:18:08 -0700467using slice = Slice<T>;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800468template <typename T>
David Tolnayf262d382020-04-11 22:12:40 -0700469using box = Box<T>;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800470template <typename T>
David Tolnay38c87642020-09-06 22:18:08 -0700471using vec = Vec<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700472using error = Error;
David Tolnayf262d382020-04-11 22:12:40 -0700473template <typename Signature>
David Tolnayf031c322020-11-29 19:41:33 -0800474using fn = Fn<Signature>;
David Tolnay174bd952020-11-02 09:23:12 -0800475template <typename T>
476using is_relocatable = IsRelocatable<T>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800477
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700478
479
480////////////////////////////////////////////////////////////////////////////////
481/// end public API, begin implementation details
482
David Tolnay0f0162f2020-11-16 23:43:37 -0800483#ifndef CXXBRIDGE1_PANIC
484#define CXXBRIDGE1_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700485template <typename Exception>
486void panic [[noreturn]] (const char *msg);
David Tolnay0f0162f2020-11-16 23:43:37 -0800487#endif // CXXBRIDGE1_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700488
David Tolnay0f0162f2020-11-16 23:43:37 -0800489#ifndef CXXBRIDGE1_RUST_FN
490#define CXXBRIDGE1_RUST_FN
David Tolnayf031c322020-11-29 19:41:33 -0800491template <typename Ret, typename... Args>
492Ret Fn<Ret(Args...)>::operator()(Args... args) const noexcept {
David Tolnay75dca2e2020-03-25 20:17:52 -0700493 return (*this->trampoline)(std::move(args)..., this->fn);
494}
495
David Tolnayf031c322020-11-29 19:41:33 -0800496template <typename Ret, typename... Args>
497Fn<Ret(Args...)> Fn<Ret(Args...)>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700498 return *this;
499}
David Tolnay0f0162f2020-11-16 23:43:37 -0800500#endif // CXXBRIDGE1_RUST_FN
David Tolnaya23129c2020-04-08 20:08:21 -0700501
David Tolnay0f0162f2020-11-16 23:43:37 -0800502#ifndef CXXBRIDGE1_RUST_BITCOPY
503#define CXXBRIDGE1_RUST_BITCOPY
David Tolnay48521222020-10-31 14:59:42 -0700504struct unsafe_bitcopy_t final {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700505 explicit unsafe_bitcopy_t() = default;
506};
507
508constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnay0f0162f2020-11-16 23:43:37 -0800509#endif // CXXBRIDGE1_RUST_BITCOPY
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700510
David Tolnay0f0162f2020-11-16 23:43:37 -0800511#ifndef CXXBRIDGE1_RUST_STR
512#define CXXBRIDGE1_RUST_STR
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700513inline const char *Str::data() const noexcept { return this->ptr; }
514
David Tolnaybe3cbf72020-12-12 22:12:07 -0800515inline std::size_t Str::size() const noexcept { return this->len; }
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700516
David Tolnaybe3cbf72020-12-12 22:12:07 -0800517inline std::size_t Str::length() const noexcept { return this->len; }
David Tolnay0f0162f2020-11-16 23:43:37 -0800518#endif // CXXBRIDGE1_RUST_STR
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700519
David Tolnay0f0162f2020-11-16 23:43:37 -0800520#ifndef CXXBRIDGE1_RUST_SLICE
521#define CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700522template <typename T>
David Tolnayeee622c2020-12-27 01:26:17 -0800523Slice<T>::Slice() noexcept
David Tolnay5ce110e2020-12-27 02:45:53 -0800524 : ptr(reinterpret_cast<void *>(align_of<T>())), len(0) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700525
526template <typename T>
David Tolnay5ce110e2020-12-27 02:45:53 -0800527Slice<T>::Slice(T *s, std::size_t count) noexcept
528 : ptr(const_cast<typename std::remove_const<T>::type *>(s)), len(count) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700529
530template <typename T>
David Tolnayce298232020-11-11 10:08:54 -0800531T *Slice<T>::data() const noexcept {
David Tolnay5ce110e2020-12-27 02:45:53 -0800532 return reinterpret_cast<T *>(this->ptr);
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700533}
534
535template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800536std::size_t Slice<T>::size() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700537 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700538}
539
540template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800541std::size_t Slice<T>::length() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700542 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700543}
David Tolnayac6cb542020-11-25 20:18:55 -0800544
545template <typename T>
David Tolnay78dd5352020-12-26 23:44:20 -0800546bool Slice<T>::empty() const noexcept {
547 return this->len == 0;
548}
549
550template <typename T>
551T &Slice<T>::operator[](std::size_t n) const noexcept {
552 assert(n < this->size());
David Tolnay5ce110e2020-12-27 02:45:53 -0800553 auto pos = static_cast<char *>(this->ptr) + size_of<T>() * n;
554 return *reinterpret_cast<T *>(pos);
David Tolnay78dd5352020-12-26 23:44:20 -0800555}
556
557template <typename T>
558T &Slice<T>::at(std::size_t n) const {
559 if (n >= this->size()) {
560 panic<std::out_of_range>("rust::Slice index out of range");
561 }
562 return (*this)[n];
563}
564
565template <typename T>
566T &Slice<T>::front() const noexcept {
567 assert(!this->empty());
David Tolnay5ce110e2020-12-27 02:45:53 -0800568 return (*this)[0];
David Tolnay78dd5352020-12-26 23:44:20 -0800569}
570
571template <typename T>
572T &Slice<T>::back() const noexcept {
573 assert(!this->empty());
David Tolnay5ce110e2020-12-27 02:45:53 -0800574 return (*this)[this->len - 1];
David Tolnay78dd5352020-12-26 23:44:20 -0800575}
576
577template <typename T>
David Tolnaydb388c92020-12-27 00:28:26 -0800578typename Slice<T>::iterator::reference
579Slice<T>::iterator::operator*() const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800580 return *static_cast<T *>(this->pos);
David Tolnayac6cb542020-11-25 20:18:55 -0800581}
582
583template <typename T>
David Tolnaydb388c92020-12-27 00:28:26 -0800584typename Slice<T>::iterator::pointer
585Slice<T>::iterator::operator->() const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800586 return static_cast<T *>(this->pos);
David Tolnayac6cb542020-11-25 20:18:55 -0800587}
588
589template <typename T>
David Tolnayc30e4472020-12-27 00:25:42 -0800590typename Slice<T>::iterator::reference Slice<T>::iterator::operator[](
591 typename Slice<T>::iterator::difference_type n) const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800592 auto pos = static_cast<char *>(this->pos) + this->stride * n;
593 return *static_cast<T *>(pos);
David Tolnayc30e4472020-12-27 00:25:42 -0800594}
595
596template <typename T>
David Tolnayac6cb542020-11-25 20:18:55 -0800597typename Slice<T>::iterator &Slice<T>::iterator::operator++() noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800598 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnayac6cb542020-11-25 20:18:55 -0800599 return *this;
600}
601
602template <typename T>
603typename Slice<T>::iterator Slice<T>::iterator::operator++(int) noexcept {
604 auto ret = iterator(*this);
David Tolnaya1ddbf82020-12-27 00:56:35 -0800605 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnayac6cb542020-11-25 20:18:55 -0800606 return ret;
607}
608
609template <typename T>
David Tolnayc30e4472020-12-27 00:25:42 -0800610typename Slice<T>::iterator &Slice<T>::iterator::operator--() noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800611 this->pos = static_cast<char *>(this->pos) - this->stride;
David Tolnayc30e4472020-12-27 00:25:42 -0800612 return *this;
613}
614
615template <typename T>
616typename Slice<T>::iterator Slice<T>::iterator::operator--(int) noexcept {
617 auto ret = iterator(*this);
David Tolnaya1ddbf82020-12-27 00:56:35 -0800618 this->pos = static_cast<char *>(this->pos) - this->stride;
David Tolnayc30e4472020-12-27 00:25:42 -0800619 return ret;
620}
621
622template <typename T>
623typename Slice<T>::iterator &Slice<T>::iterator::operator+=(
624 typename Slice<T>::iterator::difference_type n) noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800625 this->pos = static_cast<char *>(this->pos) + this->stride * n;
David Tolnayc30e4472020-12-27 00:25:42 -0800626 return *this;
627}
628
629template <typename T>
630typename Slice<T>::iterator &Slice<T>::iterator::operator-=(
631 typename Slice<T>::iterator::difference_type n) noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800632 this->pos = static_cast<char *>(this->pos) - this->stride * n;
David Tolnayc30e4472020-12-27 00:25:42 -0800633 return *this;
634}
635
636template <typename T>
637typename Slice<T>::iterator Slice<T>::iterator::operator+(
638 typename Slice<T>::iterator::difference_type n) const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800639 auto ret = iterator(*this);
640 ret.pos = static_cast<char *>(this->pos) + this->stride * n;
641 return ret;
David Tolnayc30e4472020-12-27 00:25:42 -0800642}
643
644template <typename T>
645typename Slice<T>::iterator Slice<T>::iterator::operator-(
646 typename Slice<T>::iterator::difference_type n) const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800647 auto ret = iterator(*this);
648 ret.pos = static_cast<char *>(this->pos) - this->stride * n;
649 return ret;
David Tolnayc30e4472020-12-27 00:25:42 -0800650}
651
652template <typename T>
653typename Slice<T>::iterator::difference_type
654Slice<T>::iterator::operator-(const iterator &other) const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800655 auto diff = std::distance(static_cast<char *>(other.pos),
656 static_cast<char *>(this->pos));
657 return diff / this->stride;
David Tolnayc30e4472020-12-27 00:25:42 -0800658}
659
660template <typename T>
David Tolnayac6cb542020-11-25 20:18:55 -0800661bool Slice<T>::iterator::operator==(const iterator &other) const noexcept {
662 return this->pos == other.pos;
663}
664
665template <typename T>
666bool Slice<T>::iterator::operator!=(const iterator &other) const noexcept {
667 return this->pos != other.pos;
668}
669
670template <typename T>
David Tolnayc30e4472020-12-27 00:25:42 -0800671bool Slice<T>::iterator::operator<(const iterator &other) const noexcept {
672 return this->pos < other.pos;
673}
674
675template <typename T>
676bool Slice<T>::iterator::operator<=(const iterator &other) const noexcept {
677 return this->pos <= other.pos;
678}
679
680template <typename T>
681bool Slice<T>::iterator::operator>(const iterator &other) const noexcept {
682 return this->pos > other.pos;
683}
684
685template <typename T>
686bool Slice<T>::iterator::operator>=(const iterator &other) const noexcept {
687 return this->pos >= other.pos;
688}
689
690template <typename T>
David Tolnayac6cb542020-11-25 20:18:55 -0800691typename Slice<T>::iterator Slice<T>::begin() const noexcept {
692 iterator it;
David Tolnay5ce110e2020-12-27 02:45:53 -0800693 it.pos = this->ptr;
David Tolnay6f92baa2020-12-27 01:09:26 -0800694 it.stride = size_of<T>();
David Tolnayac6cb542020-11-25 20:18:55 -0800695 return it;
696}
697
698template <typename T>
699typename Slice<T>::iterator Slice<T>::end() const noexcept {
700 iterator it = this->begin();
David Tolnaya1ddbf82020-12-27 00:56:35 -0800701 it.pos = static_cast<char *>(it.pos) + it.stride * this->len;
David Tolnayac6cb542020-11-25 20:18:55 -0800702 return it;
703}
David Tolnay0f0162f2020-11-16 23:43:37 -0800704#endif // CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700705
David Tolnay0f0162f2020-11-16 23:43:37 -0800706#ifndef CXXBRIDGE1_RUST_BOX
707#define CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700708template <typename T>
David Tolnayc4b34222020-12-12 13:06:26 -0800709class Box<T>::uninit {};
710
711template <typename T>
David Tolnaye5703162020-12-12 16:26:35 -0800712class Box<T>::allocation {
David Tolnayf448e202020-12-12 16:42:45 -0800713 static T *alloc() noexcept;
714 static void dealloc(T *) noexcept;
David Tolnay2e637d92020-12-12 21:08:01 -0800715
David Tolnaye5703162020-12-12 16:26:35 -0800716public:
David Tolnaye7d662d2020-12-12 16:38:22 -0800717 allocation() noexcept : ptr(alloc()) {}
718 ~allocation() noexcept {
719 if (this->ptr) {
720 dealloc(this->ptr);
721 }
722 }
David Tolnaye5703162020-12-12 16:26:35 -0800723 T *ptr;
724};
725
726template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700727Box<T>::Box(const Box &other) : Box(*other) {}
728
729template <typename T>
730Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
731 other.ptr = nullptr;
732}
733
734template <typename T>
David Tolnaye7d662d2020-12-12 16:38:22 -0800735Box<T>::Box(const T &val) {
736 allocation alloc;
737 ::new (alloc.ptr) T(val);
738 this->ptr = alloc.ptr;
739 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700740}
741
742template <typename T>
David Tolnaye7d662d2020-12-12 16:38:22 -0800743Box<T>::Box(T &&val) {
744 allocation alloc;
745 ::new (alloc.ptr) T(std::move(val));
746 this->ptr = alloc.ptr;
747 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700748}
749
750template <typename T>
751Box<T>::~Box() noexcept {
752 if (this->ptr) {
753 this->drop();
754 }
755}
756
757template <typename T>
758Box<T> &Box<T>::operator=(const Box &other) {
David Tolnay439ed0b2020-12-12 17:01:04 -0800759 if (this->ptr) {
760 **this = *other;
761 } else {
762 allocation alloc;
763 ::new (alloc.ptr) T(*other);
764 this->ptr = alloc.ptr;
765 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700766 }
767 return *this;
768}
769
770template <typename T>
771Box<T> &Box<T>::operator=(Box &&other) noexcept {
772 if (this->ptr) {
773 this->drop();
774 }
775 this->ptr = other.ptr;
776 other.ptr = nullptr;
777 return *this;
778}
779
780template <typename T>
781const T *Box<T>::operator->() const noexcept {
782 return this->ptr;
783}
784
785template <typename T>
786const T &Box<T>::operator*() const noexcept {
787 return *this->ptr;
788}
789
790template <typename T>
791T *Box<T>::operator->() noexcept {
792 return this->ptr;
793}
794
795template <typename T>
796T &Box<T>::operator*() noexcept {
797 return *this->ptr;
798}
799
800template <typename T>
801template <typename... Fields>
802Box<T> Box<T>::in_place(Fields &&... fields) {
David Tolnaye7d662d2020-12-12 16:38:22 -0800803 allocation alloc;
804 auto ptr = alloc.ptr;
805 ::new (ptr) T{std::forward<Fields>(fields)...};
806 alloc.ptr = nullptr;
807 return from_raw(ptr);
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700808}
809
810template <typename T>
811Box<T> Box<T>::from_raw(T *raw) noexcept {
David Tolnayc4b34222020-12-12 13:06:26 -0800812 Box box = uninit{};
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700813 box.ptr = raw;
814 return box;
815}
816
817template <typename T>
818T *Box<T>::into_raw() noexcept {
819 T *raw = this->ptr;
820 this->ptr = nullptr;
821 return raw;
822}
823
824template <typename T>
David Tolnayc4b34222020-12-12 13:06:26 -0800825Box<T>::Box(uninit) noexcept {}
David Tolnay0f0162f2020-11-16 23:43:37 -0800826#endif // CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700827
David Tolnay0f0162f2020-11-16 23:43:37 -0800828#ifndef CXXBRIDGE1_RUST_VEC
829#define CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700830template <typename T>
David Tolnayf799b372020-11-29 23:57:42 -0800831Vec<T>::Vec(std::initializer_list<T> init) : Vec{} {
832 this->reserve_total(init.size());
833 std::move(init.begin(), init.end(), std::back_inserter(*this));
834}
835
836template <typename T>
David Tolnay9007e462020-12-11 13:59:08 -0800837Vec<T>::Vec(const Vec &other) : Vec() {
838 this->reserve_total(other.size());
839 std::copy(other.begin(), other.end(), std::back_inserter(*this));
840}
841
842template <typename T>
David Tolnay15671862020-11-23 18:13:56 -0800843Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700844 new (&other) Vec();
845}
846
847template <typename T>
848Vec<T>::~Vec() noexcept {
849 this->drop();
850}
851
852template <typename T>
853Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
854 if (this != &other) {
855 this->drop();
856 this->repr = other.repr;
857 new (&other) Vec();
858 }
859 return *this;
860}
861
862template <typename T>
David Tolnaydd42c722020-12-11 14:05:26 -0800863Vec<T> &Vec<T>::operator=(const Vec &other) {
864 if (this != &other) {
865 this->drop();
866 new (this) Vec(other);
867 }
868 return *this;
869}
870
871template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700872bool Vec<T>::empty() const noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800873 return this->size() == 0;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700874}
875
876template <typename T>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800877T *Vec<T>::data() noexcept {
878 return const_cast<T *>(const_cast<const Vec<T> *>(this)->data());
879}
880
881template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800882const T &Vec<T>::operator[](std::size_t n) const noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800883 assert(n < this->size());
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700884 auto data = reinterpret_cast<const char *>(this->data());
David Tolnaye1df7dd2020-12-27 02:51:51 -0800885 return *reinterpret_cast<const T *>(data + n * size_of<T>());
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700886}
887
888template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800889const T &Vec<T>::at(std::size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700890 if (n >= this->size()) {
891 panic<std::out_of_range>("rust::Vec index out of range");
892 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700893 return (*this)[n];
894}
895
896template <typename T>
David Tolnayd4fff5d2020-12-21 14:04:09 -0800897const T &Vec<T>::front() const noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800898 assert(!this->empty());
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700899 return (*this)[0];
900}
901
902template <typename T>
David Tolnayd4fff5d2020-12-21 14:04:09 -0800903const T &Vec<T>::back() const noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800904 assert(!this->empty());
David Tolnayb10c4bc2020-08-26 21:55:29 -0700905 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700906}
907
908template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800909T &Vec<T>::operator[](std::size_t n) noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800910 assert(n < this->size());
David Tolnay908d5e52020-11-30 00:30:59 -0800911 auto data = reinterpret_cast<char *>(this->data());
David Tolnaye1df7dd2020-12-27 02:51:51 -0800912 return *reinterpret_cast<T *>(data + n * size_of<T>());
David Tolnay908d5e52020-11-30 00:30:59 -0800913}
914
915template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800916T &Vec<T>::at(std::size_t n) {
David Tolnay908d5e52020-11-30 00:30:59 -0800917 if (n >= this->size()) {
918 panic<std::out_of_range>("rust::Vec index out of range");
919 }
920 return (*this)[n];
921}
922
923template <typename T>
David Tolnayd4fff5d2020-12-21 14:04:09 -0800924T &Vec<T>::front() noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800925 assert(!this->empty());
David Tolnay908d5e52020-11-30 00:30:59 -0800926 return (*this)[0];
927}
928
929template <typename T>
David Tolnayd4fff5d2020-12-21 14:04:09 -0800930T &Vec<T>::back() noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800931 assert(!this->empty());
David Tolnay908d5e52020-11-30 00:30:59 -0800932 return (*this)[this->size() - 1];
933}
934
935template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800936void Vec<T>::reserve(std::size_t new_cap) {
David Tolnayfb6b73c2020-11-10 14:32:16 -0800937 this->reserve_total(new_cap);
938}
939
940template <typename T>
941void Vec<T>::push_back(const T &value) {
942 this->emplace_back(value);
943}
944
945template <typename T>
946void Vec<T>::push_back(T &&value) {
947 this->emplace_back(std::move(value));
948}
949
950template <typename T>
951template <typename... Args>
952void Vec<T>::emplace_back(Args &&... args) {
953 auto size = this->size();
954 this->reserve_total(size + 1);
955 ::new (reinterpret_cast<T *>(reinterpret_cast<char *>(this->data()) +
David Tolnaye1df7dd2020-12-27 02:51:51 -0800956 size * size_of<T>()))
David Tolnayfb6b73c2020-11-10 14:32:16 -0800957 T(std::forward<Args>(args)...);
958 this->set_len(size + 1);
959}
960
961template <typename T>
David Tolnay300072b2020-12-03 12:12:24 -0800962typename Vec<T>::iterator::reference
963Vec<T>::iterator::operator*() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800964 return *static_cast<T *>(this->pos);
965}
966
967template <typename T>
David Tolnay300072b2020-12-03 12:12:24 -0800968typename Vec<T>::iterator::pointer
969Vec<T>::iterator::operator->() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800970 return static_cast<T *>(this->pos);
971}
972
973template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500974typename Vec<T>::iterator::reference Vec<T>::iterator::operator[](
David Tolnay665178e2020-12-12 08:49:59 -0800975 typename Vec<T>::iterator::difference_type n) const noexcept {
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500976 auto pos = static_cast<char *>(this->pos) + this->stride * n;
977 return *static_cast<T *>(pos);
978}
979
980template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800981typename Vec<T>::iterator &Vec<T>::iterator::operator++() noexcept {
David Tolnay248215e2020-11-25 19:38:26 -0800982 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnay960b5112020-11-25 13:18:28 -0800983 return *this;
984}
985
986template <typename T>
987typename Vec<T>::iterator Vec<T>::iterator::operator++(int) noexcept {
988 auto ret = iterator(*this);
David Tolnay248215e2020-11-25 19:38:26 -0800989 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnay960b5112020-11-25 13:18:28 -0800990 return ret;
991}
992
993template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500994typename Vec<T>::iterator &Vec<T>::iterator::operator--() noexcept {
995 this->pos = static_cast<char *>(this->pos) - this->stride;
996 return *this;
997}
998
999template <typename T>
1000typename Vec<T>::iterator Vec<T>::iterator::operator--(int) noexcept {
1001 auto ret = iterator(*this);
1002 this->pos = static_cast<char *>(this->pos) - this->stride;
1003 return ret;
1004}
1005
1006template <typename T>
David Tolnay665178e2020-12-12 08:49:59 -08001007typename Vec<T>::iterator &Vec<T>::iterator::operator+=(
1008 typename Vec<T>::iterator::difference_type n) noexcept {
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001009 this->pos = static_cast<char *>(this->pos) + this->stride * n;
1010 return *this;
1011}
1012
1013template <typename T>
David Tolnay665178e2020-12-12 08:49:59 -08001014typename Vec<T>::iterator &Vec<T>::iterator::operator-=(
1015 typename Vec<T>::iterator::difference_type n) noexcept {
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001016 this->pos = static_cast<char *>(this->pos) - this->stride * n;
1017 return *this;
1018}
1019
1020template <typename T>
1021typename Vec<T>::iterator Vec<T>::iterator::operator+(
David Tolnay665178e2020-12-12 08:49:59 -08001022 typename Vec<T>::iterator::difference_type n) const noexcept {
David Tolnay300072b2020-12-03 12:12:24 -08001023 auto ret = iterator(*this);
1024 ret.pos = static_cast<char *>(this->pos) + this->stride * n;
1025 return ret;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001026}
1027
1028template <typename T>
1029typename Vec<T>::iterator Vec<T>::iterator::operator-(
David Tolnay665178e2020-12-12 08:49:59 -08001030 typename Vec<T>::iterator::difference_type n) const noexcept {
David Tolnay300072b2020-12-03 12:12:24 -08001031 auto ret = iterator(*this);
1032 ret.pos = static_cast<char *>(this->pos) - this->stride * n;
1033 return ret;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001034}
1035
1036template <typename T>
1037typename Vec<T>::iterator::difference_type
1038Vec<T>::iterator::operator-(const iterator &other) const noexcept {
1039 auto diff = std::distance(static_cast<char *>(other.pos),
1040 static_cast<char *>(this->pos));
David Tolnay300072b2020-12-03 12:12:24 -08001041 return diff / this->stride;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001042}
1043
1044template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -08001045bool Vec<T>::iterator::operator==(const iterator &other) const noexcept {
1046 return this->pos == other.pos;
1047}
1048
1049template <typename T>
1050bool Vec<T>::iterator::operator!=(const iterator &other) const noexcept {
1051 return this->pos != other.pos;
1052}
1053
1054template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001055bool Vec<T>::iterator::operator<(const iterator &other) const noexcept {
1056 return this->pos < other.pos;
1057}
1058
1059template <typename T>
David Tolnay0c9c3e62020-12-27 00:52:25 -08001060bool Vec<T>::iterator::operator<=(const iterator &other) const noexcept {
1061 return this->pos <= other.pos;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001062}
1063
1064template <typename T>
David Tolnay0c9c3e62020-12-27 00:52:25 -08001065bool Vec<T>::iterator::operator>(const iterator &other) const noexcept {
1066 return this->pos > other.pos;
1067}
1068
1069template <typename T>
1070bool Vec<T>::iterator::operator>=(const iterator &other) const noexcept {
1071 return this->pos >= other.pos;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001072}
1073
1074template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -08001075typename Vec<T>::iterator Vec<T>::begin() noexcept {
1076 iterator it;
David Tolnaya5d72c62020-11-25 13:57:16 -08001077 it.pos = const_cast<typename std::remove_const<T>::type *>(this->data());
David Tolnaye1df7dd2020-12-27 02:51:51 -08001078 it.stride = size_of<T>();
David Tolnay960b5112020-11-25 13:18:28 -08001079 return it;
1080}
1081
1082template <typename T>
1083typename Vec<T>::iterator Vec<T>::end() noexcept {
1084 iterator it = this->begin();
David Tolnay248215e2020-11-25 19:38:26 -08001085 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
David Tolnay960b5112020-11-25 13:18:28 -08001086 return it;
1087}
1088
1089template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -07001090typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -08001091 return this->cbegin();
1092}
1093
1094template <typename T>
1095typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
1096 return this->cend();
1097}
1098
1099template <typename T>
1100typename Vec<T>::const_iterator Vec<T>::cbegin() const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -07001101 const_iterator it;
David Tolnaya5d72c62020-11-25 13:57:16 -08001102 it.pos = const_cast<typename std::remove_const<T>::type *>(this->data());
David Tolnaye1df7dd2020-12-27 02:51:51 -08001103 it.stride = size_of<T>();
David Tolnay2a2b9ad2020-05-12 20:07:26 -07001104 return it;
1105}
1106
1107template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -08001108typename Vec<T>::const_iterator Vec<T>::cend() const noexcept {
1109 const_iterator it = this->cbegin();
David Tolnay248215e2020-11-25 19:38:26 -08001110 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
David Tolnay2a2b9ad2020-05-12 20:07:26 -07001111 return it;
1112}
1113
1114// Internal API only intended for the cxxbridge code generator.
1115template <typename T>
1116Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay0f0162f2020-11-16 23:43:37 -08001117#endif // CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -07001118
David Tolnay75068632020-12-26 22:15:17 -08001119#ifndef CXXBRIDGE1_IS_COMPLETE
1120#define CXXBRIDGE1_IS_COMPLETE
1121namespace detail {
1122namespace {
1123template <typename T, typename = std::size_t>
1124struct is_complete : std::false_type {};
1125template <typename T>
1126struct is_complete<T, decltype(sizeof(T))> : std::true_type {};
1127} // namespace
1128} // namespace detail
1129#endif // CXXBRIDGE1_IS_COMPLETE
1130
David Tolnayee6ecfc2020-12-26 21:54:37 -08001131#ifndef CXXBRIDGE1_LAYOUT
1132#define CXXBRIDGE1_LAYOUT
1133class layout {
1134 template <typename T>
1135 friend std::size_t size_of();
1136 template <typename T>
1137 friend std::size_t align_of();
1138 template <typename T>
1139 static typename std::enable_if<std::is_base_of<Opaque, T>::value,
1140 std::size_t>::type
1141 do_size_of() {
1142 return T::layout::size();
1143 }
1144 template <typename T>
1145 static typename std::enable_if<!std::is_base_of<Opaque, T>::value,
1146 std::size_t>::type
1147 do_size_of() {
1148 return sizeof(T);
1149 }
1150 template <typename T>
1151 static
1152 typename std::enable_if<detail::is_complete<T>::value, std::size_t>::type
1153 size_of() {
1154 return do_size_of<T>();
1155 }
1156 template <typename T>
1157 static typename std::enable_if<std::is_base_of<Opaque, T>::value,
1158 std::size_t>::type
1159 do_align_of() {
1160 return T::layout::align();
1161 }
1162 template <typename T>
1163 static typename std::enable_if<!std::is_base_of<Opaque, T>::value,
1164 std::size_t>::type
1165 do_align_of() {
1166 return alignof(T);
1167 }
1168 template <typename T>
1169 static
1170 typename std::enable_if<detail::is_complete<T>::value, std::size_t>::type
1171 align_of() {
1172 return do_align_of<T>();
1173 }
1174};
1175
1176template <typename T>
1177std::size_t size_of() {
1178 return layout::size_of<T>();
1179}
1180
1181template <typename T>
1182std::size_t align_of() {
1183 return layout::align_of<T>();
1184}
1185#endif // CXXBRIDGE1_LAYOUT
1186
David Tolnay0f0162f2020-11-16 23:43:37 -08001187#ifndef CXXBRIDGE1_RELOCATABLE
1188#define CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -08001189namespace detail {
1190template <typename... Ts>
1191struct make_void {
1192 using type = void;
1193};
1194
1195template <typename... Ts>
1196using void_t = typename make_void<Ts...>::type;
1197
1198template <typename Void, template <typename...> class, typename...>
1199struct detect : std::false_type {};
1200template <template <typename...> class T, typename... A>
1201struct detect<void_t<T<A...>>, T, A...> : std::true_type {};
1202
1203template <template <typename...> class T, typename... A>
1204using is_detected = detect<void, T, A...>;
1205
1206template <typename T>
1207using detect_IsRelocatable = typename T::IsRelocatable;
1208
1209template <typename T>
1210struct get_IsRelocatable
1211 : std::is_same<typename T::IsRelocatable, std::true_type> {};
1212} // namespace detail
1213
1214template <typename T>
1215struct IsRelocatable
1216 : std::conditional<
1217 detail::is_detected<detail::detect_IsRelocatable, T>::value,
1218 detail::get_IsRelocatable<T>,
1219 std::integral_constant<
1220 bool, std::is_trivially_move_constructible<T>::value &&
1221 std::is_trivially_destructible<T>::value>>::type {};
David Tolnay0f0162f2020-11-16 23:43:37 -08001222#endif // CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -08001223
David Tolnay0f0162f2020-11-16 23:43:37 -08001224} // namespace cxxbridge1
David Tolnay750755e2020-03-01 13:04:08 -08001225} // namespace rust