blob: 8ca1a125b18ec1d0f347a5f590a06e7d0e6c1a14 [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 Tolnayce298232020-11-11 10:08:54 -0800178 T *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 static std::size_t stride() noexcept;
317 void reserve_total(std::size_t cap) noexcept;
318 void set_len(std::size_t len) noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700319 void drop() noexcept;
320
321 // Size and alignment statically verified by rust_vec.rs.
David Tolnaybe3cbf72020-12-12 22:12:07 -0800322 std::array<std::uintptr_t, 3> repr;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700323};
David Tolnay66f216c2020-11-25 13:21:00 -0800324
325template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800326class Vec<T>::iterator final {
327public:
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500328 using iterator_category = std::random_access_iterator_tag;
David Tolnay960b5112020-11-25 13:18:28 -0800329 using value_type = T;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800330 using difference_type = std::ptrdiff_t;
David Tolnay960b5112020-11-25 13:18:28 -0800331 using pointer = typename std::add_pointer<T>::type;
332 using reference = typename std::add_lvalue_reference<T>::type;
David Tolnay960b5112020-11-25 13:18:28 -0800333
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500334 reference operator*() const noexcept;
335 pointer operator->() const noexcept;
336 reference operator[](difference_type) const noexcept;
337
David Tolnay960b5112020-11-25 13:18:28 -0800338 iterator &operator++() noexcept;
339 iterator operator++(int) noexcept;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500340 iterator &operator--() noexcept;
341 iterator operator--(int) noexcept;
342
343 iterator &operator+=(difference_type) noexcept;
344 iterator &operator-=(difference_type) noexcept;
345 iterator operator+(difference_type) const noexcept;
346 iterator operator-(difference_type) const noexcept;
347 difference_type operator-(const iterator &) const noexcept;
348
David Tolnay960b5112020-11-25 13:18:28 -0800349 bool operator==(const iterator &) const noexcept;
350 bool operator!=(const iterator &) const noexcept;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500351 bool operator<(const iterator &) const noexcept;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500352 bool operator<=(const iterator &) const noexcept;
David Tolnay0c9c3e62020-12-27 00:52:25 -0800353 bool operator>(const iterator &) const noexcept;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500354 bool operator>=(const iterator &) const noexcept;
David Tolnay960b5112020-11-25 13:18:28 -0800355
356private:
357 friend class Vec;
David Tolnaya5d72c62020-11-25 13:57:16 -0800358 friend class Vec<typename std::remove_const<T>::type>;
David Tolnay960b5112020-11-25 13:18:28 -0800359 void *pos;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800360 std::size_t stride;
David Tolnay960b5112020-11-25 13:18:28 -0800361};
David Tolnay0f0162f2020-11-16 23:43:37 -0800362#endif // CXXBRIDGE1_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700363
David Tolnay0f0162f2020-11-16 23:43:37 -0800364#ifndef CXXBRIDGE1_RUST_FN
David Tolnaye468f052020-11-29 19:39:35 -0800365// https://cxx.rs/binding/fn.html
David Tolnayf031c322020-11-29 19:41:33 -0800366template <typename Signature>
David Tolnayf262d382020-04-11 22:12:40 -0700367class Fn;
David Tolnay75dca2e2020-03-25 20:17:52 -0700368
David Tolnayf031c322020-11-29 19:41:33 -0800369template <typename Ret, typename... Args>
370class Fn<Ret(Args...)> final {
David Tolnay75dca2e2020-03-25 20:17:52 -0700371public:
David Tolnayf031c322020-11-29 19:41:33 -0800372 Ret operator()(Args... args) const noexcept;
David Tolnay533d4582020-04-08 20:29:14 -0700373 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700374
375private:
David Tolnayf031c322020-11-29 19:41:33 -0800376 Ret (*trampoline)(Args..., void *fn) noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700377 void *fn;
378};
David Tolnay0f0162f2020-11-16 23:43:37 -0800379#endif // CXXBRIDGE1_RUST_FN
David Tolnay75dca2e2020-03-25 20:17:52 -0700380
David Tolnay0f0162f2020-11-16 23:43:37 -0800381#ifndef CXXBRIDGE1_RUST_ERROR
382#define CXXBRIDGE1_RUST_ERROR
David Tolnaye468f052020-11-29 19:39:35 -0800383// https://cxx.rs/binding/result.html
David Tolnaye4fa8732020-09-08 15:04:56 -0700384class Error final : public std::exception {
David Tolnay1e548172020-03-16 13:37:09 -0700385public:
386 Error(const Error &);
387 Error(Error &&) noexcept;
David Tolnay2714d2c2020-11-23 18:17:43 -0800388 ~Error() noexcept override;
David Tolnay7c6ac712020-10-31 17:22:28 -0700389
390 Error &operator=(const Error &);
David Tolnay15491062020-10-31 17:25:13 -0700391 Error &operator=(Error &&) noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700392
David Tolnay1e548172020-03-16 13:37:09 -0700393 const char *what() const noexcept override;
394
395private:
David Tolnaya0c9bc72020-10-31 14:37:14 -0700396 Error() noexcept = default;
David Tolnay84ddf9e2020-10-31 15:36:48 -0700397 friend impl<Error>;
David Tolnaya0c9bc72020-10-31 14:37:14 -0700398 const char *msg;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800399 std::size_t len;
David Tolnay1e548172020-03-16 13:37:09 -0700400};
David Tolnay0f0162f2020-11-16 23:43:37 -0800401#endif // CXXBRIDGE1_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700402
David Tolnay0f0162f2020-11-16 23:43:37 -0800403#ifndef CXXBRIDGE1_RUST_ISIZE
404#define CXXBRIDGE1_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700405#if defined(_WIN32)
406using isize = SSIZE_T;
407#else
408using isize = ssize_t;
409#endif
David Tolnay0f0162f2020-11-16 23:43:37 -0800410#endif // CXXBRIDGE1_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700411
David Tolnay851677c2020-03-01 23:49:46 -0800412std::ostream &operator<<(std::ostream &, const String &);
413std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400414
David Tolnay365fc7c2020-11-25 16:08:13 -0800415#ifndef CXXBRIDGE1_RUST_OPAQUE
416#define CXXBRIDGE1_RUST_OPAQUE
417// Base class of generated opaque Rust types.
418class Opaque {
David Tolnaya857c322020-11-25 16:27:19 -0800419public:
David Tolnay365fc7c2020-11-25 16:08:13 -0800420 Opaque() = delete;
421 Opaque(const Opaque &) = delete;
422 ~Opaque() = delete;
423};
424#endif // CXXBRIDGE1_RUST_OPAQUE
425
David Tolnayee6ecfc2020-12-26 21:54:37 -0800426template <typename T>
427std::size_t size_of();
428template <typename T>
429std::size_t align_of();
430
David Tolnay174bd952020-11-02 09:23:12 -0800431// IsRelocatable<T> is used in assertions that a C++ type passed by value
432// between Rust and C++ is soundly relocatable by Rust.
433//
434// There may be legitimate reasons to opt out of the check for support of types
435// that the programmer knows are soundly Rust-movable despite not being
436// recognized as such by the C++ type system due to a move constructor or
437// destructor. To opt out of the relocatability check, do either of the
438// following things in any header used by `include!` in the bridge.
439//
440// --- if you define the type:
441// struct MyType {
442// ...
443// + using IsRelocatable = std::true_type;
444// };
445//
446// --- otherwise:
447// + template <>
448// + struct rust::IsRelocatable<MyType> : std::true_type {};
449template <typename T>
450struct IsRelocatable;
451
David Tolnaybe3cbf72020-12-12 22:12:07 -0800452using u8 = std::uint8_t;
453using u16 = std::uint16_t;
454using u32 = std::uint32_t;
455using u64 = std::uint64_t;
456using usize = std::size_t; // see static asserts in cxx.cc
457using i8 = std::int8_t;
458using i16 = std::int16_t;
459using i32 = std::int32_t;
460using i64 = std::int64_t;
David Tolnay1e784a32020-12-12 21:34:47 -0800461using f32 = float;
462using f64 = double;
463
David Tolnay3b0c9882020-03-01 14:08:57 -0800464// Snake case aliases for use in code that uses this style for type names.
465using string = String;
466using str = Str;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800467template <typename T>
David Tolnay38c87642020-09-06 22:18:08 -0700468using slice = Slice<T>;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800469template <typename T>
David Tolnayf262d382020-04-11 22:12:40 -0700470using box = Box<T>;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800471template <typename T>
David Tolnay38c87642020-09-06 22:18:08 -0700472using vec = Vec<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700473using error = Error;
David Tolnayf262d382020-04-11 22:12:40 -0700474template <typename Signature>
David Tolnayf031c322020-11-29 19:41:33 -0800475using fn = Fn<Signature>;
David Tolnay174bd952020-11-02 09:23:12 -0800476template <typename T>
477using is_relocatable = IsRelocatable<T>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800478
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700479
480
481////////////////////////////////////////////////////////////////////////////////
482/// end public API, begin implementation details
483
David Tolnay0f0162f2020-11-16 23:43:37 -0800484#ifndef CXXBRIDGE1_PANIC
485#define CXXBRIDGE1_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700486template <typename Exception>
487void panic [[noreturn]] (const char *msg);
David Tolnay0f0162f2020-11-16 23:43:37 -0800488#endif // CXXBRIDGE1_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700489
David Tolnay0f0162f2020-11-16 23:43:37 -0800490#ifndef CXXBRIDGE1_RUST_FN
491#define CXXBRIDGE1_RUST_FN
David Tolnayf031c322020-11-29 19:41:33 -0800492template <typename Ret, typename... Args>
493Ret Fn<Ret(Args...)>::operator()(Args... args) const noexcept {
David Tolnay75dca2e2020-03-25 20:17:52 -0700494 return (*this->trampoline)(std::move(args)..., this->fn);
495}
496
David Tolnayf031c322020-11-29 19:41:33 -0800497template <typename Ret, typename... Args>
498Fn<Ret(Args...)> Fn<Ret(Args...)>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700499 return *this;
500}
David Tolnay0f0162f2020-11-16 23:43:37 -0800501#endif // CXXBRIDGE1_RUST_FN
David Tolnaya23129c2020-04-08 20:08:21 -0700502
David Tolnay0f0162f2020-11-16 23:43:37 -0800503#ifndef CXXBRIDGE1_RUST_BITCOPY
504#define CXXBRIDGE1_RUST_BITCOPY
David Tolnay48521222020-10-31 14:59:42 -0700505struct unsafe_bitcopy_t final {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700506 explicit unsafe_bitcopy_t() = default;
507};
508
509constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnay0f0162f2020-11-16 23:43:37 -0800510#endif // CXXBRIDGE1_RUST_BITCOPY
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700511
David Tolnay0f0162f2020-11-16 23:43:37 -0800512#ifndef CXXBRIDGE1_RUST_STR
513#define CXXBRIDGE1_RUST_STR
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700514inline const char *Str::data() const noexcept { return this->ptr; }
515
David Tolnaybe3cbf72020-12-12 22:12:07 -0800516inline std::size_t Str::size() const noexcept { return this->len; }
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700517
David Tolnaybe3cbf72020-12-12 22:12:07 -0800518inline std::size_t Str::length() const noexcept { return this->len; }
David Tolnay0f0162f2020-11-16 23:43:37 -0800519#endif // CXXBRIDGE1_RUST_STR
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700520
David Tolnay0f0162f2020-11-16 23:43:37 -0800521#ifndef CXXBRIDGE1_RUST_SLICE
522#define CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700523template <typename T>
David Tolnay7b16a392020-11-25 20:23:10 -0800524Slice<T>::Slice() noexcept : ptr(reinterpret_cast<T *>(alignof(T))), len(0) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700525
526template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800527Slice<T>::Slice(T *s, std::size_t count) noexcept : ptr(s), len(count) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700528
529template <typename T>
David Tolnayce298232020-11-11 10:08:54 -0800530T *Slice<T>::data() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700531 return this->ptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700532}
533
534template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800535std::size_t Slice<T>::size() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700536 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700537}
538
539template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800540std::size_t Slice<T>::length() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700541 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700542}
David Tolnayac6cb542020-11-25 20:18:55 -0800543
544template <typename T>
David Tolnay78dd5352020-12-26 23:44:20 -0800545bool Slice<T>::empty() const noexcept {
546 return this->len == 0;
547}
548
549template <typename T>
550T &Slice<T>::operator[](std::size_t n) const noexcept {
551 assert(n < this->size());
552 return this->ptr[n];
553}
554
555template <typename T>
556T &Slice<T>::at(std::size_t n) const {
557 if (n >= this->size()) {
558 panic<std::out_of_range>("rust::Slice index out of range");
559 }
560 return (*this)[n];
561}
562
563template <typename T>
564T &Slice<T>::front() const noexcept {
565 assert(!this->empty());
566 return this->ptr[0];
567}
568
569template <typename T>
570T &Slice<T>::back() const noexcept {
571 assert(!this->empty());
572 return this->ptr[this->len - 1];
573}
574
575template <typename T>
David Tolnaydb388c92020-12-27 00:28:26 -0800576typename Slice<T>::iterator::reference
577Slice<T>::iterator::operator*() const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800578 return *static_cast<T *>(this->pos);
David Tolnayac6cb542020-11-25 20:18:55 -0800579}
580
581template <typename T>
David Tolnaydb388c92020-12-27 00:28:26 -0800582typename Slice<T>::iterator::pointer
583Slice<T>::iterator::operator->() const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800584 return static_cast<T *>(this->pos);
David Tolnayac6cb542020-11-25 20:18:55 -0800585}
586
587template <typename T>
David Tolnayc30e4472020-12-27 00:25:42 -0800588typename Slice<T>::iterator::reference Slice<T>::iterator::operator[](
589 typename Slice<T>::iterator::difference_type n) const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800590 auto pos = static_cast<char *>(this->pos) + this->stride * n;
591 return *static_cast<T *>(pos);
David Tolnayc30e4472020-12-27 00:25:42 -0800592}
593
594template <typename T>
David Tolnayac6cb542020-11-25 20:18:55 -0800595typename Slice<T>::iterator &Slice<T>::iterator::operator++() noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800596 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnayac6cb542020-11-25 20:18:55 -0800597 return *this;
598}
599
600template <typename T>
601typename Slice<T>::iterator Slice<T>::iterator::operator++(int) noexcept {
602 auto ret = iterator(*this);
David Tolnaya1ddbf82020-12-27 00:56:35 -0800603 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnayac6cb542020-11-25 20:18:55 -0800604 return ret;
605}
606
607template <typename T>
David Tolnayc30e4472020-12-27 00:25:42 -0800608typename Slice<T>::iterator &Slice<T>::iterator::operator--() noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800609 this->pos = static_cast<char *>(this->pos) - this->stride;
David Tolnayc30e4472020-12-27 00:25:42 -0800610 return *this;
611}
612
613template <typename T>
614typename Slice<T>::iterator Slice<T>::iterator::operator--(int) noexcept {
615 auto ret = iterator(*this);
David Tolnaya1ddbf82020-12-27 00:56:35 -0800616 this->pos = static_cast<char *>(this->pos) - this->stride;
David Tolnayc30e4472020-12-27 00:25:42 -0800617 return ret;
618}
619
620template <typename T>
621typename Slice<T>::iterator &Slice<T>::iterator::operator+=(
622 typename Slice<T>::iterator::difference_type n) noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800623 this->pos = static_cast<char *>(this->pos) + this->stride * n;
David Tolnayc30e4472020-12-27 00:25:42 -0800624 return *this;
625}
626
627template <typename T>
628typename Slice<T>::iterator &Slice<T>::iterator::operator-=(
629 typename Slice<T>::iterator::difference_type n) noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800630 this->pos = static_cast<char *>(this->pos) - this->stride * n;
David Tolnayc30e4472020-12-27 00:25:42 -0800631 return *this;
632}
633
634template <typename T>
635typename Slice<T>::iterator Slice<T>::iterator::operator+(
636 typename Slice<T>::iterator::difference_type n) const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800637 auto ret = iterator(*this);
638 ret.pos = static_cast<char *>(this->pos) + this->stride * n;
639 return ret;
David Tolnayc30e4472020-12-27 00:25:42 -0800640}
641
642template <typename T>
643typename Slice<T>::iterator Slice<T>::iterator::operator-(
644 typename Slice<T>::iterator::difference_type n) const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800645 auto ret = iterator(*this);
646 ret.pos = static_cast<char *>(this->pos) - this->stride * n;
647 return ret;
David Tolnayc30e4472020-12-27 00:25:42 -0800648}
649
650template <typename T>
651typename Slice<T>::iterator::difference_type
652Slice<T>::iterator::operator-(const iterator &other) const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800653 auto diff = std::distance(static_cast<char *>(other.pos),
654 static_cast<char *>(this->pos));
655 return diff / this->stride;
David Tolnayc30e4472020-12-27 00:25:42 -0800656}
657
658template <typename T>
David Tolnayac6cb542020-11-25 20:18:55 -0800659bool Slice<T>::iterator::operator==(const iterator &other) const noexcept {
660 return this->pos == other.pos;
661}
662
663template <typename T>
664bool Slice<T>::iterator::operator!=(const iterator &other) const noexcept {
665 return this->pos != other.pos;
666}
667
668template <typename T>
David Tolnayc30e4472020-12-27 00:25:42 -0800669bool Slice<T>::iterator::operator<(const iterator &other) const noexcept {
670 return this->pos < other.pos;
671}
672
673template <typename T>
674bool Slice<T>::iterator::operator<=(const iterator &other) const noexcept {
675 return this->pos <= other.pos;
676}
677
678template <typename T>
679bool Slice<T>::iterator::operator>(const iterator &other) const noexcept {
680 return this->pos > other.pos;
681}
682
683template <typename T>
684bool Slice<T>::iterator::operator>=(const iterator &other) const noexcept {
685 return this->pos >= other.pos;
686}
687
688template <typename T>
David Tolnayac6cb542020-11-25 20:18:55 -0800689typename Slice<T>::iterator Slice<T>::begin() const noexcept {
690 iterator it;
David Tolnaya1ddbf82020-12-27 00:56:35 -0800691 it.pos = const_cast<typename std::remove_const<T>::type *>(this->ptr);
David Tolnay6f92baa2020-12-27 01:09:26 -0800692 it.stride = size_of<T>();
David Tolnayac6cb542020-11-25 20:18:55 -0800693 return it;
694}
695
696template <typename T>
697typename Slice<T>::iterator Slice<T>::end() const noexcept {
698 iterator it = this->begin();
David Tolnaya1ddbf82020-12-27 00:56:35 -0800699 it.pos = static_cast<char *>(it.pos) + it.stride * this->len;
David Tolnayac6cb542020-11-25 20:18:55 -0800700 return it;
701}
David Tolnay0f0162f2020-11-16 23:43:37 -0800702#endif // CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700703
David Tolnay0f0162f2020-11-16 23:43:37 -0800704#ifndef CXXBRIDGE1_RUST_BOX
705#define CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700706template <typename T>
David Tolnayc4b34222020-12-12 13:06:26 -0800707class Box<T>::uninit {};
708
709template <typename T>
David Tolnaye5703162020-12-12 16:26:35 -0800710class Box<T>::allocation {
David Tolnayf448e202020-12-12 16:42:45 -0800711 static T *alloc() noexcept;
712 static void dealloc(T *) noexcept;
David Tolnay2e637d92020-12-12 21:08:01 -0800713
David Tolnaye5703162020-12-12 16:26:35 -0800714public:
David Tolnaye7d662d2020-12-12 16:38:22 -0800715 allocation() noexcept : ptr(alloc()) {}
716 ~allocation() noexcept {
717 if (this->ptr) {
718 dealloc(this->ptr);
719 }
720 }
David Tolnaye5703162020-12-12 16:26:35 -0800721 T *ptr;
722};
723
724template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700725Box<T>::Box(const Box &other) : Box(*other) {}
726
727template <typename T>
728Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
729 other.ptr = nullptr;
730}
731
732template <typename T>
David Tolnaye7d662d2020-12-12 16:38:22 -0800733Box<T>::Box(const T &val) {
734 allocation alloc;
735 ::new (alloc.ptr) T(val);
736 this->ptr = alloc.ptr;
737 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700738}
739
740template <typename T>
David Tolnaye7d662d2020-12-12 16:38:22 -0800741Box<T>::Box(T &&val) {
742 allocation alloc;
743 ::new (alloc.ptr) T(std::move(val));
744 this->ptr = alloc.ptr;
745 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700746}
747
748template <typename T>
749Box<T>::~Box() noexcept {
750 if (this->ptr) {
751 this->drop();
752 }
753}
754
755template <typename T>
756Box<T> &Box<T>::operator=(const Box &other) {
David Tolnay439ed0b2020-12-12 17:01:04 -0800757 if (this->ptr) {
758 **this = *other;
759 } else {
760 allocation alloc;
761 ::new (alloc.ptr) T(*other);
762 this->ptr = alloc.ptr;
763 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700764 }
765 return *this;
766}
767
768template <typename T>
769Box<T> &Box<T>::operator=(Box &&other) noexcept {
770 if (this->ptr) {
771 this->drop();
772 }
773 this->ptr = other.ptr;
774 other.ptr = nullptr;
775 return *this;
776}
777
778template <typename T>
779const T *Box<T>::operator->() const noexcept {
780 return this->ptr;
781}
782
783template <typename T>
784const T &Box<T>::operator*() const noexcept {
785 return *this->ptr;
786}
787
788template <typename T>
789T *Box<T>::operator->() noexcept {
790 return this->ptr;
791}
792
793template <typename T>
794T &Box<T>::operator*() noexcept {
795 return *this->ptr;
796}
797
798template <typename T>
799template <typename... Fields>
800Box<T> Box<T>::in_place(Fields &&... fields) {
David Tolnaye7d662d2020-12-12 16:38:22 -0800801 allocation alloc;
802 auto ptr = alloc.ptr;
803 ::new (ptr) T{std::forward<Fields>(fields)...};
804 alloc.ptr = nullptr;
805 return from_raw(ptr);
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700806}
807
808template <typename T>
809Box<T> Box<T>::from_raw(T *raw) noexcept {
David Tolnayc4b34222020-12-12 13:06:26 -0800810 Box box = uninit{};
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700811 box.ptr = raw;
812 return box;
813}
814
815template <typename T>
816T *Box<T>::into_raw() noexcept {
817 T *raw = this->ptr;
818 this->ptr = nullptr;
819 return raw;
820}
821
822template <typename T>
David Tolnayc4b34222020-12-12 13:06:26 -0800823Box<T>::Box(uninit) noexcept {}
David Tolnay0f0162f2020-11-16 23:43:37 -0800824#endif // CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700825
David Tolnay0f0162f2020-11-16 23:43:37 -0800826#ifndef CXXBRIDGE1_RUST_VEC
827#define CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700828template <typename T>
David Tolnayf799b372020-11-29 23:57:42 -0800829Vec<T>::Vec(std::initializer_list<T> init) : Vec{} {
830 this->reserve_total(init.size());
831 std::move(init.begin(), init.end(), std::back_inserter(*this));
832}
833
834template <typename T>
David Tolnay9007e462020-12-11 13:59:08 -0800835Vec<T>::Vec(const Vec &other) : Vec() {
836 this->reserve_total(other.size());
837 std::copy(other.begin(), other.end(), std::back_inserter(*this));
838}
839
840template <typename T>
David Tolnay15671862020-11-23 18:13:56 -0800841Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700842 new (&other) Vec();
843}
844
845template <typename T>
846Vec<T>::~Vec() noexcept {
847 this->drop();
848}
849
850template <typename T>
851Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
852 if (this != &other) {
853 this->drop();
854 this->repr = other.repr;
855 new (&other) Vec();
856 }
857 return *this;
858}
859
860template <typename T>
David Tolnaydd42c722020-12-11 14:05:26 -0800861Vec<T> &Vec<T>::operator=(const Vec &other) {
862 if (this != &other) {
863 this->drop();
864 new (this) Vec(other);
865 }
866 return *this;
867}
868
869template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700870bool Vec<T>::empty() const noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800871 return this->size() == 0;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700872}
873
874template <typename T>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800875T *Vec<T>::data() noexcept {
876 return const_cast<T *>(const_cast<const Vec<T> *>(this)->data());
877}
878
879template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800880const T &Vec<T>::operator[](std::size_t n) const noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800881 assert(n < this->size());
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700882 auto data = reinterpret_cast<const char *>(this->data());
883 return *reinterpret_cast<const T *>(data + n * this->stride());
884}
885
886template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800887const T &Vec<T>::at(std::size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700888 if (n >= this->size()) {
889 panic<std::out_of_range>("rust::Vec index out of range");
890 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700891 return (*this)[n];
892}
893
894template <typename T>
David Tolnayd4fff5d2020-12-21 14:04:09 -0800895const T &Vec<T>::front() const noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800896 assert(!this->empty());
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700897 return (*this)[0];
898}
899
900template <typename T>
David Tolnayd4fff5d2020-12-21 14:04:09 -0800901const T &Vec<T>::back() const noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800902 assert(!this->empty());
David Tolnayb10c4bc2020-08-26 21:55:29 -0700903 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700904}
905
906template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800907T &Vec<T>::operator[](std::size_t n) noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800908 assert(n < this->size());
David Tolnay908d5e52020-11-30 00:30:59 -0800909 auto data = reinterpret_cast<char *>(this->data());
910 return *reinterpret_cast<T *>(data + n * this->stride());
911}
912
913template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800914T &Vec<T>::at(std::size_t n) {
David Tolnay908d5e52020-11-30 00:30:59 -0800915 if (n >= this->size()) {
916 panic<std::out_of_range>("rust::Vec index out of range");
917 }
918 return (*this)[n];
919}
920
921template <typename T>
David Tolnayd4fff5d2020-12-21 14:04:09 -0800922T &Vec<T>::front() noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800923 assert(!this->empty());
David Tolnay908d5e52020-11-30 00:30:59 -0800924 return (*this)[0];
925}
926
927template <typename T>
David Tolnayd4fff5d2020-12-21 14:04:09 -0800928T &Vec<T>::back() noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800929 assert(!this->empty());
David Tolnay908d5e52020-11-30 00:30:59 -0800930 return (*this)[this->size() - 1];
931}
932
933template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800934void Vec<T>::reserve(std::size_t new_cap) {
David Tolnayfb6b73c2020-11-10 14:32:16 -0800935 this->reserve_total(new_cap);
936}
937
938template <typename T>
939void Vec<T>::push_back(const T &value) {
940 this->emplace_back(value);
941}
942
943template <typename T>
944void Vec<T>::push_back(T &&value) {
945 this->emplace_back(std::move(value));
946}
947
948template <typename T>
949template <typename... Args>
950void Vec<T>::emplace_back(Args &&... args) {
951 auto size = this->size();
952 this->reserve_total(size + 1);
953 ::new (reinterpret_cast<T *>(reinterpret_cast<char *>(this->data()) +
954 size * this->stride()))
955 T(std::forward<Args>(args)...);
956 this->set_len(size + 1);
957}
958
959template <typename T>
David Tolnay300072b2020-12-03 12:12:24 -0800960typename Vec<T>::iterator::reference
961Vec<T>::iterator::operator*() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800962 return *static_cast<T *>(this->pos);
963}
964
965template <typename T>
David Tolnay300072b2020-12-03 12:12:24 -0800966typename Vec<T>::iterator::pointer
967Vec<T>::iterator::operator->() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800968 return static_cast<T *>(this->pos);
969}
970
971template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500972typename Vec<T>::iterator::reference Vec<T>::iterator::operator[](
David Tolnay665178e2020-12-12 08:49:59 -0800973 typename Vec<T>::iterator::difference_type n) const noexcept {
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500974 auto pos = static_cast<char *>(this->pos) + this->stride * n;
975 return *static_cast<T *>(pos);
976}
977
978template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800979typename Vec<T>::iterator &Vec<T>::iterator::operator++() noexcept {
David Tolnay248215e2020-11-25 19:38:26 -0800980 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnay960b5112020-11-25 13:18:28 -0800981 return *this;
982}
983
984template <typename T>
985typename Vec<T>::iterator Vec<T>::iterator::operator++(int) noexcept {
986 auto ret = iterator(*this);
David Tolnay248215e2020-11-25 19:38:26 -0800987 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnay960b5112020-11-25 13:18:28 -0800988 return ret;
989}
990
991template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500992typename Vec<T>::iterator &Vec<T>::iterator::operator--() noexcept {
993 this->pos = static_cast<char *>(this->pos) - this->stride;
994 return *this;
995}
996
997template <typename T>
998typename Vec<T>::iterator Vec<T>::iterator::operator--(int) noexcept {
999 auto ret = iterator(*this);
1000 this->pos = static_cast<char *>(this->pos) - this->stride;
1001 return ret;
1002}
1003
1004template <typename T>
David Tolnay665178e2020-12-12 08:49:59 -08001005typename Vec<T>::iterator &Vec<T>::iterator::operator+=(
1006 typename Vec<T>::iterator::difference_type n) noexcept {
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001007 this->pos = static_cast<char *>(this->pos) + this->stride * n;
1008 return *this;
1009}
1010
1011template <typename T>
David Tolnay665178e2020-12-12 08:49:59 -08001012typename Vec<T>::iterator &Vec<T>::iterator::operator-=(
1013 typename Vec<T>::iterator::difference_type n) noexcept {
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001014 this->pos = static_cast<char *>(this->pos) - this->stride * n;
1015 return *this;
1016}
1017
1018template <typename T>
1019typename Vec<T>::iterator Vec<T>::iterator::operator+(
David Tolnay665178e2020-12-12 08:49:59 -08001020 typename Vec<T>::iterator::difference_type n) const noexcept {
David Tolnay300072b2020-12-03 12:12:24 -08001021 auto ret = iterator(*this);
1022 ret.pos = static_cast<char *>(this->pos) + this->stride * n;
1023 return ret;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001024}
1025
1026template <typename T>
1027typename Vec<T>::iterator Vec<T>::iterator::operator-(
David Tolnay665178e2020-12-12 08:49:59 -08001028 typename Vec<T>::iterator::difference_type n) const noexcept {
David Tolnay300072b2020-12-03 12:12:24 -08001029 auto ret = iterator(*this);
1030 ret.pos = static_cast<char *>(this->pos) - this->stride * n;
1031 return ret;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001032}
1033
1034template <typename T>
1035typename Vec<T>::iterator::difference_type
1036Vec<T>::iterator::operator-(const iterator &other) const noexcept {
1037 auto diff = std::distance(static_cast<char *>(other.pos),
1038 static_cast<char *>(this->pos));
David Tolnay300072b2020-12-03 12:12:24 -08001039 return diff / this->stride;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001040}
1041
1042template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -08001043bool Vec<T>::iterator::operator==(const iterator &other) const noexcept {
1044 return this->pos == other.pos;
1045}
1046
1047template <typename T>
1048bool Vec<T>::iterator::operator!=(const iterator &other) const noexcept {
1049 return this->pos != other.pos;
1050}
1051
1052template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001053bool Vec<T>::iterator::operator<(const iterator &other) const noexcept {
1054 return this->pos < other.pos;
1055}
1056
1057template <typename T>
David Tolnay0c9c3e62020-12-27 00:52:25 -08001058bool Vec<T>::iterator::operator<=(const iterator &other) const noexcept {
1059 return this->pos <= other.pos;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001060}
1061
1062template <typename T>
David Tolnay0c9c3e62020-12-27 00:52:25 -08001063bool Vec<T>::iterator::operator>(const iterator &other) const noexcept {
1064 return this->pos > other.pos;
1065}
1066
1067template <typename T>
1068bool Vec<T>::iterator::operator>=(const iterator &other) const noexcept {
1069 return this->pos >= other.pos;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001070}
1071
1072template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -08001073typename Vec<T>::iterator Vec<T>::begin() noexcept {
1074 iterator it;
David Tolnaya5d72c62020-11-25 13:57:16 -08001075 it.pos = const_cast<typename std::remove_const<T>::type *>(this->data());
David Tolnay960b5112020-11-25 13:18:28 -08001076 it.stride = this->stride();
1077 return it;
1078}
1079
1080template <typename T>
1081typename Vec<T>::iterator Vec<T>::end() noexcept {
1082 iterator it = this->begin();
David Tolnay248215e2020-11-25 19:38:26 -08001083 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
David Tolnay960b5112020-11-25 13:18:28 -08001084 return it;
1085}
1086
1087template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -07001088typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -08001089 return this->cbegin();
1090}
1091
1092template <typename T>
1093typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
1094 return this->cend();
1095}
1096
1097template <typename T>
1098typename Vec<T>::const_iterator Vec<T>::cbegin() const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -07001099 const_iterator it;
David Tolnaya5d72c62020-11-25 13:57:16 -08001100 it.pos = const_cast<typename std::remove_const<T>::type *>(this->data());
David Tolnay2a2b9ad2020-05-12 20:07:26 -07001101 it.stride = this->stride();
1102 return it;
1103}
1104
1105template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -08001106typename Vec<T>::const_iterator Vec<T>::cend() const noexcept {
1107 const_iterator it = this->cbegin();
David Tolnay248215e2020-11-25 19:38:26 -08001108 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
David Tolnay2a2b9ad2020-05-12 20:07:26 -07001109 return it;
1110}
1111
1112// Internal API only intended for the cxxbridge code generator.
1113template <typename T>
1114Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay0f0162f2020-11-16 23:43:37 -08001115#endif // CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -07001116
David Tolnay75068632020-12-26 22:15:17 -08001117#ifndef CXXBRIDGE1_IS_COMPLETE
1118#define CXXBRIDGE1_IS_COMPLETE
1119namespace detail {
1120namespace {
1121template <typename T, typename = std::size_t>
1122struct is_complete : std::false_type {};
1123template <typename T>
1124struct is_complete<T, decltype(sizeof(T))> : std::true_type {};
1125} // namespace
1126} // namespace detail
1127#endif // CXXBRIDGE1_IS_COMPLETE
1128
David Tolnayee6ecfc2020-12-26 21:54:37 -08001129#ifndef CXXBRIDGE1_LAYOUT
1130#define CXXBRIDGE1_LAYOUT
1131class layout {
1132 template <typename T>
1133 friend std::size_t size_of();
1134 template <typename T>
1135 friend std::size_t align_of();
1136 template <typename T>
1137 static typename std::enable_if<std::is_base_of<Opaque, T>::value,
1138 std::size_t>::type
1139 do_size_of() {
1140 return T::layout::size();
1141 }
1142 template <typename T>
1143 static typename std::enable_if<!std::is_base_of<Opaque, T>::value,
1144 std::size_t>::type
1145 do_size_of() {
1146 return sizeof(T);
1147 }
1148 template <typename T>
1149 static
1150 typename std::enable_if<detail::is_complete<T>::value, std::size_t>::type
1151 size_of() {
1152 return do_size_of<T>();
1153 }
1154 template <typename T>
1155 static typename std::enable_if<std::is_base_of<Opaque, T>::value,
1156 std::size_t>::type
1157 do_align_of() {
1158 return T::layout::align();
1159 }
1160 template <typename T>
1161 static typename std::enable_if<!std::is_base_of<Opaque, T>::value,
1162 std::size_t>::type
1163 do_align_of() {
1164 return alignof(T);
1165 }
1166 template <typename T>
1167 static
1168 typename std::enable_if<detail::is_complete<T>::value, std::size_t>::type
1169 align_of() {
1170 return do_align_of<T>();
1171 }
1172};
1173
1174template <typename T>
1175std::size_t size_of() {
1176 return layout::size_of<T>();
1177}
1178
1179template <typename T>
1180std::size_t align_of() {
1181 return layout::align_of<T>();
1182}
1183#endif // CXXBRIDGE1_LAYOUT
1184
David Tolnay0f0162f2020-11-16 23:43:37 -08001185#ifndef CXXBRIDGE1_RELOCATABLE
1186#define CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -08001187namespace detail {
1188template <typename... Ts>
1189struct make_void {
1190 using type = void;
1191};
1192
1193template <typename... Ts>
1194using void_t = typename make_void<Ts...>::type;
1195
1196template <typename Void, template <typename...> class, typename...>
1197struct detect : std::false_type {};
1198template <template <typename...> class T, typename... A>
1199struct detect<void_t<T<A...>>, T, A...> : std::true_type {};
1200
1201template <template <typename...> class T, typename... A>
1202using is_detected = detect<void, T, A...>;
1203
1204template <typename T>
1205using detect_IsRelocatable = typename T::IsRelocatable;
1206
1207template <typename T>
1208struct get_IsRelocatable
1209 : std::is_same<typename T::IsRelocatable, std::true_type> {};
1210} // namespace detail
1211
1212template <typename T>
1213struct IsRelocatable
1214 : std::conditional<
1215 detail::is_detected<detail::detect_IsRelocatable, T>::value,
1216 detail::get_IsRelocatable<T>,
1217 std::integral_constant<
1218 bool, std::is_trivially_move_constructible<T>::value &&
1219 std::is_trivially_destructible<T>::value>>::type {};
David Tolnay0f0162f2020-11-16 23:43:37 -08001220#endif // CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -08001221
David Tolnay0f0162f2020-11-16 23:43:37 -08001222} // namespace cxxbridge1
David Tolnay750755e2020-03-01 13:04:08 -08001223} // namespace rust