blob: 95f9a067f62caf3c05d8a737c530875f7af95659 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001#pragma once
David Tolnayf799b372020-11-29 23:57:42 -08002#include <algorithm>
David Tolnay7db73692019-10-20 14:51:12 -04003#include <array>
David Tolnayfa5a4a62020-12-21 14:08:08 -08004#include <cassert>
David Tolnay30430f12020-03-19 20:49:00 -07005#include <cstddef>
David Tolnay7db73692019-10-20 14:51:12 -04006#include <cstdint>
David Tolnayb7a7cb62020-03-17 21:18:40 -07007#include <exception>
David Tolnayf799b372020-11-29 23:57:42 -08008#include <initializer_list>
David Tolnay001102a2020-03-01 20:05:04 -08009#include <iosfwd>
David Tolnayd1df4c72020-11-25 20:38:05 -080010#include <iterator>
David Tolnay0ecd05a2020-07-29 16:32:03 -070011#include <new>
Stephen Crane9e48d5b2020-08-21 12:17:02 -070012#include <stdexcept>
David Tolnay7db73692019-10-20 14:51:12 -040013#include <string>
David Tolnayf6292372020-03-01 21:09:11 -080014#include <type_traits>
David Tolnay4791f1c2020-03-17 21:53:16 -070015#include <utility>
David Tolnay37dd7e12020-04-25 12:51:59 -070016#include <vector>
David Tolnay59b5ba12020-04-10 11:32:19 -070017#if defined(_WIN32)
David Tolnayda38b7c2020-09-16 11:50:04 -040018#include <basetsd.h>
David Tolnay74dd66f2020-12-12 22:03:47 -080019#else
20#include <sys/types.h>
David Tolnay59b5ba12020-04-10 11:32:19 -070021#endif
David Tolnay7db73692019-10-20 14:51:12 -040022
David Tolnay750755e2020-03-01 13:04:08 -080023namespace rust {
David Tolnay0f0162f2020-11-16 23:43:37 -080024inline namespace cxxbridge1 {
David Tolnay7db73692019-10-20 14:51:12 -040025
David Tolnay2a2b9ad2020-05-12 20:07:26 -070026struct unsafe_bitcopy_t;
David Tolnayd1e2efc2020-03-03 22:25:43 -080027
David Tolnay84ddf9e2020-10-31 15:36:48 -070028namespace {
29template <typename T>
30class impl;
31}
32
David Tolnay0f0162f2020-11-16 23:43:37 -080033#ifndef CXXBRIDGE1_RUST_STRING
34#define CXXBRIDGE1_RUST_STRING
David Tolnaye468f052020-11-29 19:39:35 -080035// https://cxx.rs/binding/string.html
David Tolnay56082162020-03-01 12:57:33 -080036class String final {
David Tolnay7db73692019-10-20 14:51:12 -040037public:
David Tolnay56082162020-03-01 12:57:33 -080038 String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080039 String(const String &) noexcept;
40 String(String &&) noexcept;
David Tolnay56082162020-03-01 12:57:33 -080041 ~String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080042
43 String(const std::string &);
44 String(const char *);
David Tolnaybe3cbf72020-12-12 22:12:07 -080045 String(const char *, std::size_t);
David Tolnayd9c4ac92020-03-01 20:33:58 -080046
47 String &operator=(const String &) noexcept;
48 String &operator=(String &&) noexcept;
49
David Tolnay404d6892020-03-01 20:19:41 -080050 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040051
52 // Note: no null terminator.
53 const char *data() const noexcept;
David Tolnaybe3cbf72020-12-12 22:12:07 -080054 std::size_t size() const noexcept;
55 std::size_t length() const noexcept;
David Tolnay7db73692019-10-20 14:51:12 -040056
David Tolnaycca2e612020-12-18 12:48:22 -080057 const char *c_str() noexcept;
58
David Tolnayff7f5fb2020-11-25 20:50:32 -080059 using iterator = char *;
60 iterator begin() noexcept;
61 iterator end() noexcept;
62
63 using const_iterator = const char *;
64 const_iterator begin() const noexcept;
65 const_iterator end() const noexcept;
66 const_iterator cbegin() const noexcept;
67 const_iterator cend() const noexcept;
68
David Tolnayff86dce2020-11-29 19:45:13 -080069 bool operator==(const String &) const noexcept;
70 bool operator!=(const String &) const noexcept;
71 bool operator<(const String &) const noexcept;
72 bool operator<=(const String &) const noexcept;
73 bool operator>(const String &) const noexcept;
74 bool operator>=(const String &) const noexcept;
75
David Tolnayd1e2efc2020-03-03 22:25:43 -080076 // Internal API only intended for the cxxbridge code generator.
77 String(unsafe_bitcopy_t, const String &) noexcept;
78
David Tolnay7db73692019-10-20 14:51:12 -040079private:
80 // Size and alignment statically verified by rust_string.rs.
David Tolnaybe3cbf72020-12-12 22:12:07 -080081 std::array<std::uintptr_t, 3> repr;
David Tolnay7db73692019-10-20 14:51:12 -040082};
David Tolnay0f0162f2020-11-16 23:43:37 -080083#endif // CXXBRIDGE1_RUST_STRING
David Tolnay7db73692019-10-20 14:51:12 -040084
David Tolnay0f0162f2020-11-16 23:43:37 -080085#ifndef CXXBRIDGE1_RUST_STR
David Tolnaye468f052020-11-29 19:39:35 -080086// https://cxx.rs/binding/str.html
David Tolnay09dbe752020-03-01 13:00:40 -080087class Str final {
David Tolnay7db73692019-10-20 14:51:12 -040088public:
David Tolnay09dbe752020-03-01 13:00:40 -080089 Str() noexcept;
David Tolnay828e5132020-11-29 20:40:40 -080090 Str(const String &) noexcept;
David Tolnay851677c2020-03-01 23:49:46 -080091 Str(const std::string &);
92 Str(const char *);
David Tolnaybe3cbf72020-12-12 22:12:07 -080093 Str(const char *, std::size_t);
David Tolnayd9c4ac92020-03-01 20:33:58 -080094
David Tolnay2d7f1172020-10-31 17:58:31 -070095 Str &operator=(const Str &) noexcept = default;
David Tolnayd9c4ac92020-03-01 20:33:58 -080096
David Tolnay404d6892020-03-01 20:19:41 -080097 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040098
99 // Note: no null terminator.
100 const char *data() const noexcept;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800101 std::size_t size() const noexcept;
102 std::size_t length() const noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400103
David Tolnayde9a5b12020-10-31 12:15:43 -0700104 // Important in order for System V ABI to pass in registers.
105 Str(const Str &) noexcept = default;
106 ~Str() noexcept = default;
107
David Tolnayff7f5fb2020-11-25 20:50:32 -0800108 using iterator = const char *;
109 using const_iterator = const char *;
110 const_iterator begin() const noexcept;
111 const_iterator end() const noexcept;
112 const_iterator cbegin() const noexcept;
113 const_iterator cend() const noexcept;
114
David Tolnayff86dce2020-11-29 19:45:13 -0800115 bool operator==(const Str &) const noexcept;
116 bool operator!=(const Str &) const noexcept;
117 bool operator<(const Str &) const noexcept;
118 bool operator<=(const Str &) const noexcept;
119 bool operator>(const Str &) const noexcept;
120 bool operator>=(const Str &) const noexcept;
121
David Tolnay5df1f062020-10-31 12:31:10 -0700122private:
David Tolnay0356d332020-10-31 19:46:41 -0700123 friend impl<Str>;
David Tolnay7db73692019-10-20 14:51:12 -0400124 // Not necessarily ABI compatible with &str. Codegen will translate to
125 // cxx::rust_str::RustStr which matches this layout.
David Tolnay5df1f062020-10-31 12:31:10 -0700126 const char *ptr;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800127 std::size_t len;
David Tolnay7db73692019-10-20 14:51:12 -0400128};
David Tolnay0f0162f2020-11-16 23:43:37 -0800129#endif // CXXBRIDGE1_RUST_STR
David Tolnay7db73692019-10-20 14:51:12 -0400130
David Tolnay0f0162f2020-11-16 23:43:37 -0800131#ifndef CXXBRIDGE1_RUST_SLICE
David Tolnayc5629f02020-11-23 18:32:46 -0800132namespace detail {
David Tolnayee9b9ee2020-11-25 08:28:50 -0800133template <bool>
David Tolnayc5629f02020-11-23 18:32:46 -0800134struct copy_assignable_if {};
David Tolnayce298232020-11-11 10:08:54 -0800135
David Tolnayc5629f02020-11-23 18:32:46 -0800136template <>
137struct copy_assignable_if<false> {
138 copy_assignable_if() noexcept = default;
139 copy_assignable_if(const copy_assignable_if &) noexcept = default;
140 copy_assignable_if &operator=(const copy_assignable_if &) noexcept = delete;
141 copy_assignable_if &operator=(copy_assignable_if &&) noexcept = default;
142};
143} // namespace detail
144
David Tolnaye468f052020-11-29 19:39:35 -0800145// https://cxx.rs/binding/slice.html
David Tolnayc5629f02020-11-23 18:32:46 -0800146template <typename T>
147class Slice final
148 : private detail::copy_assignable_if<std::is_const<T>::value> {
David Tolnayefe81052020-04-14 16:28:24 -0700149public:
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700150 Slice() noexcept;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800151 Slice(T *, std::size_t count) noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700152
David Tolnay2d7f1172020-10-31 17:58:31 -0700153 Slice &operator=(const Slice<T> &) noexcept = default;
David Tolnayc5629f02020-11-23 18:32:46 -0800154 Slice &operator=(Slice<T> &&) noexcept = default;
David Tolnayefe81052020-04-14 16:28:24 -0700155
David Tolnayce298232020-11-11 10:08:54 -0800156 T *data() const noexcept;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800157 std::size_t size() const noexcept;
158 std::size_t length() const noexcept;
David Tolnay78dd5352020-12-26 23:44:20 -0800159 bool empty() const noexcept;
160
161 T &operator[](std::size_t n) const noexcept;
162 T &at(std::size_t n) const;
163 T &front() const noexcept;
164 T &back() const noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700165
David Tolnayde9a5b12020-10-31 12:15:43 -0700166 // Important in order for System V ABI to pass in registers.
167 Slice(const Slice<T> &) noexcept = default;
168 ~Slice() noexcept = default;
169
David Tolnayac6cb542020-11-25 20:18:55 -0800170 class iterator;
171 iterator begin() const noexcept;
172 iterator end() const noexcept;
173
David Tolnayefe81052020-04-14 16:28:24 -0700174private:
David Tolnay36aa9e02020-10-31 23:08:21 -0700175 friend impl<Slice>;
176 // Not necessarily ABI compatible with &[T]. Codegen will translate to
David Tolnay5515a9e2020-11-25 19:07:54 -0800177 // cxx::rust_slice::RustSlice which matches this layout.
David Tolnay5ce110e2020-12-27 02:45:53 -0800178 void *ptr;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800179 std::size_t len;
David Tolnayefe81052020-04-14 16:28:24 -0700180};
David Tolnayac6cb542020-11-25 20:18:55 -0800181
182template <typename T>
183class Slice<T>::iterator final {
184public:
David Tolnayc30e4472020-12-27 00:25:42 -0800185 using iterator_category = std::random_access_iterator_tag;
David Tolnayac6cb542020-11-25 20:18:55 -0800186 using value_type = T;
David Tolnayc30e4472020-12-27 00:25:42 -0800187 using difference_type = std::ptrdiff_t;
David Tolnayac6cb542020-11-25 20:18:55 -0800188 using pointer = typename std::add_pointer<T>::type;
189 using reference = typename std::add_lvalue_reference<T>::type;
David Tolnayac6cb542020-11-25 20:18:55 -0800190
David Tolnaydb388c92020-12-27 00:28:26 -0800191 reference operator*() const noexcept;
192 pointer operator->() const noexcept;
David Tolnayc30e4472020-12-27 00:25:42 -0800193 reference operator[](difference_type) const noexcept;
194
David Tolnayac6cb542020-11-25 20:18:55 -0800195 iterator &operator++() noexcept;
196 iterator operator++(int) noexcept;
David Tolnayc30e4472020-12-27 00:25:42 -0800197 iterator &operator--() noexcept;
198 iterator operator--(int) noexcept;
199
200 iterator &operator+=(difference_type) noexcept;
201 iterator &operator-=(difference_type) noexcept;
202 iterator operator+(difference_type) const noexcept;
203 iterator operator-(difference_type) const noexcept;
204 difference_type operator-(const iterator &) const noexcept;
205
David Tolnayac6cb542020-11-25 20:18:55 -0800206 bool operator==(const iterator &) const noexcept;
207 bool operator!=(const iterator &) const noexcept;
David Tolnayc30e4472020-12-27 00:25:42 -0800208 bool operator<(const iterator &) const noexcept;
209 bool operator<=(const iterator &) const noexcept;
210 bool operator>(const iterator &) const noexcept;
211 bool operator>=(const iterator &) const noexcept;
David Tolnayac6cb542020-11-25 20:18:55 -0800212
213private:
214 friend class Slice;
David Tolnaya1ddbf82020-12-27 00:56:35 -0800215 void *pos;
216 std::size_t stride;
David Tolnayac6cb542020-11-25 20:18:55 -0800217};
David Tolnay0f0162f2020-11-16 23:43:37 -0800218#endif // CXXBRIDGE1_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -0700219
David Tolnay0f0162f2020-11-16 23:43:37 -0800220#ifndef CXXBRIDGE1_RUST_BOX
David Tolnaye468f052020-11-29 19:39:35 -0800221// https://cxx.rs/binding/box.html
David Tolnayf262d382020-04-11 22:12:40 -0700222template <typename T>
223class Box final {
David Tolnay7db73692019-10-20 14:51:12 -0400224public:
David Tolnayf6292372020-03-01 21:09:11 -0800225 using value_type = T;
David Tolnay9706a512020-04-24 17:09:01 -0700226 using const_pointer =
227 typename std::add_pointer<typename std::add_const<T>::type>::type;
228 using pointer = typename std::add_pointer<T>::type;
David Tolnayf6292372020-03-01 21:09:11 -0800229
David Tolnayc4b34222020-12-12 13:06:26 -0800230 Box() = delete;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700231 Box(const Box &);
232 Box(Box &&) noexcept;
233 ~Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400234
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700235 explicit Box(const T &);
236 explicit Box(T &&);
237
238 Box &operator=(const Box &);
239 Box &operator=(Box &&) noexcept;
240
241 const T *operator->() const noexcept;
242 const T &operator*() const noexcept;
243 T *operator->() noexcept;
244 T &operator*() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400245
David Tolnayf262d382020-04-11 22:12:40 -0700246 template <typename... Fields>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700247 static Box in_place(Fields &&...);
David Tolnay7ce59fc2020-04-11 11:46:33 -0700248
David Tolnay7db73692019-10-20 14:51:12 -0400249 // Important: requires that `raw` came from an into_raw call. Do not pass a
250 // pointer from `new` or any other source.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700251 static Box from_raw(T *) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400252
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700253 T *into_raw() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400254
255private:
David Tolnayc4b34222020-12-12 13:06:26 -0800256 class uninit;
David Tolnaye5703162020-12-12 16:26:35 -0800257 class allocation;
David Tolnayc4b34222020-12-12 13:06:26 -0800258 Box(uninit) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400259 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800260 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400261};
David Tolnay0f0162f2020-11-16 23:43:37 -0800262#endif // CXXBRIDGE1_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400263
David Tolnay0f0162f2020-11-16 23:43:37 -0800264#ifndef CXXBRIDGE1_RUST_VEC
David Tolnaye468f052020-11-29 19:39:35 -0800265// https://cxx.rs/binding/vec.html
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700266template <typename T>
267class Vec final {
268public:
David Tolnayc87c2152020-04-24 17:07:41 -0700269 using value_type = T;
270
David Tolnayf97c2d52020-04-25 16:37:48 -0700271 Vec() noexcept;
David Tolnayf799b372020-11-29 23:57:42 -0800272 Vec(std::initializer_list<T>);
David Tolnay9007e462020-12-11 13:59:08 -0800273 Vec(const Vec &);
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700274 Vec(Vec &&) noexcept;
275 ~Vec() noexcept;
David Tolnaycb800572020-04-24 20:30:43 -0700276
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700277 Vec &operator=(Vec &&) noexcept;
David Tolnaydd42c722020-12-11 14:05:26 -0800278 Vec &operator=(const Vec &);
David Tolnayf97c2d52020-04-25 16:37:48 -0700279
David Tolnaybe3cbf72020-12-12 22:12:07 -0800280 std::size_t size() const noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700281 bool empty() const noexcept;
David Tolnay219c0792020-04-24 20:31:37 -0700282 const T *data() const noexcept;
David Tolnayfb6b73c2020-11-10 14:32:16 -0800283 T *data() noexcept;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800284 std::size_t capacity() const noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700285
David Tolnaybe3cbf72020-12-12 22:12:07 -0800286 const T &operator[](std::size_t n) const noexcept;
287 const T &at(std::size_t n) const;
David Tolnayd4fff5d2020-12-21 14:04:09 -0800288 const T &front() const noexcept;
289 const T &back() const noexcept;
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700290
David Tolnaybe3cbf72020-12-12 22:12:07 -0800291 T &operator[](std::size_t n) noexcept;
292 T &at(std::size_t n);
David Tolnayd4fff5d2020-12-21 14:04:09 -0800293 T &front() noexcept;
294 T &back() noexcept;
David Tolnay908d5e52020-11-30 00:30:59 -0800295
David Tolnaybe3cbf72020-12-12 22:12:07 -0800296 void reserve(std::size_t new_cap);
David Tolnayfb6b73c2020-11-10 14:32:16 -0800297 void push_back(const T &value);
298 void push_back(T &&value);
David Tolnay4e8c49a2020-11-11 10:00:18 -0800299 template <typename... Args>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800300 void emplace_back(Args &&... args);
301
David Tolnay725bf502020-12-27 02:47:40 -0800302 using iterator = typename Slice<T>::iterator;
David Tolnay960b5112020-11-25 13:18:28 -0800303 iterator begin() noexcept;
304 iterator end() noexcept;
305
David Tolnay725bf502020-12-27 02:47:40 -0800306 using const_iterator = typename Slice<const T>::iterator;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700307 const_iterator begin() const noexcept;
308 const_iterator end() const noexcept;
David Tolnay960b5112020-11-25 13:18:28 -0800309 const_iterator cbegin() const noexcept;
310 const_iterator cend() const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700311
David Tolnay313b10e2020-04-25 16:30:51 -0700312 // Internal API only intended for the cxxbridge code generator.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700313 Vec(unsafe_bitcopy_t, const Vec &) noexcept;
David Tolnay313b10e2020-04-25 16:30:51 -0700314
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700315private:
David Tolnaybe3cbf72020-12-12 22:12:07 -0800316 void reserve_total(std::size_t cap) noexcept;
317 void set_len(std::size_t len) noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700318 void drop() noexcept;
319
320 // Size and alignment statically verified by rust_vec.rs.
David Tolnaybe3cbf72020-12-12 22:12:07 -0800321 std::array<std::uintptr_t, 3> repr;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700322};
David Tolnay0f0162f2020-11-16 23:43:37 -0800323#endif // CXXBRIDGE1_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700324
David Tolnay0f0162f2020-11-16 23:43:37 -0800325#ifndef CXXBRIDGE1_RUST_FN
David Tolnaye468f052020-11-29 19:39:35 -0800326// https://cxx.rs/binding/fn.html
David Tolnayf031c322020-11-29 19:41:33 -0800327template <typename Signature>
David Tolnayf262d382020-04-11 22:12:40 -0700328class Fn;
David Tolnay75dca2e2020-03-25 20:17:52 -0700329
David Tolnayf031c322020-11-29 19:41:33 -0800330template <typename Ret, typename... Args>
331class Fn<Ret(Args...)> final {
David Tolnay75dca2e2020-03-25 20:17:52 -0700332public:
David Tolnayf031c322020-11-29 19:41:33 -0800333 Ret operator()(Args... args) const noexcept;
David Tolnay533d4582020-04-08 20:29:14 -0700334 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700335
336private:
David Tolnayf031c322020-11-29 19:41:33 -0800337 Ret (*trampoline)(Args..., void *fn) noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700338 void *fn;
339};
David Tolnay0f0162f2020-11-16 23:43:37 -0800340#endif // CXXBRIDGE1_RUST_FN
David Tolnay75dca2e2020-03-25 20:17:52 -0700341
David Tolnay0f0162f2020-11-16 23:43:37 -0800342#ifndef CXXBRIDGE1_RUST_ERROR
343#define CXXBRIDGE1_RUST_ERROR
David Tolnaye468f052020-11-29 19:39:35 -0800344// https://cxx.rs/binding/result.html
David Tolnaye4fa8732020-09-08 15:04:56 -0700345class Error final : public std::exception {
David Tolnay1e548172020-03-16 13:37:09 -0700346public:
347 Error(const Error &);
348 Error(Error &&) noexcept;
David Tolnay2714d2c2020-11-23 18:17:43 -0800349 ~Error() noexcept override;
David Tolnay7c6ac712020-10-31 17:22:28 -0700350
351 Error &operator=(const Error &);
David Tolnay15491062020-10-31 17:25:13 -0700352 Error &operator=(Error &&) noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700353
David Tolnay1e548172020-03-16 13:37:09 -0700354 const char *what() const noexcept override;
355
356private:
David Tolnaya0c9bc72020-10-31 14:37:14 -0700357 Error() noexcept = default;
David Tolnay84ddf9e2020-10-31 15:36:48 -0700358 friend impl<Error>;
David Tolnaya0c9bc72020-10-31 14:37:14 -0700359 const char *msg;
David Tolnaybe3cbf72020-12-12 22:12:07 -0800360 std::size_t len;
David Tolnay1e548172020-03-16 13:37:09 -0700361};
David Tolnay0f0162f2020-11-16 23:43:37 -0800362#endif // CXXBRIDGE1_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700363
David Tolnay0f0162f2020-11-16 23:43:37 -0800364#ifndef CXXBRIDGE1_RUST_ISIZE
365#define CXXBRIDGE1_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700366#if defined(_WIN32)
367using isize = SSIZE_T;
368#else
369using isize = ssize_t;
370#endif
David Tolnay0f0162f2020-11-16 23:43:37 -0800371#endif // CXXBRIDGE1_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700372
David Tolnay851677c2020-03-01 23:49:46 -0800373std::ostream &operator<<(std::ostream &, const String &);
374std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400375
David Tolnay365fc7c2020-11-25 16:08:13 -0800376#ifndef CXXBRIDGE1_RUST_OPAQUE
377#define CXXBRIDGE1_RUST_OPAQUE
378// Base class of generated opaque Rust types.
379class Opaque {
David Tolnaya857c322020-11-25 16:27:19 -0800380public:
David Tolnay365fc7c2020-11-25 16:08:13 -0800381 Opaque() = delete;
382 Opaque(const Opaque &) = delete;
383 ~Opaque() = delete;
384};
385#endif // CXXBRIDGE1_RUST_OPAQUE
386
David Tolnayee6ecfc2020-12-26 21:54:37 -0800387template <typename T>
388std::size_t size_of();
389template <typename T>
390std::size_t align_of();
391
David Tolnay174bd952020-11-02 09:23:12 -0800392// IsRelocatable<T> is used in assertions that a C++ type passed by value
393// between Rust and C++ is soundly relocatable by Rust.
394//
395// There may be legitimate reasons to opt out of the check for support of types
396// that the programmer knows are soundly Rust-movable despite not being
397// recognized as such by the C++ type system due to a move constructor or
398// destructor. To opt out of the relocatability check, do either of the
399// following things in any header used by `include!` in the bridge.
400//
401// --- if you define the type:
402// struct MyType {
403// ...
404// + using IsRelocatable = std::true_type;
405// };
406//
407// --- otherwise:
408// + template <>
409// + struct rust::IsRelocatable<MyType> : std::true_type {};
410template <typename T>
411struct IsRelocatable;
412
David Tolnaybe3cbf72020-12-12 22:12:07 -0800413using u8 = std::uint8_t;
414using u16 = std::uint16_t;
415using u32 = std::uint32_t;
416using u64 = std::uint64_t;
417using usize = std::size_t; // see static asserts in cxx.cc
418using i8 = std::int8_t;
419using i16 = std::int16_t;
420using i32 = std::int32_t;
421using i64 = std::int64_t;
David Tolnay1e784a32020-12-12 21:34:47 -0800422using f32 = float;
423using f64 = double;
424
David Tolnay3b0c9882020-03-01 14:08:57 -0800425// Snake case aliases for use in code that uses this style for type names.
426using string = String;
427using str = Str;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800428template <typename T>
David Tolnay38c87642020-09-06 22:18:08 -0700429using slice = Slice<T>;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800430template <typename T>
David Tolnayf262d382020-04-11 22:12:40 -0700431using box = Box<T>;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800432template <typename T>
David Tolnay38c87642020-09-06 22:18:08 -0700433using vec = Vec<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700434using error = Error;
David Tolnayf262d382020-04-11 22:12:40 -0700435template <typename Signature>
David Tolnayf031c322020-11-29 19:41:33 -0800436using fn = Fn<Signature>;
David Tolnay174bd952020-11-02 09:23:12 -0800437template <typename T>
438using is_relocatable = IsRelocatable<T>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800439
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700440
441
442////////////////////////////////////////////////////////////////////////////////
443/// end public API, begin implementation details
444
David Tolnay0f0162f2020-11-16 23:43:37 -0800445#ifndef CXXBRIDGE1_PANIC
446#define CXXBRIDGE1_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700447template <typename Exception>
448void panic [[noreturn]] (const char *msg);
David Tolnay0f0162f2020-11-16 23:43:37 -0800449#endif // CXXBRIDGE1_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700450
David Tolnay0f0162f2020-11-16 23:43:37 -0800451#ifndef CXXBRIDGE1_RUST_FN
452#define CXXBRIDGE1_RUST_FN
David Tolnayf031c322020-11-29 19:41:33 -0800453template <typename Ret, typename... Args>
454Ret Fn<Ret(Args...)>::operator()(Args... args) const noexcept {
David Tolnay75dca2e2020-03-25 20:17:52 -0700455 return (*this->trampoline)(std::move(args)..., this->fn);
456}
457
David Tolnayf031c322020-11-29 19:41:33 -0800458template <typename Ret, typename... Args>
459Fn<Ret(Args...)> Fn<Ret(Args...)>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700460 return *this;
461}
David Tolnay0f0162f2020-11-16 23:43:37 -0800462#endif // CXXBRIDGE1_RUST_FN
David Tolnaya23129c2020-04-08 20:08:21 -0700463
David Tolnay0f0162f2020-11-16 23:43:37 -0800464#ifndef CXXBRIDGE1_RUST_BITCOPY
465#define CXXBRIDGE1_RUST_BITCOPY
David Tolnay48521222020-10-31 14:59:42 -0700466struct unsafe_bitcopy_t final {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700467 explicit unsafe_bitcopy_t() = default;
468};
469
470constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnay0f0162f2020-11-16 23:43:37 -0800471#endif // CXXBRIDGE1_RUST_BITCOPY
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700472
David Tolnay0f0162f2020-11-16 23:43:37 -0800473#ifndef CXXBRIDGE1_RUST_STR
474#define CXXBRIDGE1_RUST_STR
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700475inline const char *Str::data() const noexcept { return this->ptr; }
476
David Tolnaybe3cbf72020-12-12 22:12:07 -0800477inline std::size_t Str::size() const noexcept { return this->len; }
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700478
David Tolnaybe3cbf72020-12-12 22:12:07 -0800479inline std::size_t Str::length() const noexcept { return this->len; }
David Tolnay0f0162f2020-11-16 23:43:37 -0800480#endif // CXXBRIDGE1_RUST_STR
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700481
David Tolnay0f0162f2020-11-16 23:43:37 -0800482#ifndef CXXBRIDGE1_RUST_SLICE
483#define CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700484template <typename T>
David Tolnayeee622c2020-12-27 01:26:17 -0800485Slice<T>::Slice() noexcept
David Tolnay5ce110e2020-12-27 02:45:53 -0800486 : ptr(reinterpret_cast<void *>(align_of<T>())), len(0) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700487
488template <typename T>
David Tolnay5ce110e2020-12-27 02:45:53 -0800489Slice<T>::Slice(T *s, std::size_t count) noexcept
490 : ptr(const_cast<typename std::remove_const<T>::type *>(s)), len(count) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700491
492template <typename T>
David Tolnayce298232020-11-11 10:08:54 -0800493T *Slice<T>::data() const noexcept {
David Tolnay5ce110e2020-12-27 02:45:53 -0800494 return reinterpret_cast<T *>(this->ptr);
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700495}
496
497template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800498std::size_t Slice<T>::size() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700499 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700500}
501
502template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800503std::size_t Slice<T>::length() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700504 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700505}
David Tolnayac6cb542020-11-25 20:18:55 -0800506
507template <typename T>
David Tolnay78dd5352020-12-26 23:44:20 -0800508bool Slice<T>::empty() const noexcept {
509 return this->len == 0;
510}
511
512template <typename T>
513T &Slice<T>::operator[](std::size_t n) const noexcept {
514 assert(n < this->size());
David Tolnay5ce110e2020-12-27 02:45:53 -0800515 auto pos = static_cast<char *>(this->ptr) + size_of<T>() * n;
516 return *reinterpret_cast<T *>(pos);
David Tolnay78dd5352020-12-26 23:44:20 -0800517}
518
519template <typename T>
520T &Slice<T>::at(std::size_t n) const {
521 if (n >= this->size()) {
522 panic<std::out_of_range>("rust::Slice index out of range");
523 }
524 return (*this)[n];
525}
526
527template <typename T>
528T &Slice<T>::front() const noexcept {
529 assert(!this->empty());
David Tolnay5ce110e2020-12-27 02:45:53 -0800530 return (*this)[0];
David Tolnay78dd5352020-12-26 23:44:20 -0800531}
532
533template <typename T>
534T &Slice<T>::back() const noexcept {
535 assert(!this->empty());
David Tolnay5ce110e2020-12-27 02:45:53 -0800536 return (*this)[this->len - 1];
David Tolnay78dd5352020-12-26 23:44:20 -0800537}
538
539template <typename T>
David Tolnaydb388c92020-12-27 00:28:26 -0800540typename Slice<T>::iterator::reference
541Slice<T>::iterator::operator*() const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800542 return *static_cast<T *>(this->pos);
David Tolnayac6cb542020-11-25 20:18:55 -0800543}
544
545template <typename T>
David Tolnaydb388c92020-12-27 00:28:26 -0800546typename Slice<T>::iterator::pointer
547Slice<T>::iterator::operator->() const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800548 return static_cast<T *>(this->pos);
David Tolnayac6cb542020-11-25 20:18:55 -0800549}
550
551template <typename T>
David Tolnayc30e4472020-12-27 00:25:42 -0800552typename Slice<T>::iterator::reference Slice<T>::iterator::operator[](
553 typename Slice<T>::iterator::difference_type n) const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800554 auto pos = static_cast<char *>(this->pos) + this->stride * n;
555 return *static_cast<T *>(pos);
David Tolnayc30e4472020-12-27 00:25:42 -0800556}
557
558template <typename T>
David Tolnayac6cb542020-11-25 20:18:55 -0800559typename Slice<T>::iterator &Slice<T>::iterator::operator++() noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800560 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnayac6cb542020-11-25 20:18:55 -0800561 return *this;
562}
563
564template <typename T>
565typename Slice<T>::iterator Slice<T>::iterator::operator++(int) noexcept {
566 auto ret = iterator(*this);
David Tolnaya1ddbf82020-12-27 00:56:35 -0800567 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnayac6cb542020-11-25 20:18:55 -0800568 return ret;
569}
570
571template <typename T>
David Tolnayc30e4472020-12-27 00:25:42 -0800572typename Slice<T>::iterator &Slice<T>::iterator::operator--() noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800573 this->pos = static_cast<char *>(this->pos) - this->stride;
David Tolnayc30e4472020-12-27 00:25:42 -0800574 return *this;
575}
576
577template <typename T>
578typename Slice<T>::iterator Slice<T>::iterator::operator--(int) noexcept {
579 auto ret = iterator(*this);
David Tolnaya1ddbf82020-12-27 00:56:35 -0800580 this->pos = static_cast<char *>(this->pos) - this->stride;
David Tolnayc30e4472020-12-27 00:25:42 -0800581 return ret;
582}
583
584template <typename T>
585typename Slice<T>::iterator &Slice<T>::iterator::operator+=(
586 typename Slice<T>::iterator::difference_type n) noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800587 this->pos = static_cast<char *>(this->pos) + this->stride * n;
David Tolnayc30e4472020-12-27 00:25:42 -0800588 return *this;
589}
590
591template <typename T>
592typename Slice<T>::iterator &Slice<T>::iterator::operator-=(
593 typename Slice<T>::iterator::difference_type n) noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800594 this->pos = static_cast<char *>(this->pos) - this->stride * n;
David Tolnayc30e4472020-12-27 00:25:42 -0800595 return *this;
596}
597
598template <typename T>
599typename Slice<T>::iterator Slice<T>::iterator::operator+(
600 typename Slice<T>::iterator::difference_type n) const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800601 auto ret = iterator(*this);
602 ret.pos = static_cast<char *>(this->pos) + this->stride * n;
603 return ret;
David Tolnayc30e4472020-12-27 00:25:42 -0800604}
605
606template <typename T>
607typename Slice<T>::iterator Slice<T>::iterator::operator-(
608 typename Slice<T>::iterator::difference_type n) const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800609 auto ret = iterator(*this);
610 ret.pos = static_cast<char *>(this->pos) - this->stride * n;
611 return ret;
David Tolnayc30e4472020-12-27 00:25:42 -0800612}
613
614template <typename T>
615typename Slice<T>::iterator::difference_type
616Slice<T>::iterator::operator-(const iterator &other) const noexcept {
David Tolnaya1ddbf82020-12-27 00:56:35 -0800617 auto diff = std::distance(static_cast<char *>(other.pos),
618 static_cast<char *>(this->pos));
619 return diff / this->stride;
David Tolnayc30e4472020-12-27 00:25:42 -0800620}
621
622template <typename T>
David Tolnayac6cb542020-11-25 20:18:55 -0800623bool Slice<T>::iterator::operator==(const iterator &other) const noexcept {
624 return this->pos == other.pos;
625}
626
627template <typename T>
628bool Slice<T>::iterator::operator!=(const iterator &other) const noexcept {
629 return this->pos != other.pos;
630}
631
632template <typename T>
David Tolnayc30e4472020-12-27 00:25:42 -0800633bool Slice<T>::iterator::operator<(const iterator &other) const noexcept {
634 return this->pos < other.pos;
635}
636
637template <typename T>
638bool Slice<T>::iterator::operator<=(const iterator &other) const noexcept {
639 return this->pos <= other.pos;
640}
641
642template <typename T>
643bool Slice<T>::iterator::operator>(const iterator &other) const noexcept {
644 return this->pos > other.pos;
645}
646
647template <typename T>
648bool Slice<T>::iterator::operator>=(const iterator &other) const noexcept {
649 return this->pos >= other.pos;
650}
651
652template <typename T>
David Tolnayac6cb542020-11-25 20:18:55 -0800653typename Slice<T>::iterator Slice<T>::begin() const noexcept {
654 iterator it;
David Tolnay5ce110e2020-12-27 02:45:53 -0800655 it.pos = this->ptr;
David Tolnay6f92baa2020-12-27 01:09:26 -0800656 it.stride = size_of<T>();
David Tolnayac6cb542020-11-25 20:18:55 -0800657 return it;
658}
659
660template <typename T>
661typename Slice<T>::iterator Slice<T>::end() const noexcept {
662 iterator it = this->begin();
David Tolnaya1ddbf82020-12-27 00:56:35 -0800663 it.pos = static_cast<char *>(it.pos) + it.stride * this->len;
David Tolnayac6cb542020-11-25 20:18:55 -0800664 return it;
665}
David Tolnay0f0162f2020-11-16 23:43:37 -0800666#endif // CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700667
David Tolnay0f0162f2020-11-16 23:43:37 -0800668#ifndef CXXBRIDGE1_RUST_BOX
669#define CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700670template <typename T>
David Tolnayc4b34222020-12-12 13:06:26 -0800671class Box<T>::uninit {};
672
673template <typename T>
David Tolnaye5703162020-12-12 16:26:35 -0800674class Box<T>::allocation {
David Tolnayf448e202020-12-12 16:42:45 -0800675 static T *alloc() noexcept;
676 static void dealloc(T *) noexcept;
David Tolnay2e637d92020-12-12 21:08:01 -0800677
David Tolnaye5703162020-12-12 16:26:35 -0800678public:
David Tolnaye7d662d2020-12-12 16:38:22 -0800679 allocation() noexcept : ptr(alloc()) {}
680 ~allocation() noexcept {
681 if (this->ptr) {
682 dealloc(this->ptr);
683 }
684 }
David Tolnaye5703162020-12-12 16:26:35 -0800685 T *ptr;
686};
687
688template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700689Box<T>::Box(const Box &other) : Box(*other) {}
690
691template <typename T>
692Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
693 other.ptr = nullptr;
694}
695
696template <typename T>
David Tolnaye7d662d2020-12-12 16:38:22 -0800697Box<T>::Box(const T &val) {
698 allocation alloc;
699 ::new (alloc.ptr) T(val);
700 this->ptr = alloc.ptr;
701 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700702}
703
704template <typename T>
David Tolnaye7d662d2020-12-12 16:38:22 -0800705Box<T>::Box(T &&val) {
706 allocation alloc;
707 ::new (alloc.ptr) T(std::move(val));
708 this->ptr = alloc.ptr;
709 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700710}
711
712template <typename T>
713Box<T>::~Box() noexcept {
714 if (this->ptr) {
715 this->drop();
716 }
717}
718
719template <typename T>
720Box<T> &Box<T>::operator=(const Box &other) {
David Tolnay439ed0b2020-12-12 17:01:04 -0800721 if (this->ptr) {
722 **this = *other;
723 } else {
724 allocation alloc;
725 ::new (alloc.ptr) T(*other);
726 this->ptr = alloc.ptr;
727 alloc.ptr = nullptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700728 }
729 return *this;
730}
731
732template <typename T>
733Box<T> &Box<T>::operator=(Box &&other) noexcept {
734 if (this->ptr) {
735 this->drop();
736 }
737 this->ptr = other.ptr;
738 other.ptr = nullptr;
739 return *this;
740}
741
742template <typename T>
743const T *Box<T>::operator->() const noexcept {
744 return this->ptr;
745}
746
747template <typename T>
748const T &Box<T>::operator*() const noexcept {
749 return *this->ptr;
750}
751
752template <typename T>
753T *Box<T>::operator->() noexcept {
754 return this->ptr;
755}
756
757template <typename T>
758T &Box<T>::operator*() noexcept {
759 return *this->ptr;
760}
761
762template <typename T>
763template <typename... Fields>
764Box<T> Box<T>::in_place(Fields &&... fields) {
David Tolnaye7d662d2020-12-12 16:38:22 -0800765 allocation alloc;
766 auto ptr = alloc.ptr;
767 ::new (ptr) T{std::forward<Fields>(fields)...};
768 alloc.ptr = nullptr;
769 return from_raw(ptr);
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700770}
771
772template <typename T>
773Box<T> Box<T>::from_raw(T *raw) noexcept {
David Tolnayc4b34222020-12-12 13:06:26 -0800774 Box box = uninit{};
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700775 box.ptr = raw;
776 return box;
777}
778
779template <typename T>
780T *Box<T>::into_raw() noexcept {
781 T *raw = this->ptr;
782 this->ptr = nullptr;
783 return raw;
784}
785
786template <typename T>
David Tolnayc4b34222020-12-12 13:06:26 -0800787Box<T>::Box(uninit) noexcept {}
David Tolnay0f0162f2020-11-16 23:43:37 -0800788#endif // CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700789
David Tolnay0f0162f2020-11-16 23:43:37 -0800790#ifndef CXXBRIDGE1_RUST_VEC
791#define CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700792template <typename T>
David Tolnayf799b372020-11-29 23:57:42 -0800793Vec<T>::Vec(std::initializer_list<T> init) : Vec{} {
794 this->reserve_total(init.size());
795 std::move(init.begin(), init.end(), std::back_inserter(*this));
796}
797
798template <typename T>
David Tolnay9007e462020-12-11 13:59:08 -0800799Vec<T>::Vec(const Vec &other) : Vec() {
800 this->reserve_total(other.size());
801 std::copy(other.begin(), other.end(), std::back_inserter(*this));
802}
803
804template <typename T>
David Tolnay15671862020-11-23 18:13:56 -0800805Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700806 new (&other) Vec();
807}
808
809template <typename T>
810Vec<T>::~Vec() noexcept {
811 this->drop();
812}
813
814template <typename T>
815Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
816 if (this != &other) {
817 this->drop();
818 this->repr = other.repr;
819 new (&other) Vec();
820 }
821 return *this;
822}
823
824template <typename T>
David Tolnaydd42c722020-12-11 14:05:26 -0800825Vec<T> &Vec<T>::operator=(const Vec &other) {
826 if (this != &other) {
827 this->drop();
828 new (this) Vec(other);
829 }
830 return *this;
831}
832
833template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700834bool Vec<T>::empty() const noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800835 return this->size() == 0;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700836}
837
838template <typename T>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800839T *Vec<T>::data() noexcept {
840 return const_cast<T *>(const_cast<const Vec<T> *>(this)->data());
841}
842
843template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800844const T &Vec<T>::operator[](std::size_t n) const noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800845 assert(n < this->size());
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700846 auto data = reinterpret_cast<const char *>(this->data());
David Tolnaye1df7dd2020-12-27 02:51:51 -0800847 return *reinterpret_cast<const T *>(data + n * size_of<T>());
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700848}
849
850template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800851const T &Vec<T>::at(std::size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700852 if (n >= this->size()) {
853 panic<std::out_of_range>("rust::Vec index out of range");
854 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700855 return (*this)[n];
856}
857
858template <typename T>
David Tolnayd4fff5d2020-12-21 14:04:09 -0800859const T &Vec<T>::front() const noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800860 assert(!this->empty());
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700861 return (*this)[0];
862}
863
864template <typename T>
David Tolnayd4fff5d2020-12-21 14:04:09 -0800865const T &Vec<T>::back() const noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800866 assert(!this->empty());
David Tolnayb10c4bc2020-08-26 21:55:29 -0700867 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700868}
869
870template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800871T &Vec<T>::operator[](std::size_t n) noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800872 assert(n < this->size());
David Tolnay908d5e52020-11-30 00:30:59 -0800873 auto data = reinterpret_cast<char *>(this->data());
David Tolnaye1df7dd2020-12-27 02:51:51 -0800874 return *reinterpret_cast<T *>(data + n * size_of<T>());
David Tolnay908d5e52020-11-30 00:30:59 -0800875}
876
877template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800878T &Vec<T>::at(std::size_t n) {
David Tolnay908d5e52020-11-30 00:30:59 -0800879 if (n >= this->size()) {
880 panic<std::out_of_range>("rust::Vec index out of range");
881 }
882 return (*this)[n];
883}
884
885template <typename T>
David Tolnayd4fff5d2020-12-21 14:04:09 -0800886T &Vec<T>::front() noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800887 assert(!this->empty());
David Tolnay908d5e52020-11-30 00:30:59 -0800888 return (*this)[0];
889}
890
891template <typename T>
David Tolnayd4fff5d2020-12-21 14:04:09 -0800892T &Vec<T>::back() noexcept {
David Tolnayfa5a4a62020-12-21 14:08:08 -0800893 assert(!this->empty());
David Tolnay908d5e52020-11-30 00:30:59 -0800894 return (*this)[this->size() - 1];
895}
896
897template <typename T>
David Tolnaybe3cbf72020-12-12 22:12:07 -0800898void Vec<T>::reserve(std::size_t new_cap) {
David Tolnayfb6b73c2020-11-10 14:32:16 -0800899 this->reserve_total(new_cap);
900}
901
902template <typename T>
903void Vec<T>::push_back(const T &value) {
904 this->emplace_back(value);
905}
906
907template <typename T>
908void Vec<T>::push_back(T &&value) {
909 this->emplace_back(std::move(value));
910}
911
912template <typename T>
913template <typename... Args>
914void Vec<T>::emplace_back(Args &&... args) {
915 auto size = this->size();
916 this->reserve_total(size + 1);
917 ::new (reinterpret_cast<T *>(reinterpret_cast<char *>(this->data()) +
David Tolnaye1df7dd2020-12-27 02:51:51 -0800918 size * size_of<T>()))
David Tolnayfb6b73c2020-11-10 14:32:16 -0800919 T(std::forward<Args>(args)...);
920 this->set_len(size + 1);
921}
922
923template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800924typename Vec<T>::iterator Vec<T>::begin() noexcept {
David Tolnay725bf502020-12-27 02:47:40 -0800925 return Slice<T>(this->data(), this->size()).begin();
David Tolnay960b5112020-11-25 13:18:28 -0800926}
927
928template <typename T>
929typename Vec<T>::iterator Vec<T>::end() noexcept {
David Tolnay725bf502020-12-27 02:47:40 -0800930 return Slice<T>(this->data(), this->size()).end();
David Tolnay960b5112020-11-25 13:18:28 -0800931}
932
933template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700934typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800935 return this->cbegin();
936}
937
938template <typename T>
939typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
940 return this->cend();
941}
942
943template <typename T>
944typename Vec<T>::const_iterator Vec<T>::cbegin() const noexcept {
David Tolnay725bf502020-12-27 02:47:40 -0800945 return Slice<const T>(this->data(), this->size()).begin();
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700946}
947
948template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800949typename Vec<T>::const_iterator Vec<T>::cend() const noexcept {
David Tolnay725bf502020-12-27 02:47:40 -0800950 return Slice<const T>(this->data(), this->size()).end();
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700951}
952
953// Internal API only intended for the cxxbridge code generator.
954template <typename T>
955Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay0f0162f2020-11-16 23:43:37 -0800956#endif // CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700957
David Tolnay75068632020-12-26 22:15:17 -0800958#ifndef CXXBRIDGE1_IS_COMPLETE
959#define CXXBRIDGE1_IS_COMPLETE
960namespace detail {
961namespace {
962template <typename T, typename = std::size_t>
963struct is_complete : std::false_type {};
964template <typename T>
965struct is_complete<T, decltype(sizeof(T))> : std::true_type {};
966} // namespace
967} // namespace detail
968#endif // CXXBRIDGE1_IS_COMPLETE
969
David Tolnayee6ecfc2020-12-26 21:54:37 -0800970#ifndef CXXBRIDGE1_LAYOUT
971#define CXXBRIDGE1_LAYOUT
972class layout {
973 template <typename T>
974 friend std::size_t size_of();
975 template <typename T>
976 friend std::size_t align_of();
977 template <typename T>
978 static typename std::enable_if<std::is_base_of<Opaque, T>::value,
979 std::size_t>::type
980 do_size_of() {
981 return T::layout::size();
982 }
983 template <typename T>
984 static typename std::enable_if<!std::is_base_of<Opaque, T>::value,
985 std::size_t>::type
986 do_size_of() {
987 return sizeof(T);
988 }
989 template <typename T>
990 static
991 typename std::enable_if<detail::is_complete<T>::value, std::size_t>::type
992 size_of() {
993 return do_size_of<T>();
994 }
995 template <typename T>
996 static typename std::enable_if<std::is_base_of<Opaque, T>::value,
997 std::size_t>::type
998 do_align_of() {
999 return T::layout::align();
1000 }
1001 template <typename T>
1002 static typename std::enable_if<!std::is_base_of<Opaque, T>::value,
1003 std::size_t>::type
1004 do_align_of() {
1005 return alignof(T);
1006 }
1007 template <typename T>
1008 static
1009 typename std::enable_if<detail::is_complete<T>::value, std::size_t>::type
1010 align_of() {
1011 return do_align_of<T>();
1012 }
1013};
1014
1015template <typename T>
1016std::size_t size_of() {
1017 return layout::size_of<T>();
1018}
1019
1020template <typename T>
1021std::size_t align_of() {
1022 return layout::align_of<T>();
1023}
1024#endif // CXXBRIDGE1_LAYOUT
1025
David Tolnay0f0162f2020-11-16 23:43:37 -08001026#ifndef CXXBRIDGE1_RELOCATABLE
1027#define CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -08001028namespace detail {
1029template <typename... Ts>
1030struct make_void {
1031 using type = void;
1032};
1033
1034template <typename... Ts>
1035using void_t = typename make_void<Ts...>::type;
1036
1037template <typename Void, template <typename...> class, typename...>
1038struct detect : std::false_type {};
1039template <template <typename...> class T, typename... A>
1040struct detect<void_t<T<A...>>, T, A...> : std::true_type {};
1041
1042template <template <typename...> class T, typename... A>
1043using is_detected = detect<void, T, A...>;
1044
1045template <typename T>
1046using detect_IsRelocatable = typename T::IsRelocatable;
1047
1048template <typename T>
1049struct get_IsRelocatable
1050 : std::is_same<typename T::IsRelocatable, std::true_type> {};
1051} // namespace detail
1052
1053template <typename T>
1054struct IsRelocatable
1055 : std::conditional<
1056 detail::is_detected<detail::detect_IsRelocatable, T>::value,
1057 detail::get_IsRelocatable<T>,
1058 std::integral_constant<
1059 bool, std::is_trivially_move_constructible<T>::value &&
1060 std::is_trivially_destructible<T>::value>>::type {};
David Tolnay0f0162f2020-11-16 23:43:37 -08001061#endif // CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -08001062
David Tolnay0f0162f2020-11-16 23:43:37 -08001063} // namespace cxxbridge1
David Tolnay750755e2020-03-01 13:04:08 -08001064} // namespace rust