blob: c545e500ad2e973fc6499ca882890a7daf66641e [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 &);
David Tolnay15491062020-10-31 17:25:13 -0700262 Error &operator=(Error &&) noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700263
David Tolnay1e548172020-03-16 13:37:09 -0700264 const char *what() const noexcept override;
265
266private:
David Tolnaya0c9bc72020-10-31 14:37:14 -0700267 Error() noexcept = default;
David Tolnay84ddf9e2020-10-31 15:36:48 -0700268 friend impl<Error>;
David Tolnaya0c9bc72020-10-31 14:37:14 -0700269 const char *msg;
270 size_t len;
David Tolnay1e548172020-03-16 13:37:09 -0700271};
David Tolnay8f16ae72020-10-08 18:21:13 -0700272#endif // CXXBRIDGE05_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700273
David Tolnay8f16ae72020-10-08 18:21:13 -0700274#ifndef CXXBRIDGE05_RUST_ISIZE
275#define CXXBRIDGE05_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700276#if defined(_WIN32)
277using isize = SSIZE_T;
278#else
279using isize = ssize_t;
280#endif
David Tolnay8f16ae72020-10-08 18:21:13 -0700281#endif // CXXBRIDGE05_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700282
David Tolnay851677c2020-03-01 23:49:46 -0800283std::ostream &operator<<(std::ostream &, const String &);
284std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400285
David Tolnay3b0c9882020-03-01 14:08:57 -0800286// Snake case aliases for use in code that uses this style for type names.
287using string = String;
288using str = Str;
David Tolnayf262d382020-04-11 22:12:40 -0700289template <class T>
David Tolnay38c87642020-09-06 22:18:08 -0700290using slice = Slice<T>;
291template <class T>
David Tolnayf262d382020-04-11 22:12:40 -0700292using box = Box<T>;
David Tolnay38c87642020-09-06 22:18:08 -0700293template <class T>
294using vec = Vec<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700295using error = Error;
David Tolnay75dca2e2020-03-25 20:17:52 -0700296template <typename Signature, bool Throws = false>
297using fn = Fn<Signature, Throws>;
David Tolnayf262d382020-04-11 22:12:40 -0700298template <typename Signature>
299using try_fn = TryFn<Signature>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800300
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700301
302
303////////////////////////////////////////////////////////////////////////////////
304/// end public API, begin implementation details
305
David Tolnay8f16ae72020-10-08 18:21:13 -0700306#ifndef CXXBRIDGE05_PANIC
307#define CXXBRIDGE05_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700308template <typename Exception>
309void panic [[noreturn]] (const char *msg);
David Tolnay8f16ae72020-10-08 18:21:13 -0700310#endif // CXXBRIDGE05_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700311
David Tolnay75dca2e2020-03-25 20:17:52 -0700312template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700313Ret Fn<Ret(Args...), Throws>::operator()(Args... args) const noexcept(!Throws) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700314 return (*this->trampoline)(std::move(args)..., this->fn);
315}
316
David Tolnaya23129c2020-04-08 20:08:21 -0700317template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700318Fn<Ret(Args...), Throws> Fn<Ret(Args...), Throws>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700319 return *this;
320}
321
David Tolnay8f16ae72020-10-08 18:21:13 -0700322#ifndef CXXBRIDGE05_RUST_BITCOPY
323#define CXXBRIDGE05_RUST_BITCOPY
David Tolnay48521222020-10-31 14:59:42 -0700324struct unsafe_bitcopy_t final {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700325 explicit unsafe_bitcopy_t() = default;
326};
327
328constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnay8f16ae72020-10-08 18:21:13 -0700329#endif // CXXBRIDGE05_RUST_BITCOPY
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700330
David Tolnay8f16ae72020-10-08 18:21:13 -0700331#ifndef CXXBRIDGE05_RUST_SLICE
332#define CXXBRIDGE05_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700333template <typename T>
334Slice<T>::Slice() noexcept : repr(Repr{reinterpret_cast<const T *>(this), 0}) {}
335
336template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700337Slice<T>::Slice(const T *s, size_t count) noexcept : repr(Repr{s, count}) {}
338
339template <typename T>
340Slice<T> &Slice<T>::operator=(Slice<T> other) noexcept {
341 this->repr = other.repr;
342 return *this;
343}
344
345template <typename T>
346const T *Slice<T>::data() const noexcept {
347 return this->repr.ptr;
348}
349
350template <typename T>
351size_t Slice<T>::size() const noexcept {
352 return this->repr.len;
353}
354
355template <typename T>
356size_t Slice<T>::length() const noexcept {
357 return this->repr.len;
358}
359
360template <typename T>
361Slice<T>::Slice(Repr repr_) noexcept : repr(repr_) {}
362
363template <typename T>
David Tolnaycedcde12020-10-31 11:47:14 -0700364Slice<T>::operator Repr() const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700365 return this->repr;
366}
David Tolnay8f16ae72020-10-08 18:21:13 -0700367#endif // CXXBRIDGE05_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700368
David Tolnay8f16ae72020-10-08 18:21:13 -0700369#ifndef CXXBRIDGE05_RUST_BOX
370#define CXXBRIDGE05_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700371template <typename T>
372Box<T>::Box(const Box &other) : Box(*other) {}
373
374template <typename T>
375Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
376 other.ptr = nullptr;
377}
378
379template <typename T>
380Box<T>::Box(const T &val) {
381 this->uninit();
382 ::new (this->ptr) T(val);
383}
384
385template <typename T>
386Box<T>::Box(T &&val) {
387 this->uninit();
388 ::new (this->ptr) T(std::move(val));
389}
390
391template <typename T>
392Box<T>::~Box() noexcept {
393 if (this->ptr) {
394 this->drop();
395 }
396}
397
398template <typename T>
399Box<T> &Box<T>::operator=(const Box &other) {
400 if (this != &other) {
401 if (this->ptr) {
402 **this = *other;
403 } else {
404 this->uninit();
405 ::new (this->ptr) T(*other);
406 }
407 }
408 return *this;
409}
410
411template <typename T>
412Box<T> &Box<T>::operator=(Box &&other) noexcept {
413 if (this->ptr) {
414 this->drop();
415 }
416 this->ptr = other.ptr;
417 other.ptr = nullptr;
418 return *this;
419}
420
421template <typename T>
422const T *Box<T>::operator->() const noexcept {
423 return this->ptr;
424}
425
426template <typename T>
427const T &Box<T>::operator*() const noexcept {
428 return *this->ptr;
429}
430
431template <typename T>
432T *Box<T>::operator->() noexcept {
433 return this->ptr;
434}
435
436template <typename T>
437T &Box<T>::operator*() noexcept {
438 return *this->ptr;
439}
440
441template <typename T>
442template <typename... Fields>
443Box<T> Box<T>::in_place(Fields &&... fields) {
444 Box box;
445 box.uninit();
446 ::new (box.ptr) T{std::forward<Fields>(fields)...};
447 return box;
448}
449
450template <typename T>
451Box<T> Box<T>::from_raw(T *raw) noexcept {
452 Box box;
453 box.ptr = raw;
454 return box;
455}
456
457template <typename T>
458T *Box<T>::into_raw() noexcept {
459 T *raw = this->ptr;
460 this->ptr = nullptr;
461 return raw;
462}
463
464template <typename T>
465Box<T>::Box() noexcept {}
David Tolnay8f16ae72020-10-08 18:21:13 -0700466#endif // CXXBRIDGE05_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700467
David Tolnay8f16ae72020-10-08 18:21:13 -0700468#ifndef CXXBRIDGE05_RUST_VEC
469#define CXXBRIDGE05_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700470template <typename T>
471Vec<T>::Vec(Vec &&other) noexcept {
472 this->repr = other.repr;
473 new (&other) Vec();
474}
475
476template <typename T>
477Vec<T>::~Vec() noexcept {
478 this->drop();
479}
480
481template <typename T>
482Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
483 if (this != &other) {
484 this->drop();
485 this->repr = other.repr;
486 new (&other) Vec();
487 }
488 return *this;
489}
490
491template <typename T>
492bool Vec<T>::empty() const noexcept {
493 return size() == 0;
494}
495
496template <typename T>
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700497const T &Vec<T>::operator[](size_t n) const noexcept {
498 auto data = reinterpret_cast<const char *>(this->data());
499 return *reinterpret_cast<const T *>(data + n * this->stride());
500}
501
502template <typename T>
503const T &Vec<T>::at(size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700504 if (n >= this->size()) {
505 panic<std::out_of_range>("rust::Vec index out of range");
506 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700507 return (*this)[n];
508}
509
510template <typename T>
511const T &Vec<T>::front() const {
512 return (*this)[0];
513}
514
515template <typename T>
516const T &Vec<T>::back() const {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700517 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700518}
519
520template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700521const T &Vec<T>::const_iterator::operator*() const noexcept {
522 return *static_cast<const T *>(this->pos);
523}
524
525template <typename T>
526const T *Vec<T>::const_iterator::operator->() const noexcept {
527 return static_cast<const T *>(this->pos);
528}
529
530template <typename T>
531typename Vec<T>::const_iterator &Vec<T>::const_iterator::operator++() noexcept {
532 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
533 return *this;
534}
535
536template <typename T>
537typename Vec<T>::const_iterator
538Vec<T>::const_iterator::operator++(int) noexcept {
539 auto ret = const_iterator(*this);
540 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
541 return ret;
542}
543
544template <typename T>
David Tolnayb10c4bc2020-08-26 21:55:29 -0700545bool Vec<T>::const_iterator::operator==(
546 const const_iterator &other) const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700547 return this->pos == other.pos;
548}
549
550template <typename T>
David Tolnayb10c4bc2020-08-26 21:55:29 -0700551bool Vec<T>::const_iterator::operator!=(
552 const const_iterator &other) const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700553 return this->pos != other.pos;
554}
555
556template <typename T>
557typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
558 const_iterator it;
559 it.pos = this->data();
560 it.stride = this->stride();
561 return it;
562}
563
564template <typename T>
565typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
566 const_iterator it = this->begin();
567 it.pos = static_cast<const uint8_t *>(it.pos) + it.stride * this->size();
568 return it;
569}
570
571// Internal API only intended for the cxxbridge code generator.
572template <typename T>
573Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay8f16ae72020-10-08 18:21:13 -0700574#endif // CXXBRIDGE05_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700575
David Tolnay8f16ae72020-10-08 18:21:13 -0700576} // namespace cxxbridge05
David Tolnay750755e2020-03-01 13:04:08 -0800577} // namespace rust