blob: 0313d34b98665277c4d934dcf076d99df1b8165b [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
David Tolnayf262d382020-04-11 22:12:40 -0700218template <typename Signature, bool Throws = false>
219class Fn;
David Tolnay75dca2e2020-03-25 20:17:52 -0700220
221template <typename Ret, typename... Args, bool Throws>
David Tolnay48521222020-10-31 14:59:42 -0700222class Fn<Ret(Args...), Throws> final {
David Tolnay75dca2e2020-03-25 20:17:52 -0700223public:
David Tolnay533d4582020-04-08 20:29:14 -0700224 Ret operator()(Args... args) const noexcept(!Throws);
225 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700226
227private:
228 Ret (*trampoline)(Args..., void *fn) noexcept(!Throws);
229 void *fn;
230};
231
David Tolnayf262d382020-04-11 22:12:40 -0700232template <typename Signature>
233using TryFn = Fn<Signature, true>;
David Tolnay8f16ae72020-10-08 18:21:13 -0700234#endif // CXXBRIDGE05_RUST_FN
David Tolnay75dca2e2020-03-25 20:17:52 -0700235
David Tolnay8f16ae72020-10-08 18:21:13 -0700236#ifndef CXXBRIDGE05_RUST_ERROR
237#define CXXBRIDGE05_RUST_ERROR
David Tolnaye4fa8732020-09-08 15:04:56 -0700238class Error final : public std::exception {
David Tolnay1e548172020-03-16 13:37:09 -0700239public:
240 Error(const Error &);
241 Error(Error &&) noexcept;
David Tolnay1e548172020-03-16 13:37:09 -0700242 ~Error() noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700243
244 Error &operator=(const Error &);
David Tolnay15491062020-10-31 17:25:13 -0700245 Error &operator=(Error &&) noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700246
David Tolnay1e548172020-03-16 13:37:09 -0700247 const char *what() const noexcept override;
248
249private:
David Tolnaya0c9bc72020-10-31 14:37:14 -0700250 Error() noexcept = default;
David Tolnay84ddf9e2020-10-31 15:36:48 -0700251 friend impl<Error>;
David Tolnaya0c9bc72020-10-31 14:37:14 -0700252 const char *msg;
253 size_t len;
David Tolnay1e548172020-03-16 13:37:09 -0700254};
David Tolnay8f16ae72020-10-08 18:21:13 -0700255#endif // CXXBRIDGE05_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700256
David Tolnay8f16ae72020-10-08 18:21:13 -0700257#ifndef CXXBRIDGE05_RUST_ISIZE
258#define CXXBRIDGE05_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700259#if defined(_WIN32)
260using isize = SSIZE_T;
261#else
262using isize = ssize_t;
263#endif
David Tolnay8f16ae72020-10-08 18:21:13 -0700264#endif // CXXBRIDGE05_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700265
David Tolnay851677c2020-03-01 23:49:46 -0800266std::ostream &operator<<(std::ostream &, const String &);
267std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400268
David Tolnay174bd952020-11-02 09:23:12 -0800269// IsRelocatable<T> is used in assertions that a C++ type passed by value
270// between Rust and C++ is soundly relocatable by Rust.
271//
272// There may be legitimate reasons to opt out of the check for support of types
273// that the programmer knows are soundly Rust-movable despite not being
274// recognized as such by the C++ type system due to a move constructor or
275// destructor. To opt out of the relocatability check, do either of the
276// following things in any header used by `include!` in the bridge.
277//
278// --- if you define the type:
279// struct MyType {
280// ...
281// + using IsRelocatable = std::true_type;
282// };
283//
284// --- otherwise:
285// + template <>
286// + struct rust::IsRelocatable<MyType> : std::true_type {};
287template <typename T>
288struct IsRelocatable;
289
David Tolnay3b0c9882020-03-01 14:08:57 -0800290// Snake case aliases for use in code that uses this style for type names.
291using string = String;
292using str = Str;
David Tolnayf262d382020-04-11 22:12:40 -0700293template <class T>
David Tolnay38c87642020-09-06 22:18:08 -0700294using slice = Slice<T>;
295template <class T>
David Tolnayf262d382020-04-11 22:12:40 -0700296using box = Box<T>;
David Tolnay38c87642020-09-06 22:18:08 -0700297template <class T>
298using vec = Vec<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700299using error = Error;
David Tolnay75dca2e2020-03-25 20:17:52 -0700300template <typename Signature, bool Throws = false>
301using fn = Fn<Signature, Throws>;
David Tolnayf262d382020-04-11 22:12:40 -0700302template <typename Signature>
303using try_fn = TryFn<Signature>;
David Tolnay174bd952020-11-02 09:23:12 -0800304template <typename T>
305using is_relocatable = IsRelocatable<T>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800306
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700307
308
309////////////////////////////////////////////////////////////////////////////////
310/// end public API, begin implementation details
311
David Tolnay8f16ae72020-10-08 18:21:13 -0700312#ifndef CXXBRIDGE05_PANIC
313#define CXXBRIDGE05_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700314template <typename Exception>
315void panic [[noreturn]] (const char *msg);
David Tolnay8f16ae72020-10-08 18:21:13 -0700316#endif // CXXBRIDGE05_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700317
David Tolnaya5a13012020-11-10 15:03:09 -0800318#ifndef CXXBRIDGE05_RUST_FN
319#define CXXBRIDGE05_RUST_FN
David Tolnay75dca2e2020-03-25 20:17:52 -0700320template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700321Ret Fn<Ret(Args...), Throws>::operator()(Args... args) const noexcept(!Throws) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700322 return (*this->trampoline)(std::move(args)..., this->fn);
323}
324
David Tolnaya23129c2020-04-08 20:08:21 -0700325template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700326Fn<Ret(Args...), Throws> Fn<Ret(Args...), Throws>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700327 return *this;
328}
David Tolnaya5a13012020-11-10 15:03:09 -0800329#endif // CXXBRIDGE05_RUST_FN
David Tolnaya23129c2020-04-08 20:08:21 -0700330
David Tolnay8f16ae72020-10-08 18:21:13 -0700331#ifndef CXXBRIDGE05_RUST_BITCOPY
332#define CXXBRIDGE05_RUST_BITCOPY
David Tolnay48521222020-10-31 14:59:42 -0700333struct unsafe_bitcopy_t final {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700334 explicit unsafe_bitcopy_t() = default;
335};
336
337constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnay8f16ae72020-10-08 18:21:13 -0700338#endif // CXXBRIDGE05_RUST_BITCOPY
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700339
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700340#ifndef CXXBRIDGE05_RUST_STR
341#define CXXBRIDGE05_RUST_STR
342inline const char *Str::data() const noexcept { return this->ptr; }
343
344inline size_t Str::size() const noexcept { return this->len; }
345
346inline size_t Str::length() const noexcept { return this->len; }
347#endif // CXXBRIDGE05_RUST_STR
348
David Tolnay8f16ae72020-10-08 18:21:13 -0700349#ifndef CXXBRIDGE05_RUST_SLICE
350#define CXXBRIDGE05_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700351template <typename T>
David Tolnay36aa9e02020-10-31 23:08:21 -0700352Slice<T>::Slice() noexcept : ptr(reinterpret_cast<const T *>(this)), len(0) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700353
354template <typename T>
David Tolnay36aa9e02020-10-31 23:08:21 -0700355Slice<T>::Slice(const T *s, size_t count) noexcept : ptr(s), len(count) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700356
357template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700358const T *Slice<T>::data() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700359 return this->ptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700360}
361
362template <typename T>
363size_t Slice<T>::size() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700364 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700365}
366
367template <typename T>
368size_t Slice<T>::length() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700369 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700370}
David Tolnay8f16ae72020-10-08 18:21:13 -0700371#endif // CXXBRIDGE05_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700372
David Tolnay8f16ae72020-10-08 18:21:13 -0700373#ifndef CXXBRIDGE05_RUST_BOX
374#define CXXBRIDGE05_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700375template <typename T>
376Box<T>::Box(const Box &other) : Box(*other) {}
377
378template <typename T>
379Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
380 other.ptr = nullptr;
381}
382
383template <typename T>
384Box<T>::Box(const T &val) {
385 this->uninit();
386 ::new (this->ptr) T(val);
387}
388
389template <typename T>
390Box<T>::Box(T &&val) {
391 this->uninit();
392 ::new (this->ptr) T(std::move(val));
393}
394
395template <typename T>
396Box<T>::~Box() noexcept {
397 if (this->ptr) {
398 this->drop();
399 }
400}
401
402template <typename T>
403Box<T> &Box<T>::operator=(const Box &other) {
404 if (this != &other) {
405 if (this->ptr) {
406 **this = *other;
407 } else {
408 this->uninit();
409 ::new (this->ptr) T(*other);
410 }
411 }
412 return *this;
413}
414
415template <typename T>
416Box<T> &Box<T>::operator=(Box &&other) noexcept {
417 if (this->ptr) {
418 this->drop();
419 }
420 this->ptr = other.ptr;
421 other.ptr = nullptr;
422 return *this;
423}
424
425template <typename T>
426const T *Box<T>::operator->() const noexcept {
427 return this->ptr;
428}
429
430template <typename T>
431const T &Box<T>::operator*() const noexcept {
432 return *this->ptr;
433}
434
435template <typename T>
436T *Box<T>::operator->() noexcept {
437 return this->ptr;
438}
439
440template <typename T>
441T &Box<T>::operator*() noexcept {
442 return *this->ptr;
443}
444
445template <typename T>
446template <typename... Fields>
447Box<T> Box<T>::in_place(Fields &&... fields) {
448 Box box;
449 box.uninit();
450 ::new (box.ptr) T{std::forward<Fields>(fields)...};
451 return box;
452}
453
454template <typename T>
455Box<T> Box<T>::from_raw(T *raw) noexcept {
456 Box box;
457 box.ptr = raw;
458 return box;
459}
460
461template <typename T>
462T *Box<T>::into_raw() noexcept {
463 T *raw = this->ptr;
464 this->ptr = nullptr;
465 return raw;
466}
467
468template <typename T>
469Box<T>::Box() noexcept {}
David Tolnay8f16ae72020-10-08 18:21:13 -0700470#endif // CXXBRIDGE05_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700471
David Tolnay8f16ae72020-10-08 18:21:13 -0700472#ifndef CXXBRIDGE05_RUST_VEC
473#define CXXBRIDGE05_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700474template <typename T>
475Vec<T>::Vec(Vec &&other) noexcept {
476 this->repr = other.repr;
477 new (&other) Vec();
478}
479
480template <typename T>
481Vec<T>::~Vec() noexcept {
482 this->drop();
483}
484
485template <typename T>
486Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
487 if (this != &other) {
488 this->drop();
489 this->repr = other.repr;
490 new (&other) Vec();
491 }
492 return *this;
493}
494
495template <typename T>
496bool Vec<T>::empty() const noexcept {
497 return size() == 0;
498}
499
500template <typename T>
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700501const T &Vec<T>::operator[](size_t n) const noexcept {
502 auto data = reinterpret_cast<const char *>(this->data());
503 return *reinterpret_cast<const T *>(data + n * this->stride());
504}
505
506template <typename T>
507const T &Vec<T>::at(size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700508 if (n >= this->size()) {
509 panic<std::out_of_range>("rust::Vec index out of range");
510 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700511 return (*this)[n];
512}
513
514template <typename T>
515const T &Vec<T>::front() const {
516 return (*this)[0];
517}
518
519template <typename T>
520const T &Vec<T>::back() const {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700521 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700522}
523
524template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700525const T &Vec<T>::const_iterator::operator*() const noexcept {
526 return *static_cast<const T *>(this->pos);
527}
528
529template <typename T>
530const T *Vec<T>::const_iterator::operator->() const noexcept {
531 return static_cast<const T *>(this->pos);
532}
533
534template <typename T>
535typename Vec<T>::const_iterator &Vec<T>::const_iterator::operator++() noexcept {
536 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
537 return *this;
538}
539
540template <typename T>
541typename Vec<T>::const_iterator
542Vec<T>::const_iterator::operator++(int) noexcept {
543 auto ret = const_iterator(*this);
544 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
545 return ret;
546}
547
548template <typename T>
David Tolnayb10c4bc2020-08-26 21:55:29 -0700549bool Vec<T>::const_iterator::operator==(
550 const const_iterator &other) const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700551 return this->pos == other.pos;
552}
553
554template <typename T>
David Tolnayb10c4bc2020-08-26 21:55:29 -0700555bool Vec<T>::const_iterator::operator!=(
556 const const_iterator &other) const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700557 return this->pos != other.pos;
558}
559
560template <typename T>
561typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
562 const_iterator it;
563 it.pos = this->data();
564 it.stride = this->stride();
565 return it;
566}
567
568template <typename T>
569typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
570 const_iterator it = this->begin();
571 it.pos = static_cast<const uint8_t *>(it.pos) + it.stride * this->size();
572 return it;
573}
574
575// Internal API only intended for the cxxbridge code generator.
576template <typename T>
577Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay8f16ae72020-10-08 18:21:13 -0700578#endif // CXXBRIDGE05_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700579
David Tolnay174bd952020-11-02 09:23:12 -0800580#ifndef CXXBRIDGE05_RELOCATABLE
581#define CXXBRIDGE05_RELOCATABLE
582namespace detail {
583template <typename... Ts>
584struct make_void {
585 using type = void;
586};
587
588template <typename... Ts>
589using void_t = typename make_void<Ts...>::type;
590
591template <typename Void, template <typename...> class, typename...>
592struct detect : std::false_type {};
593template <template <typename...> class T, typename... A>
594struct detect<void_t<T<A...>>, T, A...> : std::true_type {};
595
596template <template <typename...> class T, typename... A>
597using is_detected = detect<void, T, A...>;
598
599template <typename T>
600using detect_IsRelocatable = typename T::IsRelocatable;
601
602template <typename T>
603struct get_IsRelocatable
604 : std::is_same<typename T::IsRelocatable, std::true_type> {};
605} // namespace detail
606
607template <typename T>
608struct IsRelocatable
609 : std::conditional<
610 detail::is_detected<detail::detect_IsRelocatable, T>::value,
611 detail::get_IsRelocatable<T>,
612 std::integral_constant<
613 bool, std::is_trivially_move_constructible<T>::value &&
614 std::is_trivially_destructible<T>::value>>::type {};
615#endif // CXXBRIDGE05_RELOCATABLE
616
David Tolnay8f16ae72020-10-08 18:21:13 -0700617} // namespace cxxbridge05
David Tolnay750755e2020-03-01 13:04:08 -0800618} // namespace rust