blob: 31b34fc78afa8ff290020a1b4802e168326593f6 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001#pragma once
2#include <array>
David Tolnay30430f12020-03-19 20:49:00 -07003#include <cstddef>
David Tolnay7db73692019-10-20 14:51:12 -04004#include <cstdint>
David Tolnayb7a7cb62020-03-17 21:18:40 -07005#include <exception>
David Tolnay001102a2020-03-01 20:05:04 -08006#include <iosfwd>
David Tolnayd1df4c72020-11-25 20:38:05 -08007#include <iterator>
David Tolnay0ecd05a2020-07-29 16:32:03 -07008#include <new>
Stephen Crane9e48d5b2020-08-21 12:17:02 -07009#include <stdexcept>
David Tolnay7db73692019-10-20 14:51:12 -040010#include <string>
David Tolnayf6292372020-03-01 21:09:11 -080011#include <type_traits>
David Tolnay4791f1c2020-03-17 21:53:16 -070012#include <utility>
David Tolnay37dd7e12020-04-25 12:51:59 -070013#include <vector>
David Tolnay59b5ba12020-04-10 11:32:19 -070014#if defined(_WIN32)
David Tolnayda38b7c2020-09-16 11:50:04 -040015#include <basetsd.h>
David Tolnay59b5ba12020-04-10 11:32:19 -070016#endif
David Tolnay7db73692019-10-20 14:51:12 -040017
David Tolnay750755e2020-03-01 13:04:08 -080018namespace rust {
David Tolnay0f0162f2020-11-16 23:43:37 -080019inline namespace cxxbridge1 {
David Tolnay7db73692019-10-20 14:51:12 -040020
David Tolnay2a2b9ad2020-05-12 20:07:26 -070021struct unsafe_bitcopy_t;
David Tolnayd1e2efc2020-03-03 22:25:43 -080022
David Tolnay84ddf9e2020-10-31 15:36:48 -070023namespace {
24template <typename T>
25class impl;
26}
27
David Tolnay0f0162f2020-11-16 23:43:37 -080028#ifndef CXXBRIDGE1_RUST_STRING
29#define CXXBRIDGE1_RUST_STRING
David Tolnaye468f052020-11-29 19:39:35 -080030// https://cxx.rs/binding/string.html
David Tolnay56082162020-03-01 12:57:33 -080031class String final {
David Tolnay7db73692019-10-20 14:51:12 -040032public:
David Tolnay56082162020-03-01 12:57:33 -080033 String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080034 String(const String &) noexcept;
35 String(String &&) noexcept;
David Tolnay56082162020-03-01 12:57:33 -080036 ~String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080037
38 String(const std::string &);
39 String(const char *);
David Tolnayc2bbd952020-07-29 18:15:26 -070040 String(const char *, size_t);
David Tolnayd9c4ac92020-03-01 20:33:58 -080041
42 String &operator=(const String &) noexcept;
43 String &operator=(String &&) noexcept;
44
David Tolnay404d6892020-03-01 20:19:41 -080045 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040046
47 // Note: no null terminator.
48 const char *data() const noexcept;
49 size_t size() const noexcept;
50 size_t length() const noexcept;
51
David Tolnayff7f5fb2020-11-25 20:50:32 -080052 using iterator = char *;
53 iterator begin() noexcept;
54 iterator end() noexcept;
55
56 using const_iterator = const char *;
57 const_iterator begin() const noexcept;
58 const_iterator end() const noexcept;
59 const_iterator cbegin() const noexcept;
60 const_iterator cend() const noexcept;
61
David Tolnayff86dce2020-11-29 19:45:13 -080062 bool operator==(const String &) const noexcept;
63 bool operator!=(const String &) const noexcept;
64 bool operator<(const String &) const noexcept;
65 bool operator<=(const String &) const noexcept;
66 bool operator>(const String &) const noexcept;
67 bool operator>=(const String &) const noexcept;
68
David Tolnayd1e2efc2020-03-03 22:25:43 -080069 // Internal API only intended for the cxxbridge code generator.
70 String(unsafe_bitcopy_t, const String &) noexcept;
71
David Tolnay7db73692019-10-20 14:51:12 -040072private:
73 // Size and alignment statically verified by rust_string.rs.
74 std::array<uintptr_t, 3> repr;
75};
David Tolnay0f0162f2020-11-16 23:43:37 -080076#endif // CXXBRIDGE1_RUST_STRING
David Tolnay7db73692019-10-20 14:51:12 -040077
David Tolnay0f0162f2020-11-16 23:43:37 -080078#ifndef CXXBRIDGE1_RUST_STR
David Tolnaye468f052020-11-29 19:39:35 -080079// https://cxx.rs/binding/str.html
David Tolnay09dbe752020-03-01 13:00:40 -080080class Str final {
David Tolnay7db73692019-10-20 14:51:12 -040081public:
David Tolnay09dbe752020-03-01 13:00:40 -080082 Str() noexcept;
David Tolnay828e5132020-11-29 20:40:40 -080083 Str(const String &) noexcept;
David Tolnay851677c2020-03-01 23:49:46 -080084 Str(const std::string &);
85 Str(const char *);
David Tolnay894c5e42020-07-29 18:20:00 -070086 Str(const char *, size_t);
David Tolnayd9c4ac92020-03-01 20:33:58 -080087
David Tolnay2d7f1172020-10-31 17:58:31 -070088 Str &operator=(const Str &) noexcept = default;
David Tolnayd9c4ac92020-03-01 20:33:58 -080089
David Tolnay404d6892020-03-01 20:19:41 -080090 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040091
92 // Note: no null terminator.
93 const char *data() const noexcept;
94 size_t size() const noexcept;
95 size_t length() const noexcept;
96
David Tolnayde9a5b12020-10-31 12:15:43 -070097 // Important in order for System V ABI to pass in registers.
98 Str(const Str &) noexcept = default;
99 ~Str() noexcept = default;
100
David Tolnayff7f5fb2020-11-25 20:50:32 -0800101 using iterator = const char *;
102 using const_iterator = const char *;
103 const_iterator begin() const noexcept;
104 const_iterator end() const noexcept;
105 const_iterator cbegin() const noexcept;
106 const_iterator cend() const noexcept;
107
David Tolnayff86dce2020-11-29 19:45:13 -0800108 bool operator==(const Str &) const noexcept;
109 bool operator!=(const Str &) const noexcept;
110 bool operator<(const Str &) const noexcept;
111 bool operator<=(const Str &) const noexcept;
112 bool operator>(const Str &) const noexcept;
113 bool operator>=(const Str &) const noexcept;
114
David Tolnay5df1f062020-10-31 12:31:10 -0700115private:
David Tolnay0356d332020-10-31 19:46:41 -0700116 friend impl<Str>;
David Tolnay7db73692019-10-20 14:51:12 -0400117 // Not necessarily ABI compatible with &str. Codegen will translate to
118 // cxx::rust_str::RustStr which matches this layout.
David Tolnay5df1f062020-10-31 12:31:10 -0700119 const char *ptr;
120 size_t len;
David Tolnay7db73692019-10-20 14:51:12 -0400121};
David Tolnay0f0162f2020-11-16 23:43:37 -0800122#endif // CXXBRIDGE1_RUST_STR
David Tolnay7db73692019-10-20 14:51:12 -0400123
David Tolnay0f0162f2020-11-16 23:43:37 -0800124#ifndef CXXBRIDGE1_RUST_SLICE
David Tolnayc5629f02020-11-23 18:32:46 -0800125namespace detail {
David Tolnayee9b9ee2020-11-25 08:28:50 -0800126template <bool>
David Tolnayc5629f02020-11-23 18:32:46 -0800127struct copy_assignable_if {};
David Tolnayce298232020-11-11 10:08:54 -0800128
David Tolnayc5629f02020-11-23 18:32:46 -0800129template <>
130struct copy_assignable_if<false> {
131 copy_assignable_if() noexcept = default;
132 copy_assignable_if(const copy_assignable_if &) noexcept = default;
133 copy_assignable_if &operator=(const copy_assignable_if &) noexcept = delete;
134 copy_assignable_if &operator=(copy_assignable_if &&) noexcept = default;
135};
136} // namespace detail
137
David Tolnaye468f052020-11-29 19:39:35 -0800138// https://cxx.rs/binding/slice.html
David Tolnayc5629f02020-11-23 18:32:46 -0800139template <typename T>
140class Slice final
141 : private detail::copy_assignable_if<std::is_const<T>::value> {
David Tolnayefe81052020-04-14 16:28:24 -0700142public:
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700143 Slice() noexcept;
David Tolnayce298232020-11-11 10:08:54 -0800144 Slice(T *, size_t count) noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700145
David Tolnay2d7f1172020-10-31 17:58:31 -0700146 Slice &operator=(const Slice<T> &) noexcept = default;
David Tolnayc5629f02020-11-23 18:32:46 -0800147 Slice &operator=(Slice<T> &&) noexcept = default;
David Tolnayefe81052020-04-14 16:28:24 -0700148
David Tolnayce298232020-11-11 10:08:54 -0800149 T *data() const noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700150 size_t size() const noexcept;
151 size_t length() const noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700152
David Tolnayde9a5b12020-10-31 12:15:43 -0700153 // Important in order for System V ABI to pass in registers.
154 Slice(const Slice<T> &) noexcept = default;
155 ~Slice() noexcept = default;
156
David Tolnayac6cb542020-11-25 20:18:55 -0800157 class iterator;
158 iterator begin() const noexcept;
159 iterator end() const noexcept;
160
David Tolnayefe81052020-04-14 16:28:24 -0700161private:
David Tolnay36aa9e02020-10-31 23:08:21 -0700162 friend impl<Slice>;
163 // Not necessarily ABI compatible with &[T]. Codegen will translate to
David Tolnay5515a9e2020-11-25 19:07:54 -0800164 // cxx::rust_slice::RustSlice which matches this layout.
David Tolnayce298232020-11-11 10:08:54 -0800165 T *ptr;
David Tolnay36aa9e02020-10-31 23:08:21 -0700166 size_t len;
David Tolnayefe81052020-04-14 16:28:24 -0700167};
David Tolnayac6cb542020-11-25 20:18:55 -0800168
169template <typename T>
170class Slice<T>::iterator final {
171public:
172 using difference_type = ptrdiff_t;
173 using value_type = T;
174 using pointer = typename std::add_pointer<T>::type;
175 using reference = typename std::add_lvalue_reference<T>::type;
176 using iterator_category = std::forward_iterator_tag;
177
178 T &operator*() const noexcept;
179 T *operator->() const noexcept;
180 iterator &operator++() noexcept;
181 iterator operator++(int) noexcept;
182 bool operator==(const iterator &) const noexcept;
183 bool operator!=(const iterator &) const noexcept;
184
185private:
186 friend class Slice;
187 T *pos;
188};
David Tolnay0f0162f2020-11-16 23:43:37 -0800189#endif // CXXBRIDGE1_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -0700190
David Tolnay0f0162f2020-11-16 23:43:37 -0800191#ifndef CXXBRIDGE1_RUST_BOX
David Tolnaye468f052020-11-29 19:39:35 -0800192// https://cxx.rs/binding/box.html
David Tolnayf262d382020-04-11 22:12:40 -0700193template <typename T>
194class Box final {
David Tolnay7db73692019-10-20 14:51:12 -0400195public:
David Tolnayf6292372020-03-01 21:09:11 -0800196 using value_type = T;
David Tolnay9706a512020-04-24 17:09:01 -0700197 using const_pointer =
198 typename std::add_pointer<typename std::add_const<T>::type>::type;
199 using pointer = typename std::add_pointer<T>::type;
David Tolnayf6292372020-03-01 21:09:11 -0800200
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700201 Box(const Box &);
202 Box(Box &&) noexcept;
203 ~Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400204
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700205 explicit Box(const T &);
206 explicit Box(T &&);
207
208 Box &operator=(const Box &);
209 Box &operator=(Box &&) noexcept;
210
211 const T *operator->() const noexcept;
212 const T &operator*() const noexcept;
213 T *operator->() noexcept;
214 T &operator*() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400215
David Tolnayf262d382020-04-11 22:12:40 -0700216 template <typename... Fields>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700217 static Box in_place(Fields &&...);
David Tolnay7ce59fc2020-04-11 11:46:33 -0700218
David Tolnay7db73692019-10-20 14:51:12 -0400219 // Important: requires that `raw` came from an into_raw call. Do not pass a
220 // pointer from `new` or any other source.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700221 static Box from_raw(T *) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400222
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700223 T *into_raw() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400224
225private:
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700226 Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400227 void uninit() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400228 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800229 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400230};
David Tolnay0f0162f2020-11-16 23:43:37 -0800231#endif // CXXBRIDGE1_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400232
David Tolnay0f0162f2020-11-16 23:43:37 -0800233#ifndef CXXBRIDGE1_RUST_VEC
David Tolnaye468f052020-11-29 19:39:35 -0800234// https://cxx.rs/binding/vec.html
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700235template <typename T>
236class Vec final {
237public:
David Tolnayc87c2152020-04-24 17:07:41 -0700238 using value_type = T;
239
David Tolnayf97c2d52020-04-25 16:37:48 -0700240 Vec() noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700241 Vec(Vec &&) noexcept;
242 ~Vec() noexcept;
David Tolnaycb800572020-04-24 20:30:43 -0700243
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700244 Vec &operator=(Vec &&) noexcept;
David Tolnayf97c2d52020-04-25 16:37:48 -0700245
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700246 size_t size() const noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700247 bool empty() const noexcept;
David Tolnay219c0792020-04-24 20:31:37 -0700248 const T *data() const noexcept;
David Tolnayfb6b73c2020-11-10 14:32:16 -0800249 T *data() noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700250
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700251 const T &operator[](size_t n) const noexcept;
252 const T &at(size_t n) const;
253
254 const T &front() const;
255 const T &back() const;
256
David Tolnayfb6b73c2020-11-10 14:32:16 -0800257 void reserve(size_t new_cap);
258 void push_back(const T &value);
259 void push_back(T &&value);
David Tolnay4e8c49a2020-11-11 10:00:18 -0800260 template <typename... Args>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800261 void emplace_back(Args &&... args);
262
David Tolnay960b5112020-11-25 13:18:28 -0800263 class iterator;
264 iterator begin() noexcept;
265 iterator end() noexcept;
266
David Tolnaya5d72c62020-11-25 13:57:16 -0800267 using const_iterator = typename Vec<const T>::iterator;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700268 const_iterator begin() const noexcept;
269 const_iterator end() const noexcept;
David Tolnay960b5112020-11-25 13:18:28 -0800270 const_iterator cbegin() const noexcept;
271 const_iterator cend() const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700272
David Tolnay313b10e2020-04-25 16:30:51 -0700273 // Internal API only intended for the cxxbridge code generator.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700274 Vec(unsafe_bitcopy_t, const Vec &) noexcept;
David Tolnay313b10e2020-04-25 16:30:51 -0700275
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700276private:
David Tolnay503d0192020-04-24 22:18:56 -0700277 static size_t stride() noexcept;
David Tolnayfb6b73c2020-11-10 14:32:16 -0800278 void reserve_total(size_t cap) noexcept;
279 void set_len(size_t len) noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700280 void drop() noexcept;
281
282 // Size and alignment statically verified by rust_vec.rs.
283 std::array<uintptr_t, 3> repr;
284};
David Tolnay66f216c2020-11-25 13:21:00 -0800285
286template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800287class Vec<T>::iterator final {
288public:
289 using difference_type = ptrdiff_t;
290 using value_type = T;
291 using pointer = typename std::add_pointer<T>::type;
292 using reference = typename std::add_lvalue_reference<T>::type;
293 using iterator_category = std::forward_iterator_tag;
294
295 T &operator*() const noexcept;
296 T *operator->() const noexcept;
297 iterator &operator++() noexcept;
298 iterator operator++(int) noexcept;
299 bool operator==(const iterator &) const noexcept;
300 bool operator!=(const iterator &) const noexcept;
301
302private:
303 friend class Vec;
David Tolnaya5d72c62020-11-25 13:57:16 -0800304 friend class Vec<typename std::remove_const<T>::type>;
David Tolnay960b5112020-11-25 13:18:28 -0800305 void *pos;
306 size_t stride;
307};
David Tolnay0f0162f2020-11-16 23:43:37 -0800308#endif // CXXBRIDGE1_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700309
David Tolnay0f0162f2020-11-16 23:43:37 -0800310#ifndef CXXBRIDGE1_RUST_FN
David Tolnaye468f052020-11-29 19:39:35 -0800311// https://cxx.rs/binding/fn.html
David Tolnayf031c322020-11-29 19:41:33 -0800312template <typename Signature>
David Tolnayf262d382020-04-11 22:12:40 -0700313class Fn;
David Tolnay75dca2e2020-03-25 20:17:52 -0700314
David Tolnayf031c322020-11-29 19:41:33 -0800315template <typename Ret, typename... Args>
316class Fn<Ret(Args...)> final {
David Tolnay75dca2e2020-03-25 20:17:52 -0700317public:
David Tolnayf031c322020-11-29 19:41:33 -0800318 Ret operator()(Args... args) const noexcept;
David Tolnay533d4582020-04-08 20:29:14 -0700319 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700320
321private:
David Tolnayf031c322020-11-29 19:41:33 -0800322 Ret (*trampoline)(Args..., void *fn) noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700323 void *fn;
324};
David Tolnay0f0162f2020-11-16 23:43:37 -0800325#endif // CXXBRIDGE1_RUST_FN
David Tolnay75dca2e2020-03-25 20:17:52 -0700326
David Tolnay0f0162f2020-11-16 23:43:37 -0800327#ifndef CXXBRIDGE1_RUST_ERROR
328#define CXXBRIDGE1_RUST_ERROR
David Tolnaye468f052020-11-29 19:39:35 -0800329// https://cxx.rs/binding/result.html
David Tolnaye4fa8732020-09-08 15:04:56 -0700330class Error final : public std::exception {
David Tolnay1e548172020-03-16 13:37:09 -0700331public:
332 Error(const Error &);
333 Error(Error &&) noexcept;
David Tolnay2714d2c2020-11-23 18:17:43 -0800334 ~Error() noexcept override;
David Tolnay7c6ac712020-10-31 17:22:28 -0700335
336 Error &operator=(const Error &);
David Tolnay15491062020-10-31 17:25:13 -0700337 Error &operator=(Error &&) noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700338
David Tolnay1e548172020-03-16 13:37:09 -0700339 const char *what() const noexcept override;
340
341private:
David Tolnaya0c9bc72020-10-31 14:37:14 -0700342 Error() noexcept = default;
David Tolnay84ddf9e2020-10-31 15:36:48 -0700343 friend impl<Error>;
David Tolnaya0c9bc72020-10-31 14:37:14 -0700344 const char *msg;
345 size_t len;
David Tolnay1e548172020-03-16 13:37:09 -0700346};
David Tolnay0f0162f2020-11-16 23:43:37 -0800347#endif // CXXBRIDGE1_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700348
David Tolnay0f0162f2020-11-16 23:43:37 -0800349#ifndef CXXBRIDGE1_RUST_ISIZE
350#define CXXBRIDGE1_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700351#if defined(_WIN32)
352using isize = SSIZE_T;
353#else
354using isize = ssize_t;
355#endif
David Tolnay0f0162f2020-11-16 23:43:37 -0800356#endif // CXXBRIDGE1_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700357
David Tolnay851677c2020-03-01 23:49:46 -0800358std::ostream &operator<<(std::ostream &, const String &);
359std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400360
David Tolnay365fc7c2020-11-25 16:08:13 -0800361#ifndef CXXBRIDGE1_RUST_OPAQUE
362#define CXXBRIDGE1_RUST_OPAQUE
363// Base class of generated opaque Rust types.
364class Opaque {
David Tolnaya857c322020-11-25 16:27:19 -0800365public:
David Tolnay365fc7c2020-11-25 16:08:13 -0800366 Opaque() = delete;
367 Opaque(const Opaque &) = delete;
368 ~Opaque() = delete;
369};
370#endif // CXXBRIDGE1_RUST_OPAQUE
371
David Tolnay174bd952020-11-02 09:23:12 -0800372// IsRelocatable<T> is used in assertions that a C++ type passed by value
373// between Rust and C++ is soundly relocatable by Rust.
374//
375// There may be legitimate reasons to opt out of the check for support of types
376// that the programmer knows are soundly Rust-movable despite not being
377// recognized as such by the C++ type system due to a move constructor or
378// destructor. To opt out of the relocatability check, do either of the
379// following things in any header used by `include!` in the bridge.
380//
381// --- if you define the type:
382// struct MyType {
383// ...
384// + using IsRelocatable = std::true_type;
385// };
386//
387// --- otherwise:
388// + template <>
389// + struct rust::IsRelocatable<MyType> : std::true_type {};
390template <typename T>
391struct IsRelocatable;
392
David Tolnay3b0c9882020-03-01 14:08:57 -0800393// Snake case aliases for use in code that uses this style for type names.
394using string = String;
395using str = Str;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800396template <typename T>
David Tolnay38c87642020-09-06 22:18:08 -0700397using slice = Slice<T>;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800398template <typename T>
David Tolnayf262d382020-04-11 22:12:40 -0700399using box = Box<T>;
David Tolnay4e8c49a2020-11-11 10:00:18 -0800400template <typename T>
David Tolnay38c87642020-09-06 22:18:08 -0700401using vec = Vec<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700402using error = Error;
David Tolnayf262d382020-04-11 22:12:40 -0700403template <typename Signature>
David Tolnayf031c322020-11-29 19:41:33 -0800404using fn = Fn<Signature>;
David Tolnay174bd952020-11-02 09:23:12 -0800405template <typename T>
406using is_relocatable = IsRelocatable<T>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800407
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700408
409
410////////////////////////////////////////////////////////////////////////////////
411/// end public API, begin implementation details
412
David Tolnay0f0162f2020-11-16 23:43:37 -0800413#ifndef CXXBRIDGE1_PANIC
414#define CXXBRIDGE1_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700415template <typename Exception>
416void panic [[noreturn]] (const char *msg);
David Tolnay0f0162f2020-11-16 23:43:37 -0800417#endif // CXXBRIDGE1_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700418
David Tolnay0f0162f2020-11-16 23:43:37 -0800419#ifndef CXXBRIDGE1_RUST_FN
420#define CXXBRIDGE1_RUST_FN
David Tolnayf031c322020-11-29 19:41:33 -0800421template <typename Ret, typename... Args>
422Ret Fn<Ret(Args...)>::operator()(Args... args) const noexcept {
David Tolnay75dca2e2020-03-25 20:17:52 -0700423 return (*this->trampoline)(std::move(args)..., this->fn);
424}
425
David Tolnayf031c322020-11-29 19:41:33 -0800426template <typename Ret, typename... Args>
427Fn<Ret(Args...)> Fn<Ret(Args...)>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700428 return *this;
429}
David Tolnay0f0162f2020-11-16 23:43:37 -0800430#endif // CXXBRIDGE1_RUST_FN
David Tolnaya23129c2020-04-08 20:08:21 -0700431
David Tolnay0f0162f2020-11-16 23:43:37 -0800432#ifndef CXXBRIDGE1_RUST_BITCOPY
433#define CXXBRIDGE1_RUST_BITCOPY
David Tolnay48521222020-10-31 14:59:42 -0700434struct unsafe_bitcopy_t final {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700435 explicit unsafe_bitcopy_t() = default;
436};
437
438constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnay0f0162f2020-11-16 23:43:37 -0800439#endif // CXXBRIDGE1_RUST_BITCOPY
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700440
David Tolnay0f0162f2020-11-16 23:43:37 -0800441#ifndef CXXBRIDGE1_RUST_STR
442#define CXXBRIDGE1_RUST_STR
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700443inline const char *Str::data() const noexcept { return this->ptr; }
444
445inline size_t Str::size() const noexcept { return this->len; }
446
447inline size_t Str::length() const noexcept { return this->len; }
David Tolnay0f0162f2020-11-16 23:43:37 -0800448#endif // CXXBRIDGE1_RUST_STR
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700449
David Tolnay0f0162f2020-11-16 23:43:37 -0800450#ifndef CXXBRIDGE1_RUST_SLICE
451#define CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700452template <typename T>
David Tolnay7b16a392020-11-25 20:23:10 -0800453Slice<T>::Slice() noexcept : ptr(reinterpret_cast<T *>(alignof(T))), len(0) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700454
455template <typename T>
David Tolnayce298232020-11-11 10:08:54 -0800456Slice<T>::Slice(T *s, size_t count) noexcept : ptr(s), len(count) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700457
458template <typename T>
David Tolnayce298232020-11-11 10:08:54 -0800459T *Slice<T>::data() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700460 return this->ptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700461}
462
463template <typename T>
464size_t Slice<T>::size() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700465 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700466}
467
468template <typename T>
469size_t Slice<T>::length() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700470 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700471}
David Tolnayac6cb542020-11-25 20:18:55 -0800472
473template <typename T>
474T &Slice<T>::iterator::operator*() const noexcept {
475 return *this->pos;
476}
477
478template <typename T>
479T *Slice<T>::iterator::operator->() const noexcept {
480 return this->pos;
481}
482
483template <typename T>
484typename Slice<T>::iterator &Slice<T>::iterator::operator++() noexcept {
485 ++this->pos;
486 return *this;
487}
488
489template <typename T>
490typename Slice<T>::iterator Slice<T>::iterator::operator++(int) noexcept {
491 auto ret = iterator(*this);
492 ++this->pos;
493 return ret;
494}
495
496template <typename T>
497bool Slice<T>::iterator::operator==(const iterator &other) const noexcept {
498 return this->pos == other.pos;
499}
500
501template <typename T>
502bool Slice<T>::iterator::operator!=(const iterator &other) const noexcept {
503 return this->pos != other.pos;
504}
505
506template <typename T>
507typename Slice<T>::iterator Slice<T>::begin() const noexcept {
508 iterator it;
509 it.pos = this->ptr;
510 return it;
511}
512
513template <typename T>
514typename Slice<T>::iterator Slice<T>::end() const noexcept {
515 iterator it = this->begin();
516 it.pos += this->len;
517 return it;
518}
David Tolnay0f0162f2020-11-16 23:43:37 -0800519#endif // CXXBRIDGE1_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700520
David Tolnay0f0162f2020-11-16 23:43:37 -0800521#ifndef CXXBRIDGE1_RUST_BOX
522#define CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700523template <typename T>
524Box<T>::Box(const Box &other) : Box(*other) {}
525
526template <typename T>
527Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
528 other.ptr = nullptr;
529}
530
531template <typename T>
532Box<T>::Box(const T &val) {
533 this->uninit();
534 ::new (this->ptr) T(val);
535}
536
537template <typename T>
538Box<T>::Box(T &&val) {
539 this->uninit();
540 ::new (this->ptr) T(std::move(val));
541}
542
543template <typename T>
544Box<T>::~Box() noexcept {
545 if (this->ptr) {
546 this->drop();
547 }
548}
549
550template <typename T>
551Box<T> &Box<T>::operator=(const Box &other) {
552 if (this != &other) {
553 if (this->ptr) {
554 **this = *other;
555 } else {
556 this->uninit();
557 ::new (this->ptr) T(*other);
558 }
559 }
560 return *this;
561}
562
563template <typename T>
564Box<T> &Box<T>::operator=(Box &&other) noexcept {
565 if (this->ptr) {
566 this->drop();
567 }
568 this->ptr = other.ptr;
569 other.ptr = nullptr;
570 return *this;
571}
572
573template <typename T>
574const T *Box<T>::operator->() const noexcept {
575 return this->ptr;
576}
577
578template <typename T>
579const T &Box<T>::operator*() const noexcept {
580 return *this->ptr;
581}
582
583template <typename T>
584T *Box<T>::operator->() noexcept {
585 return this->ptr;
586}
587
588template <typename T>
589T &Box<T>::operator*() noexcept {
590 return *this->ptr;
591}
592
593template <typename T>
594template <typename... Fields>
595Box<T> Box<T>::in_place(Fields &&... fields) {
596 Box box;
597 box.uninit();
598 ::new (box.ptr) T{std::forward<Fields>(fields)...};
599 return box;
600}
601
602template <typename T>
603Box<T> Box<T>::from_raw(T *raw) noexcept {
604 Box box;
605 box.ptr = raw;
606 return box;
607}
608
609template <typename T>
610T *Box<T>::into_raw() noexcept {
611 T *raw = this->ptr;
612 this->ptr = nullptr;
613 return raw;
614}
615
616template <typename T>
David Tolnay79076c72020-11-23 18:16:55 -0800617Box<T>::Box() noexcept = default;
David Tolnay0f0162f2020-11-16 23:43:37 -0800618#endif // CXXBRIDGE1_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700619
David Tolnay0f0162f2020-11-16 23:43:37 -0800620#ifndef CXXBRIDGE1_RUST_VEC
621#define CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700622template <typename T>
David Tolnay15671862020-11-23 18:13:56 -0800623Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700624 new (&other) Vec();
625}
626
627template <typename T>
628Vec<T>::~Vec() noexcept {
629 this->drop();
630}
631
632template <typename T>
633Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
634 if (this != &other) {
635 this->drop();
636 this->repr = other.repr;
637 new (&other) Vec();
638 }
639 return *this;
640}
641
642template <typename T>
643bool Vec<T>::empty() const noexcept {
644 return size() == 0;
645}
646
647template <typename T>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800648T *Vec<T>::data() noexcept {
649 return const_cast<T *>(const_cast<const Vec<T> *>(this)->data());
650}
651
652template <typename T>
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700653const T &Vec<T>::operator[](size_t n) const noexcept {
654 auto data = reinterpret_cast<const char *>(this->data());
655 return *reinterpret_cast<const T *>(data + n * this->stride());
656}
657
658template <typename T>
659const T &Vec<T>::at(size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700660 if (n >= this->size()) {
661 panic<std::out_of_range>("rust::Vec index out of range");
662 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700663 return (*this)[n];
664}
665
666template <typename T>
667const T &Vec<T>::front() const {
668 return (*this)[0];
669}
670
671template <typename T>
672const T &Vec<T>::back() const {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700673 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700674}
675
676template <typename T>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800677void Vec<T>::reserve(size_t new_cap) {
678 this->reserve_total(new_cap);
679}
680
681template <typename T>
682void Vec<T>::push_back(const T &value) {
683 this->emplace_back(value);
684}
685
686template <typename T>
687void Vec<T>::push_back(T &&value) {
688 this->emplace_back(std::move(value));
689}
690
691template <typename T>
692template <typename... Args>
693void Vec<T>::emplace_back(Args &&... args) {
694 auto size = this->size();
695 this->reserve_total(size + 1);
696 ::new (reinterpret_cast<T *>(reinterpret_cast<char *>(this->data()) +
697 size * this->stride()))
698 T(std::forward<Args>(args)...);
699 this->set_len(size + 1);
700}
701
702template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800703T &Vec<T>::iterator::operator*() const noexcept {
704 return *static_cast<T *>(this->pos);
705}
706
707template <typename T>
708T *Vec<T>::iterator::operator->() const noexcept {
709 return static_cast<T *>(this->pos);
710}
711
712template <typename T>
713typename Vec<T>::iterator &Vec<T>::iterator::operator++() noexcept {
David Tolnay248215e2020-11-25 19:38:26 -0800714 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnay960b5112020-11-25 13:18:28 -0800715 return *this;
716}
717
718template <typename T>
719typename Vec<T>::iterator Vec<T>::iterator::operator++(int) noexcept {
720 auto ret = iterator(*this);
David Tolnay248215e2020-11-25 19:38:26 -0800721 this->pos = static_cast<char *>(this->pos) + this->stride;
David Tolnay960b5112020-11-25 13:18:28 -0800722 return ret;
723}
724
725template <typename T>
726bool Vec<T>::iterator::operator==(const iterator &other) const noexcept {
727 return this->pos == other.pos;
728}
729
730template <typename T>
731bool Vec<T>::iterator::operator!=(const iterator &other) const noexcept {
732 return this->pos != other.pos;
733}
734
735template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800736typename Vec<T>::iterator Vec<T>::begin() noexcept {
737 iterator it;
David Tolnaya5d72c62020-11-25 13:57:16 -0800738 it.pos = const_cast<typename std::remove_const<T>::type *>(this->data());
David Tolnay960b5112020-11-25 13:18:28 -0800739 it.stride = this->stride();
740 return it;
741}
742
743template <typename T>
744typename Vec<T>::iterator Vec<T>::end() noexcept {
745 iterator it = this->begin();
David Tolnay248215e2020-11-25 19:38:26 -0800746 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
David Tolnay960b5112020-11-25 13:18:28 -0800747 return it;
748}
749
750template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700751typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
David Tolnay960b5112020-11-25 13:18:28 -0800752 return this->cbegin();
753}
754
755template <typename T>
756typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
757 return this->cend();
758}
759
760template <typename T>
761typename Vec<T>::const_iterator Vec<T>::cbegin() const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700762 const_iterator it;
David Tolnaya5d72c62020-11-25 13:57:16 -0800763 it.pos = const_cast<typename std::remove_const<T>::type *>(this->data());
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700764 it.stride = this->stride();
765 return it;
766}
767
768template <typename T>
David Tolnay960b5112020-11-25 13:18:28 -0800769typename Vec<T>::const_iterator Vec<T>::cend() const noexcept {
770 const_iterator it = this->cbegin();
David Tolnay248215e2020-11-25 19:38:26 -0800771 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700772 return it;
773}
774
775// Internal API only intended for the cxxbridge code generator.
776template <typename T>
777Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay0f0162f2020-11-16 23:43:37 -0800778#endif // CXXBRIDGE1_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700779
David Tolnay0f0162f2020-11-16 23:43:37 -0800780#ifndef CXXBRIDGE1_RELOCATABLE
781#define CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -0800782namespace detail {
783template <typename... Ts>
784struct make_void {
785 using type = void;
786};
787
788template <typename... Ts>
789using void_t = typename make_void<Ts...>::type;
790
791template <typename Void, template <typename...> class, typename...>
792struct detect : std::false_type {};
793template <template <typename...> class T, typename... A>
794struct detect<void_t<T<A...>>, T, A...> : std::true_type {};
795
796template <template <typename...> class T, typename... A>
797using is_detected = detect<void, T, A...>;
798
799template <typename T>
800using detect_IsRelocatable = typename T::IsRelocatable;
801
802template <typename T>
803struct get_IsRelocatable
804 : std::is_same<typename T::IsRelocatable, std::true_type> {};
805} // namespace detail
806
807template <typename T>
808struct IsRelocatable
809 : std::conditional<
810 detail::is_detected<detail::detect_IsRelocatable, T>::value,
811 detail::get_IsRelocatable<T>,
812 std::integral_constant<
813 bool, std::is_trivially_move_constructible<T>::value &&
814 std::is_trivially_destructible<T>::value>>::type {};
David Tolnay0f0162f2020-11-16 23:43:37 -0800815#endif // CXXBRIDGE1_RELOCATABLE
David Tolnay174bd952020-11-02 09:23:12 -0800816
David Tolnay0f0162f2020-11-16 23:43:37 -0800817} // namespace cxxbridge1
David Tolnay750755e2020-03-01 13:04:08 -0800818} // namespace rust