blob: d7959184e5f0b848f0e1e9787697f2f009cdfcbf [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 Tolnayeee622c2020-12-27 01:26:17 -0800524Slice<T>::Slice() noexcept
525 : ptr(reinterpret_cast<T *>(align_of<T>())), len(0) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700526
527template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800528Slice<T>::Slice(T *s, std::size_t count) noexcept : ptr(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 Tolnay36aa9e02020-10-31 23:08:21 -0700532 return 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());
553 return this->ptr[n];
554}
555
556template <typename T>
557T &Slice<T>::at(std::size_t n) const {
558 if (n >= this->size()) {
559 panic<std::out_of_range>("rust::Slice index out of range");
560 }
561 return (*this)[n];
562}
563
564template <typename T>
565T &Slice<T>::front() const noexcept {
566 assert(!this->empty());
567 return this->ptr[0];
568}
569
570template <typename T>
571T &Slice<T>::back() const noexcept {
572 assert(!this->empty());
573 return this->ptr[this->len - 1];
574}
575
576template <typename T>
David Tolnaydb388c92020-12-27 00:28:26 -0800577typename Slice<T>::iterator::reference
578Slice<T>::iterator::operator*() const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800579 return *static_cast<T *>(this->pos);
David Tolnayac6cb542020-11-25 20:18:55 -0800580}
581
582template <typename T>
David Tolnaydb388c92020-12-27 00:28:26 -0800583typename Slice<T>::iterator::pointer
584Slice<T>::iterator::operator->() const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800585 return static_cast<T *>(this->pos);
David Tolnayac6cb542020-11-25 20:18:55 -0800586}
587
588template <typename T>
David Tolnayc30e4472020-12-27 00:25:42 -0800589typename Slice<T>::iterator::reference Slice<T>::iterator::operator[](
590 typename Slice<T>::iterator::difference_type n) const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800591 auto pos = static_cast<char *>(this->pos) + this->stride * n;
592 return *static_cast<T *>(pos);
David Tolnayc30e4472020-12-27 00:25:42 -0800593}
594
595template <typename T>
David Tolnayac6cb542020-11-25 20:18:55 -0800596typename Slice<T>::iterator &Slice<T>::iterator::operator++() noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800597 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnayac6cb542020-11-25 20:18:55 -0800598 return *this;
599}
600
601template <typename T>
602typename Slice<T>::iterator Slice<T>::iterator::operator++(int) noexcept {
603 auto ret = iterator(*this);
David Tolnaya1ddbf82020-12-27 00:56:35 -0800604 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnayac6cb542020-11-25 20:18:55 -0800605 return ret;
606}
607
608template <typename T>
David Tolnayc30e4472020-12-27 00:25:42 -0800609typename Slice<T>::iterator &Slice<T>::iterator::operator--() noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800610 this->pos = static_cast<char *>(this->pos) - this->stride;
David Tolnayc30e4472020-12-27 00:25:42 -0800611 return *this;
612}
613
614template <typename T>
615typename Slice<T>::iterator Slice<T>::iterator::operator--(int) noexcept {
616 auto ret = iterator(*this);
David Tolnaya1ddbf82020-12-27 00:56:35 -0800617 this->pos = static_cast<char *>(this->pos) - this->stride;
David Tolnayc30e4472020-12-27 00:25:42 -0800618 return ret;
619}
620
621template <typename T>
622typename Slice<T>::iterator &Slice<T>::iterator::operator+=(
623 typename Slice<T>::iterator::difference_type n) noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800624 this->pos = static_cast<char *>(this->pos) + this->stride * n;
David Tolnayc30e4472020-12-27 00:25:42 -0800625 return *this;
626}
627
628template <typename T>
629typename Slice<T>::iterator &Slice<T>::iterator::operator-=(
630 typename Slice<T>::iterator::difference_type n) noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800631 this->pos = static_cast<char *>(this->pos) - this->stride * n;
David Tolnayc30e4472020-12-27 00:25:42 -0800632 return *this;
633}
634
635template <typename T>
636typename Slice<T>::iterator Slice<T>::iterator::operator+(
637 typename Slice<T>::iterator::difference_type n) const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800638 auto ret = iterator(*this);
639 ret.pos = static_cast<char *>(this->pos) + this->stride * n;
640 return ret;
David Tolnayc30e4472020-12-27 00:25:42 -0800641}
642
643template <typename T>
644typename Slice<T>::iterator Slice<T>::iterator::operator-(
645 typename Slice<T>::iterator::difference_type n) const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800646 auto ret = iterator(*this);
647 ret.pos = static_cast<char *>(this->pos) - this->stride * n;
648 return ret;
David Tolnayc30e4472020-12-27 00:25:42 -0800649}
650
651template <typename T>
652typename Slice<T>::iterator::difference_type
653Slice<T>::iterator::operator-(const iterator &other) const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800654 auto diff = std::distance(static_cast<char *>(other.pos),
655 static_cast<char *>(this->pos));
656 return diff / this->stride;
David Tolnayc30e4472020-12-27 00:25:42 -0800657}
658
659template <typename T>
David Tolnayac6cb542020-11-25 20:18:55 -0800660bool Slice<T>::iterator::operator==(const iterator &other) const noexcept {
661 return this->pos == other.pos;
662}
663
664template <typename T>
665bool Slice<T>::iterator::operator!=(const iterator &other) const noexcept {
666 return this->pos != other.pos;
667}
668
669template <typename T>
David Tolnayc30e4472020-12-27 00:25:42 -0800670bool Slice<T>::iterator::operator<(const iterator &other) const noexcept {
671 return this->pos < other.pos;
672}
673
674template <typename T>
675bool Slice<T>::iterator::operator<=(const iterator &other) const noexcept {
676 return this->pos <= other.pos;
677}
678
679template <typename T>
680bool Slice<T>::iterator::operator>(const iterator &other) const noexcept {
681 return this->pos > other.pos;
682}
683
684template <typename T>
685bool Slice<T>::iterator::operator>=(const iterator &other) const noexcept {
686 return this->pos >= other.pos;
687}
688
689template <typename T>
David Tolnayac6cb542020-11-25 20:18:55 -0800690typename Slice<T>::iterator Slice<T>::begin() const noexcept {
691 iterator it;
David Tolnaya1ddbf82020-12-27 00:56:35 -0800692 it.pos = const_cast<typename std::remove_const<T>::type *>(this->ptr);
David Tolnay6f92baa2020-12-27 01:09:26 -0800693 it.stride = size_of<T>();
David Tolnayac6cb542020-11-25 20:18:55 -0800694 return it;
695}
696
697template <typename T>
698typename Slice<T>::iterator Slice<T>::end() const noexcept {
699 iterator it = this->begin();
David Tolnaya1ddbf82020-12-27 00:56:35 -0800700 it.pos = static_cast<char *>(it.pos) + it.stride * this->len;
David Tolnayac6cb542020-11-25 20:18:55 -0800701 return it;
702}
David Tolnay0f0162f2020-11-16 23:43:37 -0800703#endif // CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700704
David Tolnay0f0162f2020-11-16 23:43:37 -0800705#ifndef CXXBRIDGE1_RUST_BOX
706#define CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700707template <typename T>
David Tolnayc4b34222020-12-12 13:06:26 -0800708class Box<T>::uninit {};
709
710template <typename T>
David Tolnaye5703162020-12-12 16:26:35 -0800711class Box<T>::allocation {
David Tolnayf448e202020-12-12 16:42:45 -0800712 static T *alloc() noexcept;
713 static void dealloc(T *) noexcept;
David Tolnay2e637d92020-12-12 21:08:01 -0800714
David Tolnaye5703162020-12-12 16:26:35 -0800715public:
David Tolnaye7d662d2020-12-12 16:38:22 -0800716 allocation() noexcept : ptr(alloc()) {}
717 ~allocation() noexcept {
718 if (this->ptr) {
719 dealloc(this->ptr);
720 }
721 }
David Tolnaye5703162020-12-12 16:26:35 -0800722 T *ptr;
723};
724
725template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700726Box<T>::Box(const Box &other) : Box(*other) {}
727
728template <typename T>
729Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
730 other.ptr = nullptr;
731}
732
733template <typename T>
David Tolnaye7d662d2020-12-12 16:38:22 -0800734Box<T>::Box(const T &val) {
735 allocation alloc;
736 ::new (alloc.ptr) T(val);
737 this->ptr = alloc.ptr;
738 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700739}
740
741template <typename T>
David Tolnaye7d662d2020-12-12 16:38:22 -0800742Box<T>::Box(T &&val) {
743 allocation alloc;
744 ::new (alloc.ptr) T(std::move(val));
745 this->ptr = alloc.ptr;
746 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700747}
748
749template <typename T>
750Box<T>::~Box() noexcept {
751 if (this->ptr) {
752 this->drop();
753 }
754}
755
756template <typename T>
757Box<T> &Box<T>::operator=(const Box &other) {
David Tolnay439ed0b2020-12-12 17:01:04 -0800758 if (this->ptr) {
759 **this = *other;
760 } else {
761 allocation alloc;
762 ::new (alloc.ptr) T(*other);
763 this->ptr = alloc.ptr;
764 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700765 }
766 return *this;
767}
768
769template <typename T>
770Box<T> &Box<T>::operator=(Box &&other) noexcept {
771 if (this->ptr) {
772 this->drop();
773 }
774 this->ptr = other.ptr;
775 other.ptr = nullptr;
776 return *this;
777}
778
779template <typename T>
780const T *Box<T>::operator->() const noexcept {
781 return this->ptr;
782}
783
784template <typename T>
785const T &Box<T>::operator*() const noexcept {
786 return *this->ptr;
787}
788
789template <typename T>
790T *Box<T>::operator->() noexcept {
791 return this->ptr;
792}
793
794template <typename T>
795T &Box<T>::operator*() noexcept {
796 return *this->ptr;
797}
798
799template <typename T>
800template <typename... Fields>
801Box<T> Box<T>::in_place(Fields &&... fields) {
David Tolnaye7d662d2020-12-12 16:38:22 -0800802 allocation alloc;
803 auto ptr = alloc.ptr;
804 ::new (ptr) T{std::forward<Fields>(fields)...};
805 alloc.ptr = nullptr;
806 return from_raw(ptr);
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700807}
808
809template <typename T>
810Box<T> Box<T>::from_raw(T *raw) noexcept {
David Tolnayc4b34222020-12-12 13:06:26 -0800811 Box box = uninit{};
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700812 box.ptr = raw;
813 return box;
814}
815
816template <typename T>
817T *Box<T>::into_raw() noexcept {
818 T *raw = this->ptr;
819 this->ptr = nullptr;
820 return raw;
821}
822
823template <typename T>
David Tolnayc4b34222020-12-12 13:06:26 -0800824Box<T>::Box(uninit) noexcept {}
David Tolnay0f0162f2020-11-16 23:43:37 -0800825#endif // CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700826
David Tolnay0f0162f2020-11-16 23:43:37 -0800827#ifndef CXXBRIDGE1_RUST_VEC
828#define CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700829template <typename T>
David Tolnayf799b372020-11-29 23:57:42 -0800830Vec<T>::Vec(std::initializer_list<T> init) : Vec{} {
831 this->reserve_total(init.size());
832 std::move(init.begin(), init.end(), std::back_inserter(*this));
833}
834
835template <typename T>
David Tolnay9007e462020-12-11 13:59:08 -0800836Vec<T>::Vec(const Vec &other) : Vec() {
837 this->reserve_total(other.size());
838 std::copy(other.begin(), other.end(), std::back_inserter(*this));
839}
840
841template <typename T>
David Tolnay15671862020-11-23 18:13:56 -0800842Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700843 new (&other) Vec();
844}
845
846template <typename T>
847Vec<T>::~Vec() noexcept {
848 this->drop();
849}
850
851template <typename T>
852Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
853 if (this != &other) {
854 this->drop();
855 this->repr = other.repr;
856 new (&other) Vec();
857 }
858 return *this;
859}
860
861template <typename T>
David Tolnaydd42c722020-12-11 14:05:26 -0800862Vec<T> &Vec<T>::operator=(const Vec &other) {
863 if (this != &other) {
864 this->drop();
865 new (this) Vec(other);
866 }
867 return *this;
868}
869
870template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700871bool Vec<T>::empty() const noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800872 return this->size() == 0;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700873}
874
875template <typename T>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800876T *Vec<T>::data() noexcept {
877 return const_cast<T *>(const_cast<const Vec<T> *>(this)->data());
878}
879
880template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800881const T &Vec<T>::operator[](std::size_t n) const noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800882 assert(n < this->size());
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700883 auto data = reinterpret_cast<const char *>(this->data());
884 return *reinterpret_cast<const T *>(data + n * this->stride());
885}
886
887template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800888const T &Vec<T>::at(std::size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700889 if (n >= this->size()) {
890 panic<std::out_of_range>("rust::Vec index out of range");
891 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700892 return (*this)[n];
893}
894
895template <typename T>
David Tolnayd4fff5d2020-12-21 14:04:09 -0800896const T &Vec<T>::front() const noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800897 assert(!this->empty());
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700898 return (*this)[0];
899}
900
901template <typename T>
David Tolnayd4fff5d2020-12-21 14:04:09 -0800902const T &Vec<T>::back() const noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800903 assert(!this->empty());
David Tolnayb10c4bc2020-08-26 21:55:29 -0700904 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700905}
906
907template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800908T &Vec<T>::operator[](std::size_t n) noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800909 assert(n < this->size());
David Tolnay908d5e52020-11-30 00:30:59 -0800910 auto data = reinterpret_cast<char *>(this->data());
911 return *reinterpret_cast<T *>(data + n * this->stride());
912}
913
914template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800915T &Vec<T>::at(std::size_t n) {
David Tolnay908d5e52020-11-30 00:30:59 -0800916 if (n >= this->size()) {
917 panic<std::out_of_range>("rust::Vec index out of range");
918 }
919 return (*this)[n];
920}
921
922template <typename T>
David Tolnayd4fff5d2020-12-21 14:04:09 -0800923T &Vec<T>::front() noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800924 assert(!this->empty());
David Tolnay908d5e52020-11-30 00:30:59 -0800925 return (*this)[0];
926}
927
928template <typename T>
David Tolnayd4fff5d2020-12-21 14:04:09 -0800929T &Vec<T>::back() noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800930 assert(!this->empty());
David Tolnay908d5e52020-11-30 00:30:59 -0800931 return (*this)[this->size() - 1];
932}
933
934template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800935void Vec<T>::reserve(std::size_t new_cap) {
David Tolnayfb6b73c2020-11-10 14:32:16 -0800936 this->reserve_total(new_cap);
937}
938
939template <typename T>
940void Vec<T>::push_back(const T &value) {
941 this->emplace_back(value);
942}
943
944template <typename T>
945void Vec<T>::push_back(T &&value) {
946 this->emplace_back(std::move(value));
947}
948
949template <typename T>
950template <typename... Args>
951void Vec<T>::emplace_back(Args &&... args) {
952 auto size = this->size();
953 this->reserve_total(size + 1);
954 ::new (reinterpret_cast<T *>(reinterpret_cast<char *>(this->data()) +
955 size * this->stride()))
956 T(std::forward<Args>(args)...);
957 this->set_len(size + 1);
958}
959
960template <typename T>
David Tolnay300072b2020-12-03 12:12:24 -0800961typename Vec<T>::iterator::reference
962Vec<T>::iterator::operator*() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800963 return *static_cast<T *>(this->pos);
964}
965
966template <typename T>
David Tolnay300072b2020-12-03 12:12:24 -0800967typename Vec<T>::iterator::pointer
968Vec<T>::iterator::operator->() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800969 return static_cast<T *>(this->pos);
970}
971
972template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500973typename Vec<T>::iterator::reference Vec<T>::iterator::operator[](
David Tolnay665178e2020-12-12 08:49:59 -0800974 typename Vec<T>::iterator::difference_type n) const noexcept {
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500975 auto pos = static_cast<char *>(this->pos) + this->stride * n;
976 return *static_cast<T *>(pos);
977}
978
979template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800980typename Vec<T>::iterator &Vec<T>::iterator::operator++() noexcept {
David Tolnay248215e2020-11-25 19:38:26 -0800981 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnay960b5112020-11-25 13:18:28 -0800982 return *this;
983}
984
985template <typename T>
986typename Vec<T>::iterator Vec<T>::iterator::operator++(int) noexcept {
987 auto ret = iterator(*this);
David Tolnay248215e2020-11-25 19:38:26 -0800988 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnay960b5112020-11-25 13:18:28 -0800989 return ret;
990}
991
992template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -0500993typename Vec<T>::iterator &Vec<T>::iterator::operator--() noexcept {
994 this->pos = static_cast<char *>(this->pos) - this->stride;
995 return *this;
996}
997
998template <typename T>
999typename Vec<T>::iterator Vec<T>::iterator::operator--(int) noexcept {
1000 auto ret = iterator(*this);
1001 this->pos = static_cast<char *>(this->pos) - this->stride;
1002 return ret;
1003}
1004
1005template <typename T>
David Tolnay665178e2020-12-12 08:49:59 -08001006typename Vec<T>::iterator &Vec<T>::iterator::operator+=(
1007 typename Vec<T>::iterator::difference_type n) noexcept {
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001008 this->pos = static_cast<char *>(this->pos) + this->stride * n;
1009 return *this;
1010}
1011
1012template <typename T>
David Tolnay665178e2020-12-12 08:49:59 -08001013typename Vec<T>::iterator &Vec<T>::iterator::operator-=(
1014 typename Vec<T>::iterator::difference_type n) noexcept {
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001015 this->pos = static_cast<char *>(this->pos) - this->stride * n;
1016 return *this;
1017}
1018
1019template <typename T>
1020typename Vec<T>::iterator Vec<T>::iterator::operator+(
David Tolnay665178e2020-12-12 08:49:59 -08001021 typename Vec<T>::iterator::difference_type n) const noexcept {
David Tolnay300072b2020-12-03 12:12:24 -08001022 auto ret = iterator(*this);
1023 ret.pos = static_cast<char *>(this->pos) + this->stride * n;
1024 return ret;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001025}
1026
1027template <typename T>
1028typename Vec<T>::iterator Vec<T>::iterator::operator-(
David Tolnay665178e2020-12-12 08:49:59 -08001029 typename Vec<T>::iterator::difference_type n) const noexcept {
David Tolnay300072b2020-12-03 12:12:24 -08001030 auto ret = iterator(*this);
1031 ret.pos = static_cast<char *>(this->pos) - this->stride * n;
1032 return ret;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001033}
1034
1035template <typename T>
1036typename Vec<T>::iterator::difference_type
1037Vec<T>::iterator::operator-(const iterator &other) const noexcept {
1038 auto diff = std::distance(static_cast<char *>(other.pos),
1039 static_cast<char *>(this->pos));
David Tolnay300072b2020-12-03 12:12:24 -08001040 return diff / this->stride;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001041}
1042
1043template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -08001044bool Vec<T>::iterator::operator==(const iterator &other) const noexcept {
1045 return this->pos == other.pos;
1046}
1047
1048template <typename T>
1049bool Vec<T>::iterator::operator!=(const iterator &other) const noexcept {
1050 return this->pos != other.pos;
1051}
1052
1053template <typename T>
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001054bool Vec<T>::iterator::operator<(const iterator &other) const noexcept {
1055 return this->pos < other.pos;
1056}
1057
1058template <typename T>
David Tolnay0c9c3e62020-12-27 00:52:25 -08001059bool Vec<T>::iterator::operator<=(const iterator &other) const noexcept {
1060 return this->pos <= other.pos;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001061}
1062
1063template <typename T>
David Tolnay0c9c3e62020-12-27 00:52:25 -08001064bool Vec<T>::iterator::operator>(const iterator &other) const noexcept {
1065 return this->pos > other.pos;
1066}
1067
1068template <typename T>
1069bool Vec<T>::iterator::operator>=(const iterator &other) const noexcept {
1070 return this->pos >= other.pos;
Yan Zaretskiyefd5cd42020-12-03 09:22:22 -05001071}
1072
1073template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -08001074typename Vec<T>::iterator Vec<T>::begin() noexcept {
1075 iterator it;
David Tolnaya5d72c62020-11-25 13:57:16 -08001076 it.pos = const_cast<typename std::remove_const<T>::type *>(this->data());
David Tolnay960b5112020-11-25 13:18:28 -08001077 it.stride = this->stride();
1078 return it;
1079}
1080
1081template <typename T>
1082typename Vec<T>::iterator Vec<T>::end() noexcept {
1083 iterator it = this->begin();
David Tolnay248215e2020-11-25 19:38:26 -08001084 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
David Tolnay960b5112020-11-25 13:18:28 -08001085 return it;
1086}
1087
1088template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -07001089typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -08001090 return this->cbegin();
1091}
1092
1093template <typename T>
1094typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
1095 return this->cend();
1096}
1097
1098template <typename T>
1099typename Vec<T>::const_iterator Vec<T>::cbegin() const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -07001100 const_iterator it;
David Tolnaya5d72c62020-11-25 13:57:16 -08001101 it.pos = const_cast<typename std::remove_const<T>::type *>(this->data());
David Tolnay2a2b9ad2020-05-12 20:07:26 -07001102 it.stride = this->stride();
1103 return it;
1104}
1105
1106template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -08001107typename Vec<T>::const_iterator Vec<T>::cend() const noexcept {
1108 const_iterator it = this->cbegin();
David Tolnay248215e2020-11-25 19:38:26 -08001109 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
David Tolnay2a2b9ad2020-05-12 20:07:26 -07001110 return it;
1111}
1112
1113// Internal API only intended for the cxxbridge code generator.
1114template <typename T>
1115Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay0f0162f2020-11-16 23:43:37 -08001116#endif // CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -07001117
David Tolnay75068632020-12-26 22:15:17 -08001118#ifndef CXXBRIDGE1_IS_COMPLETE
1119#define CXXBRIDGE1_IS_COMPLETE
1120namespace detail {
1121namespace {
1122template <typename T, typename = std::size_t>
1123struct is_complete : std::false_type {};
1124template <typename T>
1125struct is_complete<T, decltype(sizeof(T))> : std::true_type {};
1126} // namespace
1127} // namespace detail
1128#endif // CXXBRIDGE1_IS_COMPLETE
1129
David Tolnayee6ecfc2020-12-26 21:54:37 -08001130#ifndef CXXBRIDGE1_LAYOUT
1131#define CXXBRIDGE1_LAYOUT
1132class layout {
1133 template <typename T>
1134 friend std::size_t size_of();
1135 template <typename T>
1136 friend std::size_t align_of();
1137 template <typename T>
1138 static typename std::enable_if<std::is_base_of<Opaque, T>::value,
1139 std::size_t>::type
1140 do_size_of() {
1141 return T::layout::size();
1142 }
1143 template <typename T>
1144 static typename std::enable_if<!std::is_base_of<Opaque, T>::value,
1145 std::size_t>::type
1146 do_size_of() {
1147 return sizeof(T);
1148 }
1149 template <typename T>
1150 static
1151 typename std::enable_if<detail::is_complete<T>::value, std::size_t>::type
1152 size_of() {
1153 return do_size_of<T>();
1154 }
1155 template <typename T>
1156 static typename std::enable_if<std::is_base_of<Opaque, T>::value,
1157 std::size_t>::type
1158 do_align_of() {
1159 return T::layout::align();
1160 }
1161 template <typename T>
1162 static typename std::enable_if<!std::is_base_of<Opaque, T>::value,
1163 std::size_t>::type
1164 do_align_of() {
1165 return alignof(T);
1166 }
1167 template <typename T>
1168 static
1169 typename std::enable_if<detail::is_complete<T>::value, std::size_t>::type
1170 align_of() {
1171 return do_align_of<T>();
1172 }
1173};
1174
1175template <typename T>
1176std::size_t size_of() {
1177 return layout::size_of<T>();
1178}
1179
1180template <typename T>
1181std::size_t align_of() {
1182 return layout::align_of<T>();
1183}
1184#endif // CXXBRIDGE1_LAYOUT
1185
David Tolnay0f0162f2020-11-16 23:43:37 -08001186#ifndef CXXBRIDGE1_RELOCATABLE
1187#define CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -08001188namespace detail {
1189template <typename... Ts>
1190struct make_void {
1191 using type = void;
1192};
1193
1194template <typename... Ts>
1195using void_t = typename make_void<Ts...>::type;
1196
1197template <typename Void, template <typename...> class, typename...>
1198struct detect : std::false_type {};
1199template <template <typename...> class T, typename... A>
1200struct detect<void_t<T<A...>>, T, A...> : std::true_type {};
1201
1202template <template <typename...> class T, typename... A>
1203using is_detected = detect<void, T, A...>;
1204
1205template <typename T>
1206using detect_IsRelocatable = typename T::IsRelocatable;
1207
1208template <typename T>
1209struct get_IsRelocatable
1210 : std::is_same<typename T::IsRelocatable, std::true_type> {};
1211} // namespace detail
1212
1213template <typename T>
1214struct IsRelocatable
1215 : std::conditional<
1216 detail::is_detected<detail::detect_IsRelocatable, T>::value,
1217 detail::get_IsRelocatable<T>,
1218 std::integral_constant<
1219 bool, std::is_trivially_move_constructible<T>::value &&
1220 std::is_trivially_destructible<T>::value>>::type {};
David Tolnay0f0162f2020-11-16 23:43:37 -08001221#endif // CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -08001222
David Tolnay0f0162f2020-11-16 23:43:37 -08001223} // namespace cxxbridge1
David Tolnay750755e2020-03-01 13:04:08 -08001224} // namespace rust