blob: b048e27629e2e02f5b6fd4c52694beb3b21c9f76 [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 Tolnay0ecd05a2020-07-29 16:32:03 -07007#include <new>
Stephen Crane9e48d5b2020-08-21 12:17:02 -07008#include <stdexcept>
David Tolnay7db73692019-10-20 14:51:12 -04009#include <string>
David Tolnayf6292372020-03-01 21:09:11 -080010#include <type_traits>
David Tolnay4791f1c2020-03-17 21:53:16 -070011#include <utility>
David Tolnay37dd7e12020-04-25 12:51:59 -070012#include <vector>
David Tolnay59b5ba12020-04-10 11:32:19 -070013#if defined(_WIN32)
David Tolnayda38b7c2020-09-16 11:50:04 -040014#include <basetsd.h>
David Tolnay59b5ba12020-04-10 11:32:19 -070015#endif
David Tolnay7db73692019-10-20 14:51:12 -040016
David Tolnay750755e2020-03-01 13:04:08 -080017namespace rust {
David Tolnay8f16ae72020-10-08 18:21:13 -070018inline namespace cxxbridge05 {
David Tolnay7db73692019-10-20 14:51:12 -040019
David Tolnay2a2b9ad2020-05-12 20:07:26 -070020struct unsafe_bitcopy_t;
David Tolnayd1e2efc2020-03-03 22:25:43 -080021
David Tolnay84ddf9e2020-10-31 15:36:48 -070022namespace {
23template <typename T>
24class impl;
25}
26
David Tolnay8f16ae72020-10-08 18:21:13 -070027#ifndef CXXBRIDGE05_RUST_STRING
28#define CXXBRIDGE05_RUST_STRING
David Tolnay56082162020-03-01 12:57:33 -080029class String final {
David Tolnay7db73692019-10-20 14:51:12 -040030public:
David Tolnay56082162020-03-01 12:57:33 -080031 String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080032 String(const String &) noexcept;
33 String(String &&) noexcept;
David Tolnay56082162020-03-01 12:57:33 -080034 ~String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080035
36 String(const std::string &);
37 String(const char *);
David Tolnayc2bbd952020-07-29 18:15:26 -070038 String(const char *, size_t);
David Tolnayd9c4ac92020-03-01 20:33:58 -080039
40 String &operator=(const String &) noexcept;
41 String &operator=(String &&) noexcept;
42
David Tolnay404d6892020-03-01 20:19:41 -080043 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040044
45 // Note: no null terminator.
46 const char *data() const noexcept;
47 size_t size() const noexcept;
48 size_t length() const noexcept;
49
David Tolnayd1e2efc2020-03-03 22:25:43 -080050 // Internal API only intended for the cxxbridge code generator.
51 String(unsafe_bitcopy_t, const String &) noexcept;
52
David Tolnay7db73692019-10-20 14:51:12 -040053private:
54 // Size and alignment statically verified by rust_string.rs.
55 std::array<uintptr_t, 3> repr;
56};
David Tolnay8f16ae72020-10-08 18:21:13 -070057#endif // CXXBRIDGE05_RUST_STRING
David Tolnay7db73692019-10-20 14:51:12 -040058
David Tolnay8f16ae72020-10-08 18:21:13 -070059#ifndef CXXBRIDGE05_RUST_STR
David Tolnay09dbe752020-03-01 13:00:40 -080060class Str final {
David Tolnay7db73692019-10-20 14:51:12 -040061public:
David Tolnay09dbe752020-03-01 13:00:40 -080062 Str() noexcept;
David Tolnay851677c2020-03-01 23:49:46 -080063 Str(const std::string &);
64 Str(const char *);
David Tolnay894c5e42020-07-29 18:20:00 -070065 Str(const char *, size_t);
David Tolnay851677c2020-03-01 23:49:46 -080066 Str(std::string &&) = delete;
David Tolnayd9c4ac92020-03-01 20:33:58 -080067
David Tolnay2d7f1172020-10-31 17:58:31 -070068 Str &operator=(const Str &) noexcept = default;
David Tolnayd9c4ac92020-03-01 20:33:58 -080069
David Tolnay404d6892020-03-01 20:19:41 -080070 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040071
72 // Note: no null terminator.
73 const char *data() const noexcept;
74 size_t size() const noexcept;
75 size_t length() const noexcept;
76
David Tolnayde9a5b12020-10-31 12:15:43 -070077 // Important in order for System V ABI to pass in registers.
78 Str(const Str &) noexcept = default;
79 ~Str() noexcept = default;
80
David Tolnay5df1f062020-10-31 12:31:10 -070081private:
David Tolnay0356d332020-10-31 19:46:41 -070082 friend impl<Str>;
David Tolnay7db73692019-10-20 14:51:12 -040083 // Not necessarily ABI compatible with &str. Codegen will translate to
84 // cxx::rust_str::RustStr which matches this layout.
David Tolnay5df1f062020-10-31 12:31:10 -070085 const char *ptr;
86 size_t len;
David Tolnay7db73692019-10-20 14:51:12 -040087};
David Tolnay8f16ae72020-10-08 18:21:13 -070088#endif // CXXBRIDGE05_RUST_STR
David Tolnay7db73692019-10-20 14:51:12 -040089
David Tolnay8f16ae72020-10-08 18:21:13 -070090#ifndef CXXBRIDGE05_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -070091template <typename T>
92class Slice final {
93public:
David Tolnay2a2b9ad2020-05-12 20:07:26 -070094 Slice() noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -070095 Slice(const T *, size_t count) noexcept;
David Tolnayefe81052020-04-14 16:28:24 -070096
David Tolnay2d7f1172020-10-31 17:58:31 -070097 Slice &operator=(const Slice<T> &) noexcept = default;
David Tolnayefe81052020-04-14 16:28:24 -070098
David Tolnay2a2b9ad2020-05-12 20:07:26 -070099 const T *data() const noexcept;
100 size_t size() const noexcept;
101 size_t length() const noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700102
David Tolnayde9a5b12020-10-31 12:15:43 -0700103 // Important in order for System V ABI to pass in registers.
104 Slice(const Slice<T> &) noexcept = default;
105 ~Slice() noexcept = default;
106
David Tolnayefe81052020-04-14 16:28:24 -0700107private:
David Tolnay36aa9e02020-10-31 23:08:21 -0700108 friend impl<Slice>;
109 // Not necessarily ABI compatible with &[T]. Codegen will translate to
110 // cxx::rust_sliceu8::RustSliceU8 which matches this layout.
111 const T *ptr;
112 size_t len;
David Tolnayefe81052020-04-14 16:28:24 -0700113};
David Tolnay8f16ae72020-10-08 18:21:13 -0700114#endif // CXXBRIDGE05_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -0700115
David Tolnay8f16ae72020-10-08 18:21:13 -0700116#ifndef CXXBRIDGE05_RUST_BOX
David Tolnayf262d382020-04-11 22:12:40 -0700117template <typename T>
118class Box final {
David Tolnay7db73692019-10-20 14:51:12 -0400119public:
David Tolnayf6292372020-03-01 21:09:11 -0800120 using value_type = T;
David Tolnay9706a512020-04-24 17:09:01 -0700121 using const_pointer =
122 typename std::add_pointer<typename std::add_const<T>::type>::type;
123 using pointer = typename std::add_pointer<T>::type;
David Tolnayf6292372020-03-01 21:09:11 -0800124
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700125 Box(const Box &);
126 Box(Box &&) noexcept;
127 ~Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400128
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700129 explicit Box(const T &);
130 explicit Box(T &&);
131
132 Box &operator=(const Box &);
133 Box &operator=(Box &&) noexcept;
134
135 const T *operator->() const noexcept;
136 const T &operator*() const noexcept;
137 T *operator->() noexcept;
138 T &operator*() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400139
David Tolnayf262d382020-04-11 22:12:40 -0700140 template <typename... Fields>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700141 static Box in_place(Fields &&...);
David Tolnay7ce59fc2020-04-11 11:46:33 -0700142
David Tolnay7db73692019-10-20 14:51:12 -0400143 // Important: requires that `raw` came from an into_raw call. Do not pass a
144 // pointer from `new` or any other source.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700145 static Box from_raw(T *) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400146
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700147 T *into_raw() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400148
149private:
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700150 Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400151 void uninit() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400152 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800153 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400154};
David Tolnay8f16ae72020-10-08 18:21:13 -0700155#endif // CXXBRIDGE05_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400156
David Tolnay8f16ae72020-10-08 18:21:13 -0700157#ifndef CXXBRIDGE05_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700158template <typename T>
159class Vec final {
160public:
David Tolnayc87c2152020-04-24 17:07:41 -0700161 using value_type = T;
162
David Tolnayf97c2d52020-04-25 16:37:48 -0700163 Vec() noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700164 Vec(Vec &&) noexcept;
165 ~Vec() noexcept;
David Tolnaycb800572020-04-24 20:30:43 -0700166
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700167 Vec &operator=(Vec &&) noexcept;
David Tolnayf97c2d52020-04-25 16:37:48 -0700168
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700169 size_t size() const noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700170 bool empty() const noexcept;
David Tolnay219c0792020-04-24 20:31:37 -0700171 const T *data() const noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700172
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700173 const T &operator[](size_t n) const noexcept;
174 const T &at(size_t n) const;
175
176 const T &front() const;
177 const T &back() const;
178
David Tolnay48521222020-10-31 14:59:42 -0700179 class const_iterator final {
David Tolnayc87c2152020-04-24 17:07:41 -0700180 public:
myronahnda9be502020-04-29 05:47:23 +0700181 using difference_type = ptrdiff_t;
David Tolnayc87c2152020-04-24 17:07:41 -0700182 using value_type = typename std::add_const<T>::type;
David Tolnay74dd3792020-04-30 07:45:24 -0700183 using pointer =
184 typename std::add_pointer<typename std::add_const<T>::type>::type;
David Tolnayc87c2152020-04-24 17:07:41 -0700185 using reference = typename std::add_lvalue_reference<
186 typename std::add_const<T>::type>::type;
myronahnda9be502020-04-29 05:47:23 +0700187 using iterator_category = std::forward_iterator_tag;
David Tolnayc87c2152020-04-24 17:07:41 -0700188
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700189 const T &operator*() const noexcept;
190 const T *operator->() const noexcept;
191 const_iterator &operator++() noexcept;
192 const_iterator operator++(int) noexcept;
193 bool operator==(const const_iterator &) const noexcept;
194 bool operator!=(const const_iterator &) const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700195
196 private:
197 friend class Vec;
198 const void *pos;
199 size_t stride;
200 };
201
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700202 const_iterator begin() const noexcept;
203 const_iterator end() const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700204
David Tolnay313b10e2020-04-25 16:30:51 -0700205 // Internal API only intended for the cxxbridge code generator.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700206 Vec(unsafe_bitcopy_t, const Vec &) noexcept;
David Tolnay313b10e2020-04-25 16:30:51 -0700207
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700208private:
David Tolnay503d0192020-04-24 22:18:56 -0700209 static size_t stride() noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700210 void drop() noexcept;
211
212 // Size and alignment statically verified by rust_vec.rs.
213 std::array<uintptr_t, 3> repr;
214};
David Tolnay8f16ae72020-10-08 18:21:13 -0700215#endif // CXXBRIDGE05_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700216
David Tolnay8f16ae72020-10-08 18:21:13 -0700217#ifndef CXXBRIDGE05_RUST_FN
218#define CXXBRIDGE05_RUST_FN
David Tolnayf262d382020-04-11 22:12:40 -0700219template <typename Signature, bool Throws = false>
220class Fn;
David Tolnay75dca2e2020-03-25 20:17:52 -0700221
222template <typename Ret, typename... Args, bool Throws>
David Tolnay48521222020-10-31 14:59:42 -0700223class Fn<Ret(Args...), Throws> final {
David Tolnay75dca2e2020-03-25 20:17:52 -0700224public:
David Tolnay533d4582020-04-08 20:29:14 -0700225 Ret operator()(Args... args) const noexcept(!Throws);
226 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700227
228private:
229 Ret (*trampoline)(Args..., void *fn) noexcept(!Throws);
230 void *fn;
231};
232
David Tolnayf262d382020-04-11 22:12:40 -0700233template <typename Signature>
234using TryFn = Fn<Signature, true>;
David Tolnay8f16ae72020-10-08 18:21:13 -0700235#endif // CXXBRIDGE05_RUST_FN
David Tolnay75dca2e2020-03-25 20:17:52 -0700236
David Tolnay8f16ae72020-10-08 18:21:13 -0700237#ifndef CXXBRIDGE05_RUST_ERROR
238#define CXXBRIDGE05_RUST_ERROR
David Tolnaye4fa8732020-09-08 15:04:56 -0700239class Error final : public std::exception {
David Tolnay1e548172020-03-16 13:37:09 -0700240public:
241 Error(const Error &);
242 Error(Error &&) noexcept;
David Tolnay1e548172020-03-16 13:37:09 -0700243 ~Error() noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700244
245 Error &operator=(const Error &);
David Tolnay15491062020-10-31 17:25:13 -0700246 Error &operator=(Error &&) noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700247
David Tolnay1e548172020-03-16 13:37:09 -0700248 const char *what() const noexcept override;
249
250private:
David Tolnaya0c9bc72020-10-31 14:37:14 -0700251 Error() noexcept = default;
David Tolnay84ddf9e2020-10-31 15:36:48 -0700252 friend impl<Error>;
David Tolnaya0c9bc72020-10-31 14:37:14 -0700253 const char *msg;
254 size_t len;
David Tolnay1e548172020-03-16 13:37:09 -0700255};
David Tolnay8f16ae72020-10-08 18:21:13 -0700256#endif // CXXBRIDGE05_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700257
David Tolnay8f16ae72020-10-08 18:21:13 -0700258#ifndef CXXBRIDGE05_RUST_ISIZE
259#define CXXBRIDGE05_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700260#if defined(_WIN32)
261using isize = SSIZE_T;
262#else
263using isize = ssize_t;
264#endif
David Tolnay8f16ae72020-10-08 18:21:13 -0700265#endif // CXXBRIDGE05_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700266
David Tolnay851677c2020-03-01 23:49:46 -0800267std::ostream &operator<<(std::ostream &, const String &);
268std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400269
David Tolnay174bd952020-11-02 09:23:12 -0800270// IsRelocatable<T> is used in assertions that a C++ type passed by value
271// between Rust and C++ is soundly relocatable by Rust.
272//
273// There may be legitimate reasons to opt out of the check for support of types
274// that the programmer knows are soundly Rust-movable despite not being
275// recognized as such by the C++ type system due to a move constructor or
276// destructor. To opt out of the relocatability check, do either of the
277// following things in any header used by `include!` in the bridge.
278//
279// --- if you define the type:
280// struct MyType {
281// ...
282// + using IsRelocatable = std::true_type;
283// };
284//
285// --- otherwise:
286// + template <>
287// + struct rust::IsRelocatable<MyType> : std::true_type {};
288template <typename T>
289struct IsRelocatable;
290
David Tolnay3b0c9882020-03-01 14:08:57 -0800291// Snake case aliases for use in code that uses this style for type names.
292using string = String;
293using str = Str;
David Tolnayf262d382020-04-11 22:12:40 -0700294template <class T>
David Tolnay38c87642020-09-06 22:18:08 -0700295using slice = Slice<T>;
296template <class T>
David Tolnayf262d382020-04-11 22:12:40 -0700297using box = Box<T>;
David Tolnay38c87642020-09-06 22:18:08 -0700298template <class T>
299using vec = Vec<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700300using error = Error;
David Tolnay75dca2e2020-03-25 20:17:52 -0700301template <typename Signature, bool Throws = false>
302using fn = Fn<Signature, Throws>;
David Tolnayf262d382020-04-11 22:12:40 -0700303template <typename Signature>
304using try_fn = TryFn<Signature>;
David Tolnay174bd952020-11-02 09:23:12 -0800305template <typename T>
306using is_relocatable = IsRelocatable<T>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800307
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700308
309
310////////////////////////////////////////////////////////////////////////////////
311/// end public API, begin implementation details
312
David Tolnay8f16ae72020-10-08 18:21:13 -0700313#ifndef CXXBRIDGE05_PANIC
314#define CXXBRIDGE05_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700315template <typename Exception>
316void panic [[noreturn]] (const char *msg);
David Tolnay8f16ae72020-10-08 18:21:13 -0700317#endif // CXXBRIDGE05_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700318
David Tolnay75dca2e2020-03-25 20:17:52 -0700319template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700320Ret Fn<Ret(Args...), Throws>::operator()(Args... args) const noexcept(!Throws) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700321 return (*this->trampoline)(std::move(args)..., this->fn);
322}
323
David Tolnaya23129c2020-04-08 20:08:21 -0700324template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700325Fn<Ret(Args...), Throws> Fn<Ret(Args...), Throws>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700326 return *this;
327}
328
David Tolnay8f16ae72020-10-08 18:21:13 -0700329#ifndef CXXBRIDGE05_RUST_BITCOPY
330#define CXXBRIDGE05_RUST_BITCOPY
David Tolnay48521222020-10-31 14:59:42 -0700331struct unsafe_bitcopy_t final {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700332 explicit unsafe_bitcopy_t() = default;
333};
334
335constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnay8f16ae72020-10-08 18:21:13 -0700336#endif // CXXBRIDGE05_RUST_BITCOPY
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700337
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700338#ifndef CXXBRIDGE05_RUST_STR
339#define CXXBRIDGE05_RUST_STR
340inline const char *Str::data() const noexcept { return this->ptr; }
341
342inline size_t Str::size() const noexcept { return this->len; }
343
344inline size_t Str::length() const noexcept { return this->len; }
345#endif // CXXBRIDGE05_RUST_STR
346
David Tolnay8f16ae72020-10-08 18:21:13 -0700347#ifndef CXXBRIDGE05_RUST_SLICE
348#define CXXBRIDGE05_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700349template <typename T>
David Tolnay36aa9e02020-10-31 23:08:21 -0700350Slice<T>::Slice() noexcept : ptr(reinterpret_cast<const T *>(this)), len(0) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700351
352template <typename T>
David Tolnay36aa9e02020-10-31 23:08:21 -0700353Slice<T>::Slice(const T *s, size_t count) noexcept : ptr(s), len(count) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700354
355template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700356const T *Slice<T>::data() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700357 return this->ptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700358}
359
360template <typename T>
361size_t Slice<T>::size() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700362 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700363}
364
365template <typename T>
366size_t Slice<T>::length() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700367 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700368}
David Tolnay8f16ae72020-10-08 18:21:13 -0700369#endif // CXXBRIDGE05_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700370
David Tolnay8f16ae72020-10-08 18:21:13 -0700371#ifndef CXXBRIDGE05_RUST_BOX
372#define CXXBRIDGE05_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700373template <typename T>
374Box<T>::Box(const Box &other) : Box(*other) {}
375
376template <typename T>
377Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
378 other.ptr = nullptr;
379}
380
381template <typename T>
382Box<T>::Box(const T &val) {
383 this->uninit();
384 ::new (this->ptr) T(val);
385}
386
387template <typename T>
388Box<T>::Box(T &&val) {
389 this->uninit();
390 ::new (this->ptr) T(std::move(val));
391}
392
393template <typename T>
394Box<T>::~Box() noexcept {
395 if (this->ptr) {
396 this->drop();
397 }
398}
399
400template <typename T>
401Box<T> &Box<T>::operator=(const Box &other) {
402 if (this != &other) {
403 if (this->ptr) {
404 **this = *other;
405 } else {
406 this->uninit();
407 ::new (this->ptr) T(*other);
408 }
409 }
410 return *this;
411}
412
413template <typename T>
414Box<T> &Box<T>::operator=(Box &&other) noexcept {
415 if (this->ptr) {
416 this->drop();
417 }
418 this->ptr = other.ptr;
419 other.ptr = nullptr;
420 return *this;
421}
422
423template <typename T>
424const T *Box<T>::operator->() const noexcept {
425 return this->ptr;
426}
427
428template <typename T>
429const T &Box<T>::operator*() const noexcept {
430 return *this->ptr;
431}
432
433template <typename T>
434T *Box<T>::operator->() noexcept {
435 return this->ptr;
436}
437
438template <typename T>
439T &Box<T>::operator*() noexcept {
440 return *this->ptr;
441}
442
443template <typename T>
444template <typename... Fields>
445Box<T> Box<T>::in_place(Fields &&... fields) {
446 Box box;
447 box.uninit();
448 ::new (box.ptr) T{std::forward<Fields>(fields)...};
449 return box;
450}
451
452template <typename T>
453Box<T> Box<T>::from_raw(T *raw) noexcept {
454 Box box;
455 box.ptr = raw;
456 return box;
457}
458
459template <typename T>
460T *Box<T>::into_raw() noexcept {
461 T *raw = this->ptr;
462 this->ptr = nullptr;
463 return raw;
464}
465
466template <typename T>
467Box<T>::Box() noexcept {}
David Tolnay8f16ae72020-10-08 18:21:13 -0700468#endif // CXXBRIDGE05_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700469
David Tolnay8f16ae72020-10-08 18:21:13 -0700470#ifndef CXXBRIDGE05_RUST_VEC
471#define CXXBRIDGE05_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700472template <typename T>
473Vec<T>::Vec(Vec &&other) noexcept {
474 this->repr = other.repr;
475 new (&other) Vec();
476}
477
478template <typename T>
479Vec<T>::~Vec() noexcept {
480 this->drop();
481}
482
483template <typename T>
484Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
485 if (this != &other) {
486 this->drop();
487 this->repr = other.repr;
488 new (&other) Vec();
489 }
490 return *this;
491}
492
493template <typename T>
494bool Vec<T>::empty() const noexcept {
495 return size() == 0;
496}
497
498template <typename T>
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700499const T &Vec<T>::operator[](size_t n) const noexcept {
500 auto data = reinterpret_cast<const char *>(this->data());
501 return *reinterpret_cast<const T *>(data + n * this->stride());
502}
503
504template <typename T>
505const T &Vec<T>::at(size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700506 if (n >= this->size()) {
507 panic<std::out_of_range>("rust::Vec index out of range");
508 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700509 return (*this)[n];
510}
511
512template <typename T>
513const T &Vec<T>::front() const {
514 return (*this)[0];
515}
516
517template <typename T>
518const T &Vec<T>::back() const {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700519 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700520}
521
522template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700523const T &Vec<T>::const_iterator::operator*() const noexcept {
524 return *static_cast<const T *>(this->pos);
525}
526
527template <typename T>
528const T *Vec<T>::const_iterator::operator->() const noexcept {
529 return static_cast<const T *>(this->pos);
530}
531
532template <typename T>
533typename Vec<T>::const_iterator &Vec<T>::const_iterator::operator++() noexcept {
534 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
535 return *this;
536}
537
538template <typename T>
539typename Vec<T>::const_iterator
540Vec<T>::const_iterator::operator++(int) noexcept {
541 auto ret = const_iterator(*this);
542 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
543 return ret;
544}
545
546template <typename T>
David Tolnayb10c4bc2020-08-26 21:55:29 -0700547bool Vec<T>::const_iterator::operator==(
548 const const_iterator &other) const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700549 return this->pos == other.pos;
550}
551
552template <typename T>
David Tolnayb10c4bc2020-08-26 21:55:29 -0700553bool Vec<T>::const_iterator::operator!=(
554 const const_iterator &other) const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700555 return this->pos != other.pos;
556}
557
558template <typename T>
559typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
560 const_iterator it;
561 it.pos = this->data();
562 it.stride = this->stride();
563 return it;
564}
565
566template <typename T>
567typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
568 const_iterator it = this->begin();
569 it.pos = static_cast<const uint8_t *>(it.pos) + it.stride * this->size();
570 return it;
571}
572
573// Internal API only intended for the cxxbridge code generator.
574template <typename T>
575Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay8f16ae72020-10-08 18:21:13 -0700576#endif // CXXBRIDGE05_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700577
David Tolnay174bd952020-11-02 09:23:12 -0800578#ifndef CXXBRIDGE05_RELOCATABLE
579#define CXXBRIDGE05_RELOCATABLE
580namespace detail {
581template <typename... Ts>
582struct make_void {
583 using type = void;
584};
585
586template <typename... Ts>
587using void_t = typename make_void<Ts...>::type;
588
589template <typename Void, template <typename...> class, typename...>
590struct detect : std::false_type {};
591template <template <typename...> class T, typename... A>
592struct detect<void_t<T<A...>>, T, A...> : std::true_type {};
593
594template <template <typename...> class T, typename... A>
595using is_detected = detect<void, T, A...>;
596
597template <typename T>
598using detect_IsRelocatable = typename T::IsRelocatable;
599
600template <typename T>
601struct get_IsRelocatable
602 : std::is_same<typename T::IsRelocatable, std::true_type> {};
603} // namespace detail
604
605template <typename T>
606struct IsRelocatable
607 : std::conditional<
608 detail::is_detected<detail::detect_IsRelocatable, T>::value,
609 detail::get_IsRelocatable<T>,
610 std::integral_constant<
611 bool, std::is_trivially_move_constructible<T>::value &&
612 std::is_trivially_destructible<T>::value>>::type {};
613#endif // CXXBRIDGE05_RELOCATABLE
614
David Tolnay8f16ae72020-10-08 18:21:13 -0700615} // namespace cxxbridge05
David Tolnay750755e2020-03-01 13:04:08 -0800616} // namespace rust