blob: d044111b74d55353799cfa555c88da48629def3d [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 Tolnay8f16ae72020-10-08 18:21:13 -070022#ifndef CXXBRIDGE05_RUST_STRING
23#define CXXBRIDGE05_RUST_STRING
David Tolnay56082162020-03-01 12:57:33 -080024class String final {
David Tolnay7db73692019-10-20 14:51:12 -040025public:
David Tolnay56082162020-03-01 12:57:33 -080026 String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080027 String(const String &) noexcept;
28 String(String &&) noexcept;
David Tolnay56082162020-03-01 12:57:33 -080029 ~String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080030
31 String(const std::string &);
32 String(const char *);
David Tolnayc2bbd952020-07-29 18:15:26 -070033 String(const char *, size_t);
David Tolnayd9c4ac92020-03-01 20:33:58 -080034
35 String &operator=(const String &) noexcept;
36 String &operator=(String &&) noexcept;
37
David Tolnay404d6892020-03-01 20:19:41 -080038 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040039
40 // Note: no null terminator.
41 const char *data() const noexcept;
42 size_t size() const noexcept;
43 size_t length() const noexcept;
44
David Tolnayd1e2efc2020-03-03 22:25:43 -080045 // Internal API only intended for the cxxbridge code generator.
46 String(unsafe_bitcopy_t, const String &) noexcept;
47
David Tolnay7db73692019-10-20 14:51:12 -040048private:
49 // Size and alignment statically verified by rust_string.rs.
50 std::array<uintptr_t, 3> repr;
51};
David Tolnay8f16ae72020-10-08 18:21:13 -070052#endif // CXXBRIDGE05_RUST_STRING
David Tolnay7db73692019-10-20 14:51:12 -040053
David Tolnay8f16ae72020-10-08 18:21:13 -070054#ifndef CXXBRIDGE05_RUST_STR
55#define CXXBRIDGE05_RUST_STR
David Tolnay09dbe752020-03-01 13:00:40 -080056class Str final {
David Tolnay7db73692019-10-20 14:51:12 -040057public:
David Tolnay09dbe752020-03-01 13:00:40 -080058 Str() noexcept;
David Tolnay851677c2020-03-01 23:49:46 -080059 Str(const std::string &);
60 Str(const char *);
David Tolnay894c5e42020-07-29 18:20:00 -070061 Str(const char *, size_t);
David Tolnay851677c2020-03-01 23:49:46 -080062 Str(std::string &&) = delete;
David Tolnayd9c4ac92020-03-01 20:33:58 -080063
64 Str &operator=(Str) noexcept;
65
David Tolnay404d6892020-03-01 20:19:41 -080066 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040067
68 // Note: no null terminator.
69 const char *data() const noexcept;
70 size_t size() const noexcept;
71 size_t length() const noexcept;
72
David Tolnayde9a5b12020-10-31 12:15:43 -070073 // Important in order for System V ABI to pass in registers.
74 Str(const Str &) noexcept = default;
75 ~Str() noexcept = default;
76
David Tolnay7db73692019-10-20 14:51:12 -040077 // Repr is PRIVATE; must not be used other than by our generated code.
78 //
79 // Not necessarily ABI compatible with &str. Codegen will translate to
80 // cxx::rust_str::RustStr which matches this layout.
David Tolnay48521222020-10-31 14:59:42 -070081 struct Repr final {
David Tolnay7db73692019-10-20 14:51:12 -040082 const char *ptr;
83 size_t len;
84 };
David Tolnayd9c4ac92020-03-01 20:33:58 -080085 Str(Repr) noexcept;
David Tolnaycedcde12020-10-31 11:47:14 -070086 explicit operator Repr() const noexcept;
David Tolnay7db73692019-10-20 14:51:12 -040087
88private:
89 Repr repr;
90};
David Tolnay8f16ae72020-10-08 18:21:13 -070091#endif // CXXBRIDGE05_RUST_STR
David Tolnay7db73692019-10-20 14:51:12 -040092
David Tolnay8f16ae72020-10-08 18:21:13 -070093#ifndef CXXBRIDGE05_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -070094template <typename T>
95class Slice final {
96public:
David Tolnay2a2b9ad2020-05-12 20:07:26 -070097 Slice() noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -070098 Slice(const T *, size_t count) noexcept;
David Tolnayefe81052020-04-14 16:28:24 -070099
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700100 Slice &operator=(Slice<T>) noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700101
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700102 const T *data() const noexcept;
103 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 -0700110 // Repr is PRIVATE; must not be used other than by our generated code.
111 //
112 // At present this class is only used for &[u8] slices.
113 // Not necessarily ABI compatible with &[u8]. Codegen will translate to
David Tolnaye710af12020-04-14 16:31:54 -0700114 // cxx::rust_sliceu8::RustSliceU8 which matches this layout.
David Tolnay48521222020-10-31 14:59:42 -0700115 struct Repr final {
David Tolnayefe81052020-04-14 16:28:24 -0700116 const T *ptr;
117 size_t len;
118 };
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700119 Slice(Repr) noexcept;
David Tolnaycedcde12020-10-31 11:47:14 -0700120 explicit operator Repr() const noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700121
122private:
123 Repr repr;
124};
David Tolnay8f16ae72020-10-08 18:21:13 -0700125#endif // CXXBRIDGE05_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -0700126
David Tolnay8f16ae72020-10-08 18:21:13 -0700127#ifndef CXXBRIDGE05_RUST_BOX
David Tolnayf262d382020-04-11 22:12:40 -0700128template <typename T>
129class Box final {
David Tolnay7db73692019-10-20 14:51:12 -0400130public:
David Tolnayf6292372020-03-01 21:09:11 -0800131 using value_type = T;
David Tolnay9706a512020-04-24 17:09:01 -0700132 using const_pointer =
133 typename std::add_pointer<typename std::add_const<T>::type>::type;
134 using pointer = typename std::add_pointer<T>::type;
David Tolnayf6292372020-03-01 21:09:11 -0800135
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700136 Box(const Box &);
137 Box(Box &&) noexcept;
138 ~Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400139
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700140 explicit Box(const T &);
141 explicit Box(T &&);
142
143 Box &operator=(const Box &);
144 Box &operator=(Box &&) noexcept;
145
146 const T *operator->() const noexcept;
147 const T &operator*() const noexcept;
148 T *operator->() noexcept;
149 T &operator*() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400150
David Tolnayf262d382020-04-11 22:12:40 -0700151 template <typename... Fields>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700152 static Box in_place(Fields &&...);
David Tolnay7ce59fc2020-04-11 11:46:33 -0700153
David Tolnay7db73692019-10-20 14:51:12 -0400154 // Important: requires that `raw` came from an into_raw call. Do not pass a
155 // pointer from `new` or any other source.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700156 static Box from_raw(T *) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400157
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700158 T *into_raw() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400159
160private:
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700161 Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400162 void uninit() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400163 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800164 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400165};
David Tolnay8f16ae72020-10-08 18:21:13 -0700166#endif // CXXBRIDGE05_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400167
David Tolnay8f16ae72020-10-08 18:21:13 -0700168#ifndef CXXBRIDGE05_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700169template <typename T>
170class Vec final {
171public:
David Tolnayc87c2152020-04-24 17:07:41 -0700172 using value_type = T;
173
David Tolnayf97c2d52020-04-25 16:37:48 -0700174 Vec() noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700175 Vec(Vec &&) noexcept;
176 ~Vec() noexcept;
David Tolnaycb800572020-04-24 20:30:43 -0700177
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700178 Vec &operator=(Vec &&) noexcept;
David Tolnayf97c2d52020-04-25 16:37:48 -0700179
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700180 size_t size() const noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700181 bool empty() const noexcept;
David Tolnay219c0792020-04-24 20:31:37 -0700182 const T *data() const noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700183
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700184 const T &operator[](size_t n) const noexcept;
185 const T &at(size_t n) const;
186
187 const T &front() const;
188 const T &back() const;
189
David Tolnay48521222020-10-31 14:59:42 -0700190 class const_iterator final {
David Tolnayc87c2152020-04-24 17:07:41 -0700191 public:
myronahnda9be502020-04-29 05:47:23 +0700192 using difference_type = ptrdiff_t;
David Tolnayc87c2152020-04-24 17:07:41 -0700193 using value_type = typename std::add_const<T>::type;
David Tolnay74dd3792020-04-30 07:45:24 -0700194 using pointer =
195 typename std::add_pointer<typename std::add_const<T>::type>::type;
David Tolnayc87c2152020-04-24 17:07:41 -0700196 using reference = typename std::add_lvalue_reference<
197 typename std::add_const<T>::type>::type;
myronahnda9be502020-04-29 05:47:23 +0700198 using iterator_category = std::forward_iterator_tag;
David Tolnayc87c2152020-04-24 17:07:41 -0700199
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700200 const T &operator*() const noexcept;
201 const T *operator->() const noexcept;
202 const_iterator &operator++() noexcept;
203 const_iterator operator++(int) noexcept;
204 bool operator==(const const_iterator &) const noexcept;
205 bool operator!=(const const_iterator &) const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700206
207 private:
208 friend class Vec;
209 const void *pos;
210 size_t stride;
211 };
212
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700213 const_iterator begin() const noexcept;
214 const_iterator end() const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700215
David Tolnay313b10e2020-04-25 16:30:51 -0700216 // Internal API only intended for the cxxbridge code generator.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700217 Vec(unsafe_bitcopy_t, const Vec &) noexcept;
David Tolnay313b10e2020-04-25 16:30:51 -0700218
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700219private:
David Tolnay503d0192020-04-24 22:18:56 -0700220 static size_t stride() noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700221 void drop() noexcept;
222
223 // Size and alignment statically verified by rust_vec.rs.
224 std::array<uintptr_t, 3> repr;
225};
David Tolnay8f16ae72020-10-08 18:21:13 -0700226#endif // CXXBRIDGE05_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700227
David Tolnay8f16ae72020-10-08 18:21:13 -0700228#ifndef CXXBRIDGE05_RUST_FN
229#define 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;
255 const char *what() const noexcept override;
256
257private:
David Tolnaya0c9bc72020-10-31 14:37:14 -0700258 Error() noexcept = default;
259 friend class impl;
260 const char *msg;
261 size_t len;
David Tolnay1e548172020-03-16 13:37:09 -0700262};
David Tolnay8f16ae72020-10-08 18:21:13 -0700263#endif // CXXBRIDGE05_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700264
David Tolnay8f16ae72020-10-08 18:21:13 -0700265#ifndef CXXBRIDGE05_RUST_ISIZE
266#define CXXBRIDGE05_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700267#if defined(_WIN32)
268using isize = SSIZE_T;
269#else
270using isize = ssize_t;
271#endif
David Tolnay8f16ae72020-10-08 18:21:13 -0700272#endif // CXXBRIDGE05_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700273
David Tolnay851677c2020-03-01 23:49:46 -0800274std::ostream &operator<<(std::ostream &, const String &);
275std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400276
David Tolnay3b0c9882020-03-01 14:08:57 -0800277// Snake case aliases for use in code that uses this style for type names.
278using string = String;
279using str = Str;
David Tolnayf262d382020-04-11 22:12:40 -0700280template <class T>
David Tolnay38c87642020-09-06 22:18:08 -0700281using slice = Slice<T>;
282template <class T>
David Tolnayf262d382020-04-11 22:12:40 -0700283using box = Box<T>;
David Tolnay38c87642020-09-06 22:18:08 -0700284template <class T>
285using vec = Vec<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700286using error = Error;
David Tolnay75dca2e2020-03-25 20:17:52 -0700287template <typename Signature, bool Throws = false>
288using fn = Fn<Signature, Throws>;
David Tolnayf262d382020-04-11 22:12:40 -0700289template <typename Signature>
290using try_fn = TryFn<Signature>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800291
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700292
293
294////////////////////////////////////////////////////////////////////////////////
295/// end public API, begin implementation details
296
David Tolnay8f16ae72020-10-08 18:21:13 -0700297#ifndef CXXBRIDGE05_PANIC
298#define CXXBRIDGE05_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700299template <typename Exception>
300void panic [[noreturn]] (const char *msg);
David Tolnay8f16ae72020-10-08 18:21:13 -0700301#endif // CXXBRIDGE05_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700302
David Tolnay75dca2e2020-03-25 20:17:52 -0700303template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700304Ret Fn<Ret(Args...), Throws>::operator()(Args... args) const noexcept(!Throws) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700305 return (*this->trampoline)(std::move(args)..., this->fn);
306}
307
David Tolnaya23129c2020-04-08 20:08:21 -0700308template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700309Fn<Ret(Args...), Throws> Fn<Ret(Args...), Throws>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700310 return *this;
311}
312
David Tolnay8f16ae72020-10-08 18:21:13 -0700313#ifndef CXXBRIDGE05_RUST_BITCOPY
314#define CXXBRIDGE05_RUST_BITCOPY
David Tolnay48521222020-10-31 14:59:42 -0700315struct unsafe_bitcopy_t final {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700316 explicit unsafe_bitcopy_t() = default;
317};
318
319constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnay8f16ae72020-10-08 18:21:13 -0700320#endif // CXXBRIDGE05_RUST_BITCOPY
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700321
David Tolnay8f16ae72020-10-08 18:21:13 -0700322#ifndef CXXBRIDGE05_RUST_SLICE
323#define CXXBRIDGE05_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700324template <typename T>
325Slice<T>::Slice() noexcept : repr(Repr{reinterpret_cast<const T *>(this), 0}) {}
326
327template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700328Slice<T>::Slice(const T *s, size_t count) noexcept : repr(Repr{s, count}) {}
329
330template <typename T>
331Slice<T> &Slice<T>::operator=(Slice<T> other) noexcept {
332 this->repr = other.repr;
333 return *this;
334}
335
336template <typename T>
337const T *Slice<T>::data() const noexcept {
338 return this->repr.ptr;
339}
340
341template <typename T>
342size_t Slice<T>::size() const noexcept {
343 return this->repr.len;
344}
345
346template <typename T>
347size_t Slice<T>::length() const noexcept {
348 return this->repr.len;
349}
350
351template <typename T>
352Slice<T>::Slice(Repr repr_) noexcept : repr(repr_) {}
353
354template <typename T>
David Tolnaycedcde12020-10-31 11:47:14 -0700355Slice<T>::operator Repr() const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700356 return this->repr;
357}
David Tolnay8f16ae72020-10-08 18:21:13 -0700358#endif // CXXBRIDGE05_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700359
David Tolnay8f16ae72020-10-08 18:21:13 -0700360#ifndef CXXBRIDGE05_RUST_BOX
361#define CXXBRIDGE05_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700362template <typename T>
363Box<T>::Box(const Box &other) : Box(*other) {}
364
365template <typename T>
366Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
367 other.ptr = nullptr;
368}
369
370template <typename T>
371Box<T>::Box(const T &val) {
372 this->uninit();
373 ::new (this->ptr) T(val);
374}
375
376template <typename T>
377Box<T>::Box(T &&val) {
378 this->uninit();
379 ::new (this->ptr) T(std::move(val));
380}
381
382template <typename T>
383Box<T>::~Box() noexcept {
384 if (this->ptr) {
385 this->drop();
386 }
387}
388
389template <typename T>
390Box<T> &Box<T>::operator=(const Box &other) {
391 if (this != &other) {
392 if (this->ptr) {
393 **this = *other;
394 } else {
395 this->uninit();
396 ::new (this->ptr) T(*other);
397 }
398 }
399 return *this;
400}
401
402template <typename T>
403Box<T> &Box<T>::operator=(Box &&other) noexcept {
404 if (this->ptr) {
405 this->drop();
406 }
407 this->ptr = other.ptr;
408 other.ptr = nullptr;
409 return *this;
410}
411
412template <typename T>
413const T *Box<T>::operator->() const noexcept {
414 return this->ptr;
415}
416
417template <typename T>
418const T &Box<T>::operator*() const noexcept {
419 return *this->ptr;
420}
421
422template <typename T>
423T *Box<T>::operator->() noexcept {
424 return this->ptr;
425}
426
427template <typename T>
428T &Box<T>::operator*() noexcept {
429 return *this->ptr;
430}
431
432template <typename T>
433template <typename... Fields>
434Box<T> Box<T>::in_place(Fields &&... fields) {
435 Box box;
436 box.uninit();
437 ::new (box.ptr) T{std::forward<Fields>(fields)...};
438 return box;
439}
440
441template <typename T>
442Box<T> Box<T>::from_raw(T *raw) noexcept {
443 Box box;
444 box.ptr = raw;
445 return box;
446}
447
448template <typename T>
449T *Box<T>::into_raw() noexcept {
450 T *raw = this->ptr;
451 this->ptr = nullptr;
452 return raw;
453}
454
455template <typename T>
456Box<T>::Box() noexcept {}
David Tolnay8f16ae72020-10-08 18:21:13 -0700457#endif // CXXBRIDGE05_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700458
David Tolnay8f16ae72020-10-08 18:21:13 -0700459#ifndef CXXBRIDGE05_RUST_VEC
460#define CXXBRIDGE05_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700461template <typename T>
462Vec<T>::Vec(Vec &&other) noexcept {
463 this->repr = other.repr;
464 new (&other) Vec();
465}
466
467template <typename T>
468Vec<T>::~Vec() noexcept {
469 this->drop();
470}
471
472template <typename T>
473Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
474 if (this != &other) {
475 this->drop();
476 this->repr = other.repr;
477 new (&other) Vec();
478 }
479 return *this;
480}
481
482template <typename T>
483bool Vec<T>::empty() const noexcept {
484 return size() == 0;
485}
486
487template <typename T>
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700488const T &Vec<T>::operator[](size_t n) const noexcept {
489 auto data = reinterpret_cast<const char *>(this->data());
490 return *reinterpret_cast<const T *>(data + n * this->stride());
491}
492
493template <typename T>
494const T &Vec<T>::at(size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700495 if (n >= this->size()) {
496 panic<std::out_of_range>("rust::Vec index out of range");
497 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700498 return (*this)[n];
499}
500
501template <typename T>
502const T &Vec<T>::front() const {
503 return (*this)[0];
504}
505
506template <typename T>
507const T &Vec<T>::back() const {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700508 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700509}
510
511template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700512const T &Vec<T>::const_iterator::operator*() const noexcept {
513 return *static_cast<const T *>(this->pos);
514}
515
516template <typename T>
517const T *Vec<T>::const_iterator::operator->() const noexcept {
518 return static_cast<const T *>(this->pos);
519}
520
521template <typename T>
522typename Vec<T>::const_iterator &Vec<T>::const_iterator::operator++() noexcept {
523 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
524 return *this;
525}
526
527template <typename T>
528typename Vec<T>::const_iterator
529Vec<T>::const_iterator::operator++(int) noexcept {
530 auto ret = const_iterator(*this);
531 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
532 return ret;
533}
534
535template <typename T>
David Tolnayb10c4bc2020-08-26 21:55:29 -0700536bool Vec<T>::const_iterator::operator==(
537 const const_iterator &other) const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700538 return this->pos == other.pos;
539}
540
541template <typename T>
David Tolnayb10c4bc2020-08-26 21:55:29 -0700542bool Vec<T>::const_iterator::operator!=(
543 const const_iterator &other) const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700544 return this->pos != other.pos;
545}
546
547template <typename T>
548typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
549 const_iterator it;
550 it.pos = this->data();
551 it.stride = this->stride();
552 return it;
553}
554
555template <typename T>
556typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
557 const_iterator it = this->begin();
558 it.pos = static_cast<const uint8_t *>(it.pos) + it.stride * this->size();
559 return it;
560}
561
562// Internal API only intended for the cxxbridge code generator.
563template <typename T>
564Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay8f16ae72020-10-08 18:21:13 -0700565#endif // CXXBRIDGE05_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700566
David Tolnay8f16ae72020-10-08 18:21:13 -0700567} // namespace cxxbridge05
David Tolnay750755e2020-03-01 13:04:08 -0800568} // namespace rust