blob: 83a0ccd6a229ef511072e050804c2a55f5221fc6 [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 {
David Tolnayce298232020-11-11 10:08:54 -080093 static_assert(std::is_const<T>::value,
94 "&[T] needs to be written as rust::Slice<const T> in C++");
95
David Tolnayefe81052020-04-14 16:28:24 -070096public:
David Tolnay2a2b9ad2020-05-12 20:07:26 -070097 Slice() noexcept;
David Tolnayce298232020-11-11 10:08:54 -080098 Slice(T *, size_t count) noexcept;
David Tolnayefe81052020-04-14 16:28:24 -070099
David Tolnay2d7f1172020-10-31 17:58:31 -0700100 Slice &operator=(const Slice<T> &) noexcept = default;
David Tolnayefe81052020-04-14 16:28:24 -0700101
David Tolnayce298232020-11-11 10:08:54 -0800102 T *data() const noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700103 size_t size() const noexcept;
104 size_t length() const noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700105
David Tolnayde9a5b12020-10-31 12:15:43 -0700106 // Important in order for System V ABI to pass in registers.
107 Slice(const Slice<T> &) noexcept = default;
108 ~Slice() noexcept = default;
109
David Tolnayefe81052020-04-14 16:28:24 -0700110private:
David Tolnay36aa9e02020-10-31 23:08:21 -0700111 friend impl<Slice>;
112 // Not necessarily ABI compatible with &[T]. Codegen will translate to
113 // cxx::rust_sliceu8::RustSliceU8 which matches this layout.
David Tolnayce298232020-11-11 10:08:54 -0800114 T *ptr;
David Tolnay36aa9e02020-10-31 23:08:21 -0700115 size_t len;
David Tolnayefe81052020-04-14 16:28:24 -0700116};
David Tolnay8f16ae72020-10-08 18:21:13 -0700117#endif // CXXBRIDGE05_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -0700118
David Tolnay8f16ae72020-10-08 18:21:13 -0700119#ifndef CXXBRIDGE05_RUST_BOX
David Tolnayf262d382020-04-11 22:12:40 -0700120template <typename T>
121class Box final {
David Tolnay7db73692019-10-20 14:51:12 -0400122public:
David Tolnayf6292372020-03-01 21:09:11 -0800123 using value_type = T;
David Tolnay9706a512020-04-24 17:09:01 -0700124 using const_pointer =
125 typename std::add_pointer<typename std::add_const<T>::type>::type;
126 using pointer = typename std::add_pointer<T>::type;
David Tolnayf6292372020-03-01 21:09:11 -0800127
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700128 Box(const Box &);
129 Box(Box &&) noexcept;
130 ~Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400131
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700132 explicit Box(const T &);
133 explicit Box(T &&);
134
135 Box &operator=(const Box &);
136 Box &operator=(Box &&) noexcept;
137
138 const T *operator->() const noexcept;
139 const T &operator*() const noexcept;
140 T *operator->() noexcept;
141 T &operator*() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400142
David Tolnayf262d382020-04-11 22:12:40 -0700143 template <typename... Fields>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700144 static Box in_place(Fields &&...);
David Tolnay7ce59fc2020-04-11 11:46:33 -0700145
David Tolnay7db73692019-10-20 14:51:12 -0400146 // Important: requires that `raw` came from an into_raw call. Do not pass a
147 // pointer from `new` or any other source.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700148 static Box from_raw(T *) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400149
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700150 T *into_raw() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400151
152private:
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700153 Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400154 void uninit() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400155 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800156 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400157};
David Tolnay8f16ae72020-10-08 18:21:13 -0700158#endif // CXXBRIDGE05_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400159
David Tolnay8f16ae72020-10-08 18:21:13 -0700160#ifndef CXXBRIDGE05_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700161template <typename T>
162class Vec final {
163public:
David Tolnayc87c2152020-04-24 17:07:41 -0700164 using value_type = T;
165
David Tolnayf97c2d52020-04-25 16:37:48 -0700166 Vec() noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700167 Vec(Vec &&) noexcept;
168 ~Vec() noexcept;
David Tolnaycb800572020-04-24 20:30:43 -0700169
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700170 Vec &operator=(Vec &&) noexcept;
David Tolnayf97c2d52020-04-25 16:37:48 -0700171
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700172 size_t size() const noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700173 bool empty() const noexcept;
David Tolnay219c0792020-04-24 20:31:37 -0700174 const T *data() const noexcept;
David Tolnayfb6b73c2020-11-10 14:32:16 -0800175 T *data() noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700176
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700177 const T &operator[](size_t n) const noexcept;
178 const T &at(size_t n) const;
179
180 const T &front() const;
181 const T &back() const;
182
David Tolnayfb6b73c2020-11-10 14:32:16 -0800183 void reserve(size_t new_cap);
184 void push_back(const T &value);
185 void push_back(T &&value);
186 template <class... Args>
187 void emplace_back(Args &&... args);
188
David Tolnay48521222020-10-31 14:59:42 -0700189 class const_iterator final {
David Tolnayc87c2152020-04-24 17:07:41 -0700190 public:
myronahnda9be502020-04-29 05:47:23 +0700191 using difference_type = ptrdiff_t;
David Tolnayc87c2152020-04-24 17:07:41 -0700192 using value_type = typename std::add_const<T>::type;
David Tolnay74dd3792020-04-30 07:45:24 -0700193 using pointer =
194 typename std::add_pointer<typename std::add_const<T>::type>::type;
David Tolnayc87c2152020-04-24 17:07:41 -0700195 using reference = typename std::add_lvalue_reference<
196 typename std::add_const<T>::type>::type;
myronahnda9be502020-04-29 05:47:23 +0700197 using iterator_category = std::forward_iterator_tag;
David Tolnayc87c2152020-04-24 17:07:41 -0700198
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700199 const T &operator*() const noexcept;
200 const T *operator->() const noexcept;
201 const_iterator &operator++() noexcept;
202 const_iterator operator++(int) noexcept;
203 bool operator==(const const_iterator &) const noexcept;
204 bool operator!=(const const_iterator &) const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700205
206 private:
207 friend class Vec;
208 const void *pos;
209 size_t stride;
210 };
211
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700212 const_iterator begin() const noexcept;
213 const_iterator end() const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700214
David Tolnay313b10e2020-04-25 16:30:51 -0700215 // Internal API only intended for the cxxbridge code generator.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700216 Vec(unsafe_bitcopy_t, const Vec &) noexcept;
David Tolnay313b10e2020-04-25 16:30:51 -0700217
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700218private:
David Tolnay503d0192020-04-24 22:18:56 -0700219 static size_t stride() noexcept;
David Tolnayfb6b73c2020-11-10 14:32:16 -0800220 void reserve_total(size_t cap) noexcept;
221 void set_len(size_t len) noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700222 void drop() noexcept;
223
224 // Size and alignment statically verified by rust_vec.rs.
225 std::array<uintptr_t, 3> repr;
226};
David Tolnay8f16ae72020-10-08 18:21:13 -0700227#endif // CXXBRIDGE05_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700228
David Tolnay8f16ae72020-10-08 18:21:13 -0700229#ifndef CXXBRIDGE05_RUST_FN
David Tolnayf262d382020-04-11 22:12:40 -0700230template <typename Signature, bool Throws = false>
231class Fn;
David Tolnay75dca2e2020-03-25 20:17:52 -0700232
233template <typename Ret, typename... Args, bool Throws>
David Tolnay48521222020-10-31 14:59:42 -0700234class Fn<Ret(Args...), Throws> final {
David Tolnay75dca2e2020-03-25 20:17:52 -0700235public:
David Tolnay533d4582020-04-08 20:29:14 -0700236 Ret operator()(Args... args) const noexcept(!Throws);
237 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700238
239private:
240 Ret (*trampoline)(Args..., void *fn) noexcept(!Throws);
241 void *fn;
242};
243
David Tolnayf262d382020-04-11 22:12:40 -0700244template <typename Signature>
245using TryFn = Fn<Signature, true>;
David Tolnay8f16ae72020-10-08 18:21:13 -0700246#endif // CXXBRIDGE05_RUST_FN
David Tolnay75dca2e2020-03-25 20:17:52 -0700247
David Tolnay8f16ae72020-10-08 18:21:13 -0700248#ifndef CXXBRIDGE05_RUST_ERROR
249#define CXXBRIDGE05_RUST_ERROR
David Tolnaye4fa8732020-09-08 15:04:56 -0700250class Error final : public std::exception {
David Tolnay1e548172020-03-16 13:37:09 -0700251public:
252 Error(const Error &);
253 Error(Error &&) noexcept;
David Tolnay1e548172020-03-16 13:37:09 -0700254 ~Error() noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700255
256 Error &operator=(const Error &);
David Tolnay15491062020-10-31 17:25:13 -0700257 Error &operator=(Error &&) noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700258
David Tolnay1e548172020-03-16 13:37:09 -0700259 const char *what() const noexcept override;
260
261private:
David Tolnaya0c9bc72020-10-31 14:37:14 -0700262 Error() noexcept = default;
David Tolnay84ddf9e2020-10-31 15:36:48 -0700263 friend impl<Error>;
David Tolnaya0c9bc72020-10-31 14:37:14 -0700264 const char *msg;
265 size_t len;
David Tolnay1e548172020-03-16 13:37:09 -0700266};
David Tolnay8f16ae72020-10-08 18:21:13 -0700267#endif // CXXBRIDGE05_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700268
David Tolnay8f16ae72020-10-08 18:21:13 -0700269#ifndef CXXBRIDGE05_RUST_ISIZE
270#define CXXBRIDGE05_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700271#if defined(_WIN32)
272using isize = SSIZE_T;
273#else
274using isize = ssize_t;
275#endif
David Tolnay8f16ae72020-10-08 18:21:13 -0700276#endif // CXXBRIDGE05_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700277
David Tolnay851677c2020-03-01 23:49:46 -0800278std::ostream &operator<<(std::ostream &, const String &);
279std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400280
David Tolnay174bd952020-11-02 09:23:12 -0800281// IsRelocatable<T> is used in assertions that a C++ type passed by value
282// between Rust and C++ is soundly relocatable by Rust.
283//
284// There may be legitimate reasons to opt out of the check for support of types
285// that the programmer knows are soundly Rust-movable despite not being
286// recognized as such by the C++ type system due to a move constructor or
287// destructor. To opt out of the relocatability check, do either of the
288// following things in any header used by `include!` in the bridge.
289//
290// --- if you define the type:
291// struct MyType {
292// ...
293// + using IsRelocatable = std::true_type;
294// };
295//
296// --- otherwise:
297// + template <>
298// + struct rust::IsRelocatable<MyType> : std::true_type {};
299template <typename T>
300struct IsRelocatable;
301
David Tolnay3b0c9882020-03-01 14:08:57 -0800302// Snake case aliases for use in code that uses this style for type names.
303using string = String;
304using str = Str;
David Tolnayf262d382020-04-11 22:12:40 -0700305template <class T>
David Tolnay38c87642020-09-06 22:18:08 -0700306using slice = Slice<T>;
307template <class T>
David Tolnayf262d382020-04-11 22:12:40 -0700308using box = Box<T>;
David Tolnay38c87642020-09-06 22:18:08 -0700309template <class T>
310using vec = Vec<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700311using error = Error;
David Tolnay75dca2e2020-03-25 20:17:52 -0700312template <typename Signature, bool Throws = false>
313using fn = Fn<Signature, Throws>;
David Tolnayf262d382020-04-11 22:12:40 -0700314template <typename Signature>
315using try_fn = TryFn<Signature>;
David Tolnay174bd952020-11-02 09:23:12 -0800316template <typename T>
317using is_relocatable = IsRelocatable<T>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800318
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700319
320
321////////////////////////////////////////////////////////////////////////////////
322/// end public API, begin implementation details
323
David Tolnay8f16ae72020-10-08 18:21:13 -0700324#ifndef CXXBRIDGE05_PANIC
325#define CXXBRIDGE05_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700326template <typename Exception>
327void panic [[noreturn]] (const char *msg);
David Tolnay8f16ae72020-10-08 18:21:13 -0700328#endif // CXXBRIDGE05_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700329
David Tolnaya5a13012020-11-10 15:03:09 -0800330#ifndef CXXBRIDGE05_RUST_FN
331#define CXXBRIDGE05_RUST_FN
David Tolnay75dca2e2020-03-25 20:17:52 -0700332template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700333Ret Fn<Ret(Args...), Throws>::operator()(Args... args) const noexcept(!Throws) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700334 return (*this->trampoline)(std::move(args)..., this->fn);
335}
336
David Tolnaya23129c2020-04-08 20:08:21 -0700337template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700338Fn<Ret(Args...), Throws> Fn<Ret(Args...), Throws>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700339 return *this;
340}
David Tolnaya5a13012020-11-10 15:03:09 -0800341#endif // CXXBRIDGE05_RUST_FN
David Tolnaya23129c2020-04-08 20:08:21 -0700342
David Tolnay8f16ae72020-10-08 18:21:13 -0700343#ifndef CXXBRIDGE05_RUST_BITCOPY
344#define CXXBRIDGE05_RUST_BITCOPY
David Tolnay48521222020-10-31 14:59:42 -0700345struct unsafe_bitcopy_t final {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700346 explicit unsafe_bitcopy_t() = default;
347};
348
349constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnay8f16ae72020-10-08 18:21:13 -0700350#endif // CXXBRIDGE05_RUST_BITCOPY
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700351
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700352#ifndef CXXBRIDGE05_RUST_STR
353#define CXXBRIDGE05_RUST_STR
354inline const char *Str::data() const noexcept { return this->ptr; }
355
356inline size_t Str::size() const noexcept { return this->len; }
357
358inline size_t Str::length() const noexcept { return this->len; }
359#endif // CXXBRIDGE05_RUST_STR
360
David Tolnay8f16ae72020-10-08 18:21:13 -0700361#ifndef CXXBRIDGE05_RUST_SLICE
362#define CXXBRIDGE05_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700363template <typename T>
David Tolnayce298232020-11-11 10:08:54 -0800364Slice<T>::Slice() noexcept : ptr(reinterpret_cast<T *>(this)), len(0) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700365
366template <typename T>
David Tolnayce298232020-11-11 10:08:54 -0800367Slice<T>::Slice(T *s, size_t count) noexcept : ptr(s), len(count) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700368
369template <typename T>
David Tolnayce298232020-11-11 10:08:54 -0800370T *Slice<T>::data() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700371 return this->ptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700372}
373
374template <typename T>
375size_t Slice<T>::size() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700376 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700377}
378
379template <typename T>
380size_t Slice<T>::length() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700381 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700382}
David Tolnay8f16ae72020-10-08 18:21:13 -0700383#endif // CXXBRIDGE05_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700384
David Tolnay8f16ae72020-10-08 18:21:13 -0700385#ifndef CXXBRIDGE05_RUST_BOX
386#define CXXBRIDGE05_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700387template <typename T>
388Box<T>::Box(const Box &other) : Box(*other) {}
389
390template <typename T>
391Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
392 other.ptr = nullptr;
393}
394
395template <typename T>
396Box<T>::Box(const T &val) {
397 this->uninit();
398 ::new (this->ptr) T(val);
399}
400
401template <typename T>
402Box<T>::Box(T &&val) {
403 this->uninit();
404 ::new (this->ptr) T(std::move(val));
405}
406
407template <typename T>
408Box<T>::~Box() noexcept {
409 if (this->ptr) {
410 this->drop();
411 }
412}
413
414template <typename T>
415Box<T> &Box<T>::operator=(const Box &other) {
416 if (this != &other) {
417 if (this->ptr) {
418 **this = *other;
419 } else {
420 this->uninit();
421 ::new (this->ptr) T(*other);
422 }
423 }
424 return *this;
425}
426
427template <typename T>
428Box<T> &Box<T>::operator=(Box &&other) noexcept {
429 if (this->ptr) {
430 this->drop();
431 }
432 this->ptr = other.ptr;
433 other.ptr = nullptr;
434 return *this;
435}
436
437template <typename T>
438const T *Box<T>::operator->() const noexcept {
439 return this->ptr;
440}
441
442template <typename T>
443const T &Box<T>::operator*() const noexcept {
444 return *this->ptr;
445}
446
447template <typename T>
448T *Box<T>::operator->() noexcept {
449 return this->ptr;
450}
451
452template <typename T>
453T &Box<T>::operator*() noexcept {
454 return *this->ptr;
455}
456
457template <typename T>
458template <typename... Fields>
459Box<T> Box<T>::in_place(Fields &&... fields) {
460 Box box;
461 box.uninit();
462 ::new (box.ptr) T{std::forward<Fields>(fields)...};
463 return box;
464}
465
466template <typename T>
467Box<T> Box<T>::from_raw(T *raw) noexcept {
468 Box box;
469 box.ptr = raw;
470 return box;
471}
472
473template <typename T>
474T *Box<T>::into_raw() noexcept {
475 T *raw = this->ptr;
476 this->ptr = nullptr;
477 return raw;
478}
479
480template <typename T>
481Box<T>::Box() noexcept {}
David Tolnay8f16ae72020-10-08 18:21:13 -0700482#endif // CXXBRIDGE05_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700483
David Tolnay8f16ae72020-10-08 18:21:13 -0700484#ifndef CXXBRIDGE05_RUST_VEC
485#define CXXBRIDGE05_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700486template <typename T>
487Vec<T>::Vec(Vec &&other) noexcept {
488 this->repr = other.repr;
489 new (&other) Vec();
490}
491
492template <typename T>
493Vec<T>::~Vec() noexcept {
494 this->drop();
495}
496
497template <typename T>
498Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
499 if (this != &other) {
500 this->drop();
501 this->repr = other.repr;
502 new (&other) Vec();
503 }
504 return *this;
505}
506
507template <typename T>
508bool Vec<T>::empty() const noexcept {
509 return size() == 0;
510}
511
512template <typename T>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800513T *Vec<T>::data() noexcept {
514 return const_cast<T *>(const_cast<const Vec<T> *>(this)->data());
515}
516
517template <typename T>
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700518const T &Vec<T>::operator[](size_t n) const noexcept {
519 auto data = reinterpret_cast<const char *>(this->data());
520 return *reinterpret_cast<const T *>(data + n * this->stride());
521}
522
523template <typename T>
524const T &Vec<T>::at(size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700525 if (n >= this->size()) {
526 panic<std::out_of_range>("rust::Vec index out of range");
527 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700528 return (*this)[n];
529}
530
531template <typename T>
532const T &Vec<T>::front() const {
533 return (*this)[0];
534}
535
536template <typename T>
537const T &Vec<T>::back() const {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700538 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700539}
540
541template <typename T>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800542void Vec<T>::reserve(size_t new_cap) {
543 this->reserve_total(new_cap);
544}
545
546template <typename T>
547void Vec<T>::push_back(const T &value) {
548 this->emplace_back(value);
549}
550
551template <typename T>
552void Vec<T>::push_back(T &&value) {
553 this->emplace_back(std::move(value));
554}
555
556template <typename T>
557template <typename... Args>
558void Vec<T>::emplace_back(Args &&... args) {
559 auto size = this->size();
560 this->reserve_total(size + 1);
561 ::new (reinterpret_cast<T *>(reinterpret_cast<char *>(this->data()) +
562 size * this->stride()))
563 T(std::forward<Args>(args)...);
564 this->set_len(size + 1);
565}
566
567template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700568const T &Vec<T>::const_iterator::operator*() const noexcept {
569 return *static_cast<const T *>(this->pos);
570}
571
572template <typename T>
573const T *Vec<T>::const_iterator::operator->() const noexcept {
574 return static_cast<const T *>(this->pos);
575}
576
577template <typename T>
578typename Vec<T>::const_iterator &Vec<T>::const_iterator::operator++() noexcept {
579 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
580 return *this;
581}
582
583template <typename T>
584typename Vec<T>::const_iterator
585Vec<T>::const_iterator::operator++(int) noexcept {
586 auto ret = const_iterator(*this);
587 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
588 return ret;
589}
590
591template <typename T>
David Tolnayb10c4bc2020-08-26 21:55:29 -0700592bool Vec<T>::const_iterator::operator==(
593 const const_iterator &other) const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700594 return this->pos == other.pos;
595}
596
597template <typename T>
David Tolnayb10c4bc2020-08-26 21:55:29 -0700598bool Vec<T>::const_iterator::operator!=(
599 const const_iterator &other) const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700600 return this->pos != other.pos;
601}
602
603template <typename T>
604typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
605 const_iterator it;
606 it.pos = this->data();
607 it.stride = this->stride();
608 return it;
609}
610
611template <typename T>
612typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
613 const_iterator it = this->begin();
614 it.pos = static_cast<const uint8_t *>(it.pos) + it.stride * this->size();
615 return it;
616}
617
618// Internal API only intended for the cxxbridge code generator.
619template <typename T>
620Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay8f16ae72020-10-08 18:21:13 -0700621#endif // CXXBRIDGE05_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700622
David Tolnay174bd952020-11-02 09:23:12 -0800623#ifndef CXXBRIDGE05_RELOCATABLE
624#define CXXBRIDGE05_RELOCATABLE
625namespace detail {
626template <typename... Ts>
627struct make_void {
628 using type = void;
629};
630
631template <typename... Ts>
632using void_t = typename make_void<Ts...>::type;
633
634template <typename Void, template <typename...> class, typename...>
635struct detect : std::false_type {};
636template <template <typename...> class T, typename... A>
637struct detect<void_t<T<A...>>, T, A...> : std::true_type {};
638
639template <template <typename...> class T, typename... A>
640using is_detected = detect<void, T, A...>;
641
642template <typename T>
643using detect_IsRelocatable = typename T::IsRelocatable;
644
645template <typename T>
646struct get_IsRelocatable
647 : std::is_same<typename T::IsRelocatable, std::true_type> {};
648} // namespace detail
649
650template <typename T>
651struct IsRelocatable
652 : std::conditional<
653 detail::is_detected<detail::detect_IsRelocatable, T>::value,
654 detail::get_IsRelocatable<T>,
655 std::integral_constant<
656 bool, std::is_trivially_move_constructible<T>::value &&
657 std::is_trivially_destructible<T>::value>>::type {};
658#endif // CXXBRIDGE05_RELOCATABLE
659
David Tolnay8f16ae72020-10-08 18:21:13 -0700660} // namespace cxxbridge05
David Tolnay750755e2020-03-01 13:04:08 -0800661} // namespace rust