blob: d43854ca07e8ba773c431dd1b471561029c46f27 [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
60#define CXXBRIDGE05_RUST_STR
David Tolnay09dbe752020-03-01 13:00:40 -080061class Str final {
David Tolnay7db73692019-10-20 14:51:12 -040062public:
David Tolnay09dbe752020-03-01 13:00:40 -080063 Str() noexcept;
David Tolnay851677c2020-03-01 23:49:46 -080064 Str(const std::string &);
65 Str(const char *);
David Tolnay894c5e42020-07-29 18:20:00 -070066 Str(const char *, size_t);
David Tolnay851677c2020-03-01 23:49:46 -080067 Str(std::string &&) = delete;
David Tolnayd9c4ac92020-03-01 20:33:58 -080068
69 Str &operator=(Str) noexcept;
70
David Tolnay404d6892020-03-01 20:19:41 -080071 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040072
73 // Note: no null terminator.
74 const char *data() const noexcept;
75 size_t size() const noexcept;
76 size_t length() const noexcept;
77
David Tolnayde9a5b12020-10-31 12:15:43 -070078 // Important in order for System V ABI to pass in registers.
79 Str(const Str &) noexcept = default;
80 ~Str() noexcept = default;
81
David Tolnay7db73692019-10-20 14:51:12 -040082 // Repr is PRIVATE; must not be used other than by our generated code.
83 //
84 // Not necessarily ABI compatible with &str. Codegen will translate to
85 // cxx::rust_str::RustStr which matches this layout.
David Tolnay48521222020-10-31 14:59:42 -070086 struct Repr final {
David Tolnay7db73692019-10-20 14:51:12 -040087 const char *ptr;
88 size_t len;
89 };
David Tolnayd9c4ac92020-03-01 20:33:58 -080090 Str(Repr) noexcept;
David Tolnaycedcde12020-10-31 11:47:14 -070091 explicit operator Repr() const noexcept;
David Tolnay7db73692019-10-20 14:51:12 -040092
93private:
94 Repr repr;
95};
David Tolnay8f16ae72020-10-08 18:21:13 -070096#endif // CXXBRIDGE05_RUST_STR
David Tolnay7db73692019-10-20 14:51:12 -040097
David Tolnay8f16ae72020-10-08 18:21:13 -070098#ifndef CXXBRIDGE05_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -070099template <typename T>
100class Slice final {
101public:
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700102 Slice() noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700103 Slice(const T *, size_t count) noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700104
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700105 Slice &operator=(Slice<T>) noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700106
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700107 const T *data() const noexcept;
108 size_t size() const noexcept;
109 size_t length() const noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700110
David Tolnayde9a5b12020-10-31 12:15:43 -0700111 // Important in order for System V ABI to pass in registers.
112 Slice(const Slice<T> &) noexcept = default;
113 ~Slice() noexcept = default;
114
David Tolnayefe81052020-04-14 16:28:24 -0700115 // Repr is PRIVATE; must not be used other than by our generated code.
116 //
117 // At present this class is only used for &[u8] slices.
118 // Not necessarily ABI compatible with &[u8]. Codegen will translate to
David Tolnaye710af12020-04-14 16:31:54 -0700119 // cxx::rust_sliceu8::RustSliceU8 which matches this layout.
David Tolnay48521222020-10-31 14:59:42 -0700120 struct Repr final {
David Tolnayefe81052020-04-14 16:28:24 -0700121 const T *ptr;
122 size_t len;
123 };
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700124 Slice(Repr) noexcept;
David Tolnaycedcde12020-10-31 11:47:14 -0700125 explicit operator Repr() const noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700126
127private:
128 Repr repr;
129};
David Tolnay8f16ae72020-10-08 18:21:13 -0700130#endif // CXXBRIDGE05_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -0700131
David Tolnay8f16ae72020-10-08 18:21:13 -0700132#ifndef CXXBRIDGE05_RUST_BOX
David Tolnayf262d382020-04-11 22:12:40 -0700133template <typename T>
134class Box final {
David Tolnay7db73692019-10-20 14:51:12 -0400135public:
David Tolnayf6292372020-03-01 21:09:11 -0800136 using value_type = T;
David Tolnay9706a512020-04-24 17:09:01 -0700137 using const_pointer =
138 typename std::add_pointer<typename std::add_const<T>::type>::type;
139 using pointer = typename std::add_pointer<T>::type;
David Tolnayf6292372020-03-01 21:09:11 -0800140
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700141 Box(const Box &);
142 Box(Box &&) noexcept;
143 ~Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400144
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700145 explicit Box(const T &);
146 explicit Box(T &&);
147
148 Box &operator=(const Box &);
149 Box &operator=(Box &&) noexcept;
150
151 const T *operator->() const noexcept;
152 const T &operator*() const noexcept;
153 T *operator->() noexcept;
154 T &operator*() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400155
David Tolnayf262d382020-04-11 22:12:40 -0700156 template <typename... Fields>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700157 static Box in_place(Fields &&...);
David Tolnay7ce59fc2020-04-11 11:46:33 -0700158
David Tolnay7db73692019-10-20 14:51:12 -0400159 // Important: requires that `raw` came from an into_raw call. Do not pass a
160 // pointer from `new` or any other source.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700161 static Box from_raw(T *) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400162
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700163 T *into_raw() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400164
165private:
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700166 Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400167 void uninit() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400168 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800169 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400170};
David Tolnay8f16ae72020-10-08 18:21:13 -0700171#endif // CXXBRIDGE05_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400172
David Tolnay8f16ae72020-10-08 18:21:13 -0700173#ifndef CXXBRIDGE05_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700174template <typename T>
175class Vec final {
176public:
David Tolnayc87c2152020-04-24 17:07:41 -0700177 using value_type = T;
178
David Tolnayf97c2d52020-04-25 16:37:48 -0700179 Vec() noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700180 Vec(Vec &&) noexcept;
181 ~Vec() noexcept;
David Tolnaycb800572020-04-24 20:30:43 -0700182
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700183 Vec &operator=(Vec &&) noexcept;
David Tolnayf97c2d52020-04-25 16:37:48 -0700184
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700185 size_t size() const noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700186 bool empty() const noexcept;
David Tolnay219c0792020-04-24 20:31:37 -0700187 const T *data() const noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700188
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700189 const T &operator[](size_t n) const noexcept;
190 const T &at(size_t n) const;
191
192 const T &front() const;
193 const T &back() const;
194
David Tolnay48521222020-10-31 14:59:42 -0700195 class const_iterator final {
David Tolnayc87c2152020-04-24 17:07:41 -0700196 public:
myronahnda9be502020-04-29 05:47:23 +0700197 using difference_type = ptrdiff_t;
David Tolnayc87c2152020-04-24 17:07:41 -0700198 using value_type = typename std::add_const<T>::type;
David Tolnay74dd3792020-04-30 07:45:24 -0700199 using pointer =
200 typename std::add_pointer<typename std::add_const<T>::type>::type;
David Tolnayc87c2152020-04-24 17:07:41 -0700201 using reference = typename std::add_lvalue_reference<
202 typename std::add_const<T>::type>::type;
myronahnda9be502020-04-29 05:47:23 +0700203 using iterator_category = std::forward_iterator_tag;
David Tolnayc87c2152020-04-24 17:07:41 -0700204
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700205 const T &operator*() const noexcept;
206 const T *operator->() const noexcept;
207 const_iterator &operator++() noexcept;
208 const_iterator operator++(int) noexcept;
209 bool operator==(const const_iterator &) const noexcept;
210 bool operator!=(const const_iterator &) const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700211
212 private:
213 friend class Vec;
214 const void *pos;
215 size_t stride;
216 };
217
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700218 const_iterator begin() const noexcept;
219 const_iterator end() const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700220
David Tolnay313b10e2020-04-25 16:30:51 -0700221 // Internal API only intended for the cxxbridge code generator.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700222 Vec(unsafe_bitcopy_t, const Vec &) noexcept;
David Tolnay313b10e2020-04-25 16:30:51 -0700223
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700224private:
David Tolnay503d0192020-04-24 22:18:56 -0700225 static size_t stride() noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700226 void drop() noexcept;
227
228 // Size and alignment statically verified by rust_vec.rs.
229 std::array<uintptr_t, 3> repr;
230};
David Tolnay8f16ae72020-10-08 18:21:13 -0700231#endif // CXXBRIDGE05_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700232
David Tolnay8f16ae72020-10-08 18:21:13 -0700233#ifndef CXXBRIDGE05_RUST_FN
234#define CXXBRIDGE05_RUST_FN
David Tolnayf262d382020-04-11 22:12:40 -0700235template <typename Signature, bool Throws = false>
236class Fn;
David Tolnay75dca2e2020-03-25 20:17:52 -0700237
238template <typename Ret, typename... Args, bool Throws>
David Tolnay48521222020-10-31 14:59:42 -0700239class Fn<Ret(Args...), Throws> final {
David Tolnay75dca2e2020-03-25 20:17:52 -0700240public:
David Tolnay533d4582020-04-08 20:29:14 -0700241 Ret operator()(Args... args) const noexcept(!Throws);
242 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700243
244private:
245 Ret (*trampoline)(Args..., void *fn) noexcept(!Throws);
246 void *fn;
247};
248
David Tolnayf262d382020-04-11 22:12:40 -0700249template <typename Signature>
250using TryFn = Fn<Signature, true>;
David Tolnay8f16ae72020-10-08 18:21:13 -0700251#endif // CXXBRIDGE05_RUST_FN
David Tolnay75dca2e2020-03-25 20:17:52 -0700252
David Tolnay8f16ae72020-10-08 18:21:13 -0700253#ifndef CXXBRIDGE05_RUST_ERROR
254#define CXXBRIDGE05_RUST_ERROR
David Tolnaye4fa8732020-09-08 15:04:56 -0700255class Error final : public std::exception {
David Tolnay1e548172020-03-16 13:37:09 -0700256public:
257 Error(const Error &);
258 Error(Error &&) noexcept;
David Tolnay1e548172020-03-16 13:37:09 -0700259 ~Error() noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700260
261 Error &operator=(const Error &);
262
David Tolnay1e548172020-03-16 13:37:09 -0700263 const char *what() const noexcept override;
264
265private:
David Tolnaya0c9bc72020-10-31 14:37:14 -0700266 Error() noexcept = default;
David Tolnay84ddf9e2020-10-31 15:36:48 -0700267 friend impl<Error>;
David Tolnaya0c9bc72020-10-31 14:37:14 -0700268 const char *msg;
269 size_t len;
David Tolnay1e548172020-03-16 13:37:09 -0700270};
David Tolnay8f16ae72020-10-08 18:21:13 -0700271#endif // CXXBRIDGE05_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700272
David Tolnay8f16ae72020-10-08 18:21:13 -0700273#ifndef CXXBRIDGE05_RUST_ISIZE
274#define CXXBRIDGE05_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700275#if defined(_WIN32)
276using isize = SSIZE_T;
277#else
278using isize = ssize_t;
279#endif
David Tolnay8f16ae72020-10-08 18:21:13 -0700280#endif // CXXBRIDGE05_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700281
David Tolnay851677c2020-03-01 23:49:46 -0800282std::ostream &operator<<(std::ostream &, const String &);
283std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400284
David Tolnay3b0c9882020-03-01 14:08:57 -0800285// Snake case aliases for use in code that uses this style for type names.
286using string = String;
287using str = Str;
David Tolnayf262d382020-04-11 22:12:40 -0700288template <class T>
David Tolnay38c87642020-09-06 22:18:08 -0700289using slice = Slice<T>;
290template <class T>
David Tolnayf262d382020-04-11 22:12:40 -0700291using box = Box<T>;
David Tolnay38c87642020-09-06 22:18:08 -0700292template <class T>
293using vec = Vec<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700294using error = Error;
David Tolnay75dca2e2020-03-25 20:17:52 -0700295template <typename Signature, bool Throws = false>
296using fn = Fn<Signature, Throws>;
David Tolnayf262d382020-04-11 22:12:40 -0700297template <typename Signature>
298using try_fn = TryFn<Signature>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800299
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700300
301
302////////////////////////////////////////////////////////////////////////////////
303/// end public API, begin implementation details
304
David Tolnay8f16ae72020-10-08 18:21:13 -0700305#ifndef CXXBRIDGE05_PANIC
306#define CXXBRIDGE05_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700307template <typename Exception>
308void panic [[noreturn]] (const char *msg);
David Tolnay8f16ae72020-10-08 18:21:13 -0700309#endif // CXXBRIDGE05_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700310
David Tolnay75dca2e2020-03-25 20:17:52 -0700311template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700312Ret Fn<Ret(Args...), Throws>::operator()(Args... args) const noexcept(!Throws) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700313 return (*this->trampoline)(std::move(args)..., this->fn);
314}
315
David Tolnaya23129c2020-04-08 20:08:21 -0700316template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700317Fn<Ret(Args...), Throws> Fn<Ret(Args...), Throws>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700318 return *this;
319}
320
David Tolnay8f16ae72020-10-08 18:21:13 -0700321#ifndef CXXBRIDGE05_RUST_BITCOPY
322#define CXXBRIDGE05_RUST_BITCOPY
David Tolnay48521222020-10-31 14:59:42 -0700323struct unsafe_bitcopy_t final {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700324 explicit unsafe_bitcopy_t() = default;
325};
326
327constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnay8f16ae72020-10-08 18:21:13 -0700328#endif // CXXBRIDGE05_RUST_BITCOPY
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700329
David Tolnay8f16ae72020-10-08 18:21:13 -0700330#ifndef CXXBRIDGE05_RUST_SLICE
331#define CXXBRIDGE05_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700332template <typename T>
333Slice<T>::Slice() noexcept : repr(Repr{reinterpret_cast<const T *>(this), 0}) {}
334
335template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700336Slice<T>::Slice(const T *s, size_t count) noexcept : repr(Repr{s, count}) {}
337
338template <typename T>
339Slice<T> &Slice<T>::operator=(Slice<T> other) noexcept {
340 this->repr = other.repr;
341 return *this;
342}
343
344template <typename T>
345const T *Slice<T>::data() const noexcept {
346 return this->repr.ptr;
347}
348
349template <typename T>
350size_t Slice<T>::size() const noexcept {
351 return this->repr.len;
352}
353
354template <typename T>
355size_t Slice<T>::length() const noexcept {
356 return this->repr.len;
357}
358
359template <typename T>
360Slice<T>::Slice(Repr repr_) noexcept : repr(repr_) {}
361
362template <typename T>
David Tolnaycedcde12020-10-31 11:47:14 -0700363Slice<T>::operator Repr() const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700364 return this->repr;
365}
David Tolnay8f16ae72020-10-08 18:21:13 -0700366#endif // CXXBRIDGE05_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700367
David Tolnay8f16ae72020-10-08 18:21:13 -0700368#ifndef CXXBRIDGE05_RUST_BOX
369#define CXXBRIDGE05_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700370template <typename T>
371Box<T>::Box(const Box &other) : Box(*other) {}
372
373template <typename T>
374Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
375 other.ptr = nullptr;
376}
377
378template <typename T>
379Box<T>::Box(const T &val) {
380 this->uninit();
381 ::new (this->ptr) T(val);
382}
383
384template <typename T>
385Box<T>::Box(T &&val) {
386 this->uninit();
387 ::new (this->ptr) T(std::move(val));
388}
389
390template <typename T>
391Box<T>::~Box() noexcept {
392 if (this->ptr) {
393 this->drop();
394 }
395}
396
397template <typename T>
398Box<T> &Box<T>::operator=(const Box &other) {
399 if (this != &other) {
400 if (this->ptr) {
401 **this = *other;
402 } else {
403 this->uninit();
404 ::new (this->ptr) T(*other);
405 }
406 }
407 return *this;
408}
409
410template <typename T>
411Box<T> &Box<T>::operator=(Box &&other) noexcept {
412 if (this->ptr) {
413 this->drop();
414 }
415 this->ptr = other.ptr;
416 other.ptr = nullptr;
417 return *this;
418}
419
420template <typename T>
421const T *Box<T>::operator->() const noexcept {
422 return this->ptr;
423}
424
425template <typename T>
426const T &Box<T>::operator*() const noexcept {
427 return *this->ptr;
428}
429
430template <typename T>
431T *Box<T>::operator->() noexcept {
432 return this->ptr;
433}
434
435template <typename T>
436T &Box<T>::operator*() noexcept {
437 return *this->ptr;
438}
439
440template <typename T>
441template <typename... Fields>
442Box<T> Box<T>::in_place(Fields &&... fields) {
443 Box box;
444 box.uninit();
445 ::new (box.ptr) T{std::forward<Fields>(fields)...};
446 return box;
447}
448
449template <typename T>
450Box<T> Box<T>::from_raw(T *raw) noexcept {
451 Box box;
452 box.ptr = raw;
453 return box;
454}
455
456template <typename T>
457T *Box<T>::into_raw() noexcept {
458 T *raw = this->ptr;
459 this->ptr = nullptr;
460 return raw;
461}
462
463template <typename T>
464Box<T>::Box() noexcept {}
David Tolnay8f16ae72020-10-08 18:21:13 -0700465#endif // CXXBRIDGE05_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700466
David Tolnay8f16ae72020-10-08 18:21:13 -0700467#ifndef CXXBRIDGE05_RUST_VEC
468#define CXXBRIDGE05_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700469template <typename T>
470Vec<T>::Vec(Vec &&other) noexcept {
471 this->repr = other.repr;
472 new (&other) Vec();
473}
474
475template <typename T>
476Vec<T>::~Vec() noexcept {
477 this->drop();
478}
479
480template <typename T>
481Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
482 if (this != &other) {
483 this->drop();
484 this->repr = other.repr;
485 new (&other) Vec();
486 }
487 return *this;
488}
489
490template <typename T>
491bool Vec<T>::empty() const noexcept {
492 return size() == 0;
493}
494
495template <typename T>
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700496const T &Vec<T>::operator[](size_t n) const noexcept {
497 auto data = reinterpret_cast<const char *>(this->data());
498 return *reinterpret_cast<const T *>(data + n * this->stride());
499}
500
501template <typename T>
502const T &Vec<T>::at(size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700503 if (n >= this->size()) {
504 panic<std::out_of_range>("rust::Vec index out of range");
505 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700506 return (*this)[n];
507}
508
509template <typename T>
510const T &Vec<T>::front() const {
511 return (*this)[0];
512}
513
514template <typename T>
515const T &Vec<T>::back() const {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700516 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700517}
518
519template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700520const T &Vec<T>::const_iterator::operator*() const noexcept {
521 return *static_cast<const T *>(this->pos);
522}
523
524template <typename T>
525const T *Vec<T>::const_iterator::operator->() const noexcept {
526 return static_cast<const T *>(this->pos);
527}
528
529template <typename T>
530typename Vec<T>::const_iterator &Vec<T>::const_iterator::operator++() noexcept {
531 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
532 return *this;
533}
534
535template <typename T>
536typename Vec<T>::const_iterator
537Vec<T>::const_iterator::operator++(int) noexcept {
538 auto ret = const_iterator(*this);
539 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
540 return ret;
541}
542
543template <typename T>
David Tolnayb10c4bc2020-08-26 21:55:29 -0700544bool Vec<T>::const_iterator::operator==(
545 const const_iterator &other) const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700546 return this->pos == other.pos;
547}
548
549template <typename T>
David Tolnayb10c4bc2020-08-26 21:55:29 -0700550bool Vec<T>::const_iterator::operator!=(
551 const const_iterator &other) const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700552 return this->pos != other.pos;
553}
554
555template <typename T>
556typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
557 const_iterator it;
558 it.pos = this->data();
559 it.stride = this->stride();
560 return it;
561}
562
563template <typename T>
564typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
565 const_iterator it = this->begin();
566 it.pos = static_cast<const uint8_t *>(it.pos) + it.stride * this->size();
567 return it;
568}
569
570// Internal API only intended for the cxxbridge code generator.
571template <typename T>
572Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay8f16ae72020-10-08 18:21:13 -0700573#endif // CXXBRIDGE05_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700574
David Tolnay8f16ae72020-10-08 18:21:13 -0700575} // namespace cxxbridge05
David Tolnay750755e2020-03-01 13:04:08 -0800576} // namespace rust