blob: de614d10c18e221aa261506a77faf4152549b98e [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 Tolnay7db73692019-10-20 14:51:12 -040082 // Not necessarily ABI compatible with &str. Codegen will translate to
83 // cxx::rust_str::RustStr which matches this layout.
David Tolnay5df1f062020-10-31 12:31:10 -070084 const char *ptr;
85 size_t len;
David Tolnay7db73692019-10-20 14:51:12 -040086};
David Tolnay8f16ae72020-10-08 18:21:13 -070087#endif // CXXBRIDGE05_RUST_STR
David Tolnay7db73692019-10-20 14:51:12 -040088
David Tolnay8f16ae72020-10-08 18:21:13 -070089#ifndef CXXBRIDGE05_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -070090template <typename T>
91class Slice final {
92public:
David Tolnay2a2b9ad2020-05-12 20:07:26 -070093 Slice() noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -070094 Slice(const T *, size_t count) noexcept;
David Tolnayefe81052020-04-14 16:28:24 -070095
David Tolnay2d7f1172020-10-31 17:58:31 -070096 Slice &operator=(const Slice<T> &) noexcept = default;
David Tolnayefe81052020-04-14 16:28:24 -070097
David Tolnay2a2b9ad2020-05-12 20:07:26 -070098 const T *data() const noexcept;
99 size_t size() const noexcept;
100 size_t length() const noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700101
David Tolnayde9a5b12020-10-31 12:15:43 -0700102 // Important in order for System V ABI to pass in registers.
103 Slice(const Slice<T> &) noexcept = default;
104 ~Slice() noexcept = default;
105
David Tolnayefe81052020-04-14 16:28:24 -0700106 // Repr is PRIVATE; must not be used other than by our generated code.
107 //
108 // At present this class is only used for &[u8] slices.
109 // Not necessarily ABI compatible with &[u8]. Codegen will translate to
David Tolnaye710af12020-04-14 16:31:54 -0700110 // cxx::rust_sliceu8::RustSliceU8 which matches this layout.
David Tolnay48521222020-10-31 14:59:42 -0700111 struct Repr final {
David Tolnayefe81052020-04-14 16:28:24 -0700112 const T *ptr;
113 size_t len;
114 };
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700115 Slice(Repr) noexcept;
David Tolnaycedcde12020-10-31 11:47:14 -0700116 explicit operator Repr() const noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700117
118private:
119 Repr repr;
120};
David Tolnay8f16ae72020-10-08 18:21:13 -0700121#endif // CXXBRIDGE05_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -0700122
David Tolnay8f16ae72020-10-08 18:21:13 -0700123#ifndef CXXBRIDGE05_RUST_BOX
David Tolnayf262d382020-04-11 22:12:40 -0700124template <typename T>
125class Box final {
David Tolnay7db73692019-10-20 14:51:12 -0400126public:
David Tolnayf6292372020-03-01 21:09:11 -0800127 using value_type = T;
David Tolnay9706a512020-04-24 17:09:01 -0700128 using const_pointer =
129 typename std::add_pointer<typename std::add_const<T>::type>::type;
130 using pointer = typename std::add_pointer<T>::type;
David Tolnayf6292372020-03-01 21:09:11 -0800131
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700132 Box(const Box &);
133 Box(Box &&) noexcept;
134 ~Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400135
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700136 explicit Box(const T &);
137 explicit Box(T &&);
138
139 Box &operator=(const Box &);
140 Box &operator=(Box &&) noexcept;
141
142 const T *operator->() const noexcept;
143 const T &operator*() const noexcept;
144 T *operator->() noexcept;
145 T &operator*() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400146
David Tolnayf262d382020-04-11 22:12:40 -0700147 template <typename... Fields>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700148 static Box in_place(Fields &&...);
David Tolnay7ce59fc2020-04-11 11:46:33 -0700149
David Tolnay7db73692019-10-20 14:51:12 -0400150 // Important: requires that `raw` came from an into_raw call. Do not pass a
151 // pointer from `new` or any other source.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700152 static Box from_raw(T *) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400153
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700154 T *into_raw() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400155
156private:
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700157 Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400158 void uninit() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400159 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800160 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400161};
David Tolnay8f16ae72020-10-08 18:21:13 -0700162#endif // CXXBRIDGE05_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400163
David Tolnay8f16ae72020-10-08 18:21:13 -0700164#ifndef CXXBRIDGE05_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700165template <typename T>
166class Vec final {
167public:
David Tolnayc87c2152020-04-24 17:07:41 -0700168 using value_type = T;
169
David Tolnayf97c2d52020-04-25 16:37:48 -0700170 Vec() noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700171 Vec(Vec &&) noexcept;
172 ~Vec() noexcept;
David Tolnaycb800572020-04-24 20:30:43 -0700173
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700174 Vec &operator=(Vec &&) noexcept;
David Tolnayf97c2d52020-04-25 16:37:48 -0700175
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700176 size_t size() const noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700177 bool empty() const noexcept;
David Tolnay219c0792020-04-24 20:31:37 -0700178 const T *data() const noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700179
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700180 const T &operator[](size_t n) const noexcept;
181 const T &at(size_t n) const;
182
183 const T &front() const;
184 const T &back() const;
185
David Tolnay48521222020-10-31 14:59:42 -0700186 class const_iterator final {
David Tolnayc87c2152020-04-24 17:07:41 -0700187 public:
myronahnda9be502020-04-29 05:47:23 +0700188 using difference_type = ptrdiff_t;
David Tolnayc87c2152020-04-24 17:07:41 -0700189 using value_type = typename std::add_const<T>::type;
David Tolnay74dd3792020-04-30 07:45:24 -0700190 using pointer =
191 typename std::add_pointer<typename std::add_const<T>::type>::type;
David Tolnayc87c2152020-04-24 17:07:41 -0700192 using reference = typename std::add_lvalue_reference<
193 typename std::add_const<T>::type>::type;
myronahnda9be502020-04-29 05:47:23 +0700194 using iterator_category = std::forward_iterator_tag;
David Tolnayc87c2152020-04-24 17:07:41 -0700195
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700196 const T &operator*() const noexcept;
197 const T *operator->() const noexcept;
198 const_iterator &operator++() noexcept;
199 const_iterator operator++(int) noexcept;
200 bool operator==(const const_iterator &) const noexcept;
201 bool operator!=(const const_iterator &) const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700202
203 private:
204 friend class Vec;
205 const void *pos;
206 size_t stride;
207 };
208
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700209 const_iterator begin() const noexcept;
210 const_iterator end() const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700211
David Tolnay313b10e2020-04-25 16:30:51 -0700212 // Internal API only intended for the cxxbridge code generator.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700213 Vec(unsafe_bitcopy_t, const Vec &) noexcept;
David Tolnay313b10e2020-04-25 16:30:51 -0700214
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700215private:
David Tolnay503d0192020-04-24 22:18:56 -0700216 static size_t stride() noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700217 void drop() noexcept;
218
219 // Size and alignment statically verified by rust_vec.rs.
220 std::array<uintptr_t, 3> repr;
221};
David Tolnay8f16ae72020-10-08 18:21:13 -0700222#endif // CXXBRIDGE05_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700223
David Tolnay8f16ae72020-10-08 18:21:13 -0700224#ifndef CXXBRIDGE05_RUST_FN
225#define CXXBRIDGE05_RUST_FN
David Tolnayf262d382020-04-11 22:12:40 -0700226template <typename Signature, bool Throws = false>
227class Fn;
David Tolnay75dca2e2020-03-25 20:17:52 -0700228
229template <typename Ret, typename... Args, bool Throws>
David Tolnay48521222020-10-31 14:59:42 -0700230class Fn<Ret(Args...), Throws> final {
David Tolnay75dca2e2020-03-25 20:17:52 -0700231public:
David Tolnay533d4582020-04-08 20:29:14 -0700232 Ret operator()(Args... args) const noexcept(!Throws);
233 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700234
235private:
236 Ret (*trampoline)(Args..., void *fn) noexcept(!Throws);
237 void *fn;
238};
239
David Tolnayf262d382020-04-11 22:12:40 -0700240template <typename Signature>
241using TryFn = Fn<Signature, true>;
David Tolnay8f16ae72020-10-08 18:21:13 -0700242#endif // CXXBRIDGE05_RUST_FN
David Tolnay75dca2e2020-03-25 20:17:52 -0700243
David Tolnay8f16ae72020-10-08 18:21:13 -0700244#ifndef CXXBRIDGE05_RUST_ERROR
245#define CXXBRIDGE05_RUST_ERROR
David Tolnaye4fa8732020-09-08 15:04:56 -0700246class Error final : public std::exception {
David Tolnay1e548172020-03-16 13:37:09 -0700247public:
248 Error(const Error &);
249 Error(Error &&) noexcept;
David Tolnay1e548172020-03-16 13:37:09 -0700250 ~Error() noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700251
252 Error &operator=(const Error &);
David Tolnay15491062020-10-31 17:25:13 -0700253 Error &operator=(Error &&) noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700254
David Tolnay1e548172020-03-16 13:37:09 -0700255 const char *what() const noexcept override;
256
257private:
David Tolnaya0c9bc72020-10-31 14:37:14 -0700258 Error() noexcept = default;
David Tolnay84ddf9e2020-10-31 15:36:48 -0700259 friend impl<Error>;
David Tolnaya0c9bc72020-10-31 14:37:14 -0700260 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 Tolnay5b1ee1f2020-10-31 18:21:39 -0700322#ifndef CXXBRIDGE05_RUST_STR
323#define CXXBRIDGE05_RUST_STR
324inline const char *Str::data() const noexcept { return this->ptr; }
325
326inline size_t Str::size() const noexcept { return this->len; }
327
328inline size_t Str::length() const noexcept { return this->len; }
329#endif // CXXBRIDGE05_RUST_STR
330
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>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700340const T *Slice<T>::data() const noexcept {
341 return this->repr.ptr;
342}
343
344template <typename T>
345size_t Slice<T>::size() const noexcept {
346 return this->repr.len;
347}
348
349template <typename T>
350size_t Slice<T>::length() const noexcept {
351 return this->repr.len;
352}
353
354template <typename T>
355Slice<T>::Slice(Repr repr_) noexcept : repr(repr_) {}
356
357template <typename T>
David Tolnaycedcde12020-10-31 11:47:14 -0700358Slice<T>::operator Repr() const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700359 return this->repr;
360}
David Tolnay8f16ae72020-10-08 18:21:13 -0700361#endif // CXXBRIDGE05_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700362
David Tolnay8f16ae72020-10-08 18:21:13 -0700363#ifndef CXXBRIDGE05_RUST_BOX
364#define CXXBRIDGE05_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700365template <typename T>
366Box<T>::Box(const Box &other) : Box(*other) {}
367
368template <typename T>
369Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
370 other.ptr = nullptr;
371}
372
373template <typename T>
374Box<T>::Box(const T &val) {
375 this->uninit();
376 ::new (this->ptr) T(val);
377}
378
379template <typename T>
380Box<T>::Box(T &&val) {
381 this->uninit();
382 ::new (this->ptr) T(std::move(val));
383}
384
385template <typename T>
386Box<T>::~Box() noexcept {
387 if (this->ptr) {
388 this->drop();
389 }
390}
391
392template <typename T>
393Box<T> &Box<T>::operator=(const Box &other) {
394 if (this != &other) {
395 if (this->ptr) {
396 **this = *other;
397 } else {
398 this->uninit();
399 ::new (this->ptr) T(*other);
400 }
401 }
402 return *this;
403}
404
405template <typename T>
406Box<T> &Box<T>::operator=(Box &&other) noexcept {
407 if (this->ptr) {
408 this->drop();
409 }
410 this->ptr = other.ptr;
411 other.ptr = nullptr;
412 return *this;
413}
414
415template <typename T>
416const T *Box<T>::operator->() const noexcept {
417 return this->ptr;
418}
419
420template <typename T>
421const T &Box<T>::operator*() const noexcept {
422 return *this->ptr;
423}
424
425template <typename T>
426T *Box<T>::operator->() noexcept {
427 return this->ptr;
428}
429
430template <typename T>
431T &Box<T>::operator*() noexcept {
432 return *this->ptr;
433}
434
435template <typename T>
436template <typename... Fields>
437Box<T> Box<T>::in_place(Fields &&... fields) {
438 Box box;
439 box.uninit();
440 ::new (box.ptr) T{std::forward<Fields>(fields)...};
441 return box;
442}
443
444template <typename T>
445Box<T> Box<T>::from_raw(T *raw) noexcept {
446 Box box;
447 box.ptr = raw;
448 return box;
449}
450
451template <typename T>
452T *Box<T>::into_raw() noexcept {
453 T *raw = this->ptr;
454 this->ptr = nullptr;
455 return raw;
456}
457
458template <typename T>
459Box<T>::Box() noexcept {}
David Tolnay8f16ae72020-10-08 18:21:13 -0700460#endif // CXXBRIDGE05_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700461
David Tolnay8f16ae72020-10-08 18:21:13 -0700462#ifndef CXXBRIDGE05_RUST_VEC
463#define CXXBRIDGE05_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700464template <typename T>
465Vec<T>::Vec(Vec &&other) noexcept {
466 this->repr = other.repr;
467 new (&other) Vec();
468}
469
470template <typename T>
471Vec<T>::~Vec() noexcept {
472 this->drop();
473}
474
475template <typename T>
476Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
477 if (this != &other) {
478 this->drop();
479 this->repr = other.repr;
480 new (&other) Vec();
481 }
482 return *this;
483}
484
485template <typename T>
486bool Vec<T>::empty() const noexcept {
487 return size() == 0;
488}
489
490template <typename T>
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700491const T &Vec<T>::operator[](size_t n) const noexcept {
492 auto data = reinterpret_cast<const char *>(this->data());
493 return *reinterpret_cast<const T *>(data + n * this->stride());
494}
495
496template <typename T>
497const T &Vec<T>::at(size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700498 if (n >= this->size()) {
499 panic<std::out_of_range>("rust::Vec index out of range");
500 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700501 return (*this)[n];
502}
503
504template <typename T>
505const T &Vec<T>::front() const {
506 return (*this)[0];
507}
508
509template <typename T>
510const T &Vec<T>::back() const {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700511 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700512}
513
514template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700515const T &Vec<T>::const_iterator::operator*() const noexcept {
516 return *static_cast<const T *>(this->pos);
517}
518
519template <typename T>
520const T *Vec<T>::const_iterator::operator->() const noexcept {
521 return static_cast<const T *>(this->pos);
522}
523
524template <typename T>
525typename Vec<T>::const_iterator &Vec<T>::const_iterator::operator++() noexcept {
526 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
527 return *this;
528}
529
530template <typename T>
531typename Vec<T>::const_iterator
532Vec<T>::const_iterator::operator++(int) noexcept {
533 auto ret = const_iterator(*this);
534 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
535 return ret;
536}
537
538template <typename T>
David Tolnayb10c4bc2020-08-26 21:55:29 -0700539bool Vec<T>::const_iterator::operator==(
540 const const_iterator &other) const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700541 return this->pos == other.pos;
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>
551typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
552 const_iterator it;
553 it.pos = this->data();
554 it.stride = this->stride();
555 return it;
556}
557
558template <typename T>
559typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
560 const_iterator it = this->begin();
561 it.pos = static_cast<const uint8_t *>(it.pos) + it.stride * this->size();
562 return it;
563}
564
565// Internal API only intended for the cxxbridge code generator.
566template <typename T>
567Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay8f16ae72020-10-08 18:21:13 -0700568#endif // CXXBRIDGE05_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700569
David Tolnay8f16ae72020-10-08 18:21:13 -0700570} // namespace cxxbridge05
David Tolnay750755e2020-03-01 13:04:08 -0800571} // namespace rust