blob: 3499fecaba7f9ec9f472aebb9cfaa050d472de30 [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
David Tolnay2d7f1172020-10-31 17:58:31 -070069 Str &operator=(const Str &) noexcept = default;
David Tolnayd9c4ac92020-03-01 20:33:58 -080070
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 Tolnay5df1f062020-10-31 12:31:10 -070082private:
David Tolnay7db73692019-10-20 14:51:12 -040083 // Not necessarily ABI compatible with &str. Codegen will translate to
84 // cxx::rust_str::RustStr which matches this layout.
David Tolnay5df1f062020-10-31 12:31:10 -070085 const char *ptr;
86 size_t len;
David Tolnay7db73692019-10-20 14:51:12 -040087};
David Tolnay8f16ae72020-10-08 18:21:13 -070088#endif // CXXBRIDGE05_RUST_STR
David Tolnay7db73692019-10-20 14:51:12 -040089
David Tolnay8f16ae72020-10-08 18:21:13 -070090#ifndef CXXBRIDGE05_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -070091template <typename T>
92class Slice final {
93public:
David Tolnay2a2b9ad2020-05-12 20:07:26 -070094 Slice() noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -070095 Slice(const T *, size_t count) noexcept;
David Tolnayefe81052020-04-14 16:28:24 -070096
David Tolnay2d7f1172020-10-31 17:58:31 -070097 Slice &operator=(const Slice<T> &) noexcept = default;
David Tolnayefe81052020-04-14 16:28:24 -070098
David Tolnay2a2b9ad2020-05-12 20:07:26 -070099 const T *data() const noexcept;
100 size_t size() const noexcept;
101 size_t length() const noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700102
David Tolnayde9a5b12020-10-31 12:15:43 -0700103 // Important in order for System V ABI to pass in registers.
104 Slice(const Slice<T> &) noexcept = default;
105 ~Slice() noexcept = default;
106
David Tolnayefe81052020-04-14 16:28:24 -0700107 // Repr is PRIVATE; must not be used other than by our generated code.
108 //
109 // At present this class is only used for &[u8] slices.
110 // Not necessarily ABI compatible with &[u8]. Codegen will translate to
David Tolnaye710af12020-04-14 16:31:54 -0700111 // cxx::rust_sliceu8::RustSliceU8 which matches this layout.
David Tolnay48521222020-10-31 14:59:42 -0700112 struct Repr final {
David Tolnayefe81052020-04-14 16:28:24 -0700113 const T *ptr;
114 size_t len;
115 };
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700116 Slice(Repr) noexcept;
David Tolnaycedcde12020-10-31 11:47:14 -0700117 explicit operator Repr() const noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700118
119private:
120 Repr repr;
121};
David Tolnay8f16ae72020-10-08 18:21:13 -0700122#endif // CXXBRIDGE05_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -0700123
David Tolnay8f16ae72020-10-08 18:21:13 -0700124#ifndef CXXBRIDGE05_RUST_BOX
David Tolnayf262d382020-04-11 22:12:40 -0700125template <typename T>
126class Box final {
David Tolnay7db73692019-10-20 14:51:12 -0400127public:
David Tolnayf6292372020-03-01 21:09:11 -0800128 using value_type = T;
David Tolnay9706a512020-04-24 17:09:01 -0700129 using const_pointer =
130 typename std::add_pointer<typename std::add_const<T>::type>::type;
131 using pointer = typename std::add_pointer<T>::type;
David Tolnayf6292372020-03-01 21:09:11 -0800132
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700133 Box(const Box &);
134 Box(Box &&) noexcept;
135 ~Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400136
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700137 explicit Box(const T &);
138 explicit Box(T &&);
139
140 Box &operator=(const Box &);
141 Box &operator=(Box &&) noexcept;
142
143 const T *operator->() const noexcept;
144 const T &operator*() const noexcept;
145 T *operator->() noexcept;
146 T &operator*() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400147
David Tolnayf262d382020-04-11 22:12:40 -0700148 template <typename... Fields>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700149 static Box in_place(Fields &&...);
David Tolnay7ce59fc2020-04-11 11:46:33 -0700150
David Tolnay7db73692019-10-20 14:51:12 -0400151 // Important: requires that `raw` came from an into_raw call. Do not pass a
152 // pointer from `new` or any other source.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700153 static Box from_raw(T *) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400154
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700155 T *into_raw() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400156
157private:
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700158 Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400159 void uninit() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400160 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800161 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400162};
David Tolnay8f16ae72020-10-08 18:21:13 -0700163#endif // CXXBRIDGE05_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400164
David Tolnay8f16ae72020-10-08 18:21:13 -0700165#ifndef CXXBRIDGE05_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700166template <typename T>
167class Vec final {
168public:
David Tolnayc87c2152020-04-24 17:07:41 -0700169 using value_type = T;
170
David Tolnayf97c2d52020-04-25 16:37:48 -0700171 Vec() noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700172 Vec(Vec &&) noexcept;
173 ~Vec() noexcept;
David Tolnaycb800572020-04-24 20:30:43 -0700174
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700175 Vec &operator=(Vec &&) noexcept;
David Tolnayf97c2d52020-04-25 16:37:48 -0700176
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700177 size_t size() const noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700178 bool empty() const noexcept;
David Tolnay219c0792020-04-24 20:31:37 -0700179 const T *data() const noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700180
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700181 const T &operator[](size_t n) const noexcept;
182 const T &at(size_t n) const;
183
184 const T &front() const;
185 const T &back() const;
186
David Tolnay48521222020-10-31 14:59:42 -0700187 class const_iterator final {
David Tolnayc87c2152020-04-24 17:07:41 -0700188 public:
myronahnda9be502020-04-29 05:47:23 +0700189 using difference_type = ptrdiff_t;
David Tolnayc87c2152020-04-24 17:07:41 -0700190 using value_type = typename std::add_const<T>::type;
David Tolnay74dd3792020-04-30 07:45:24 -0700191 using pointer =
192 typename std::add_pointer<typename std::add_const<T>::type>::type;
David Tolnayc87c2152020-04-24 17:07:41 -0700193 using reference = typename std::add_lvalue_reference<
194 typename std::add_const<T>::type>::type;
myronahnda9be502020-04-29 05:47:23 +0700195 using iterator_category = std::forward_iterator_tag;
David Tolnayc87c2152020-04-24 17:07:41 -0700196
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700197 const T &operator*() const noexcept;
198 const T *operator->() const noexcept;
199 const_iterator &operator++() noexcept;
200 const_iterator operator++(int) noexcept;
201 bool operator==(const const_iterator &) const noexcept;
202 bool operator!=(const const_iterator &) const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700203
204 private:
205 friend class Vec;
206 const void *pos;
207 size_t stride;
208 };
209
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700210 const_iterator begin() const noexcept;
211 const_iterator end() const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700212
David Tolnay313b10e2020-04-25 16:30:51 -0700213 // Internal API only intended for the cxxbridge code generator.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700214 Vec(unsafe_bitcopy_t, const Vec &) noexcept;
David Tolnay313b10e2020-04-25 16:30:51 -0700215
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700216private:
David Tolnay503d0192020-04-24 22:18:56 -0700217 static size_t stride() noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700218 void drop() noexcept;
219
220 // Size and alignment statically verified by rust_vec.rs.
221 std::array<uintptr_t, 3> repr;
222};
David Tolnay8f16ae72020-10-08 18:21:13 -0700223#endif // CXXBRIDGE05_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700224
David Tolnay8f16ae72020-10-08 18:21:13 -0700225#ifndef CXXBRIDGE05_RUST_FN
226#define CXXBRIDGE05_RUST_FN
David Tolnayf262d382020-04-11 22:12:40 -0700227template <typename Signature, bool Throws = false>
228class Fn;
David Tolnay75dca2e2020-03-25 20:17:52 -0700229
230template <typename Ret, typename... Args, bool Throws>
David Tolnay48521222020-10-31 14:59:42 -0700231class Fn<Ret(Args...), Throws> final {
David Tolnay75dca2e2020-03-25 20:17:52 -0700232public:
David Tolnay533d4582020-04-08 20:29:14 -0700233 Ret operator()(Args... args) const noexcept(!Throws);
234 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700235
236private:
237 Ret (*trampoline)(Args..., void *fn) noexcept(!Throws);
238 void *fn;
239};
240
David Tolnayf262d382020-04-11 22:12:40 -0700241template <typename Signature>
242using TryFn = Fn<Signature, true>;
David Tolnay8f16ae72020-10-08 18:21:13 -0700243#endif // CXXBRIDGE05_RUST_FN
David Tolnay75dca2e2020-03-25 20:17:52 -0700244
David Tolnay8f16ae72020-10-08 18:21:13 -0700245#ifndef CXXBRIDGE05_RUST_ERROR
246#define CXXBRIDGE05_RUST_ERROR
David Tolnaye4fa8732020-09-08 15:04:56 -0700247class Error final : public std::exception {
David Tolnay1e548172020-03-16 13:37:09 -0700248public:
249 Error(const Error &);
250 Error(Error &&) noexcept;
David Tolnay1e548172020-03-16 13:37:09 -0700251 ~Error() noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700252
253 Error &operator=(const Error &);
David Tolnay15491062020-10-31 17:25:13 -0700254 Error &operator=(Error &&) noexcept;
David Tolnay7c6ac712020-10-31 17:22:28 -0700255
David Tolnay1e548172020-03-16 13:37:09 -0700256 const char *what() const noexcept override;
257
258private:
David Tolnaya0c9bc72020-10-31 14:37:14 -0700259 Error() noexcept = default;
David Tolnay84ddf9e2020-10-31 15:36:48 -0700260 friend impl<Error>;
David Tolnaya0c9bc72020-10-31 14:37:14 -0700261 const char *msg;
262 size_t len;
David Tolnay1e548172020-03-16 13:37:09 -0700263};
David Tolnay8f16ae72020-10-08 18:21:13 -0700264#endif // CXXBRIDGE05_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700265
David Tolnay8f16ae72020-10-08 18:21:13 -0700266#ifndef CXXBRIDGE05_RUST_ISIZE
267#define CXXBRIDGE05_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700268#if defined(_WIN32)
269using isize = SSIZE_T;
270#else
271using isize = ssize_t;
272#endif
David Tolnay8f16ae72020-10-08 18:21:13 -0700273#endif // CXXBRIDGE05_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700274
David Tolnay851677c2020-03-01 23:49:46 -0800275std::ostream &operator<<(std::ostream &, const String &);
276std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400277
David Tolnay3b0c9882020-03-01 14:08:57 -0800278// Snake case aliases for use in code that uses this style for type names.
279using string = String;
280using str = Str;
David Tolnayf262d382020-04-11 22:12:40 -0700281template <class T>
David Tolnay38c87642020-09-06 22:18:08 -0700282using slice = Slice<T>;
283template <class T>
David Tolnayf262d382020-04-11 22:12:40 -0700284using box = Box<T>;
David Tolnay38c87642020-09-06 22:18:08 -0700285template <class T>
286using vec = Vec<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700287using error = Error;
David Tolnay75dca2e2020-03-25 20:17:52 -0700288template <typename Signature, bool Throws = false>
289using fn = Fn<Signature, Throws>;
David Tolnayf262d382020-04-11 22:12:40 -0700290template <typename Signature>
291using try_fn = TryFn<Signature>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800292
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700293
294
295////////////////////////////////////////////////////////////////////////////////
296/// end public API, begin implementation details
297
David Tolnay8f16ae72020-10-08 18:21:13 -0700298#ifndef CXXBRIDGE05_PANIC
299#define CXXBRIDGE05_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700300template <typename Exception>
301void panic [[noreturn]] (const char *msg);
David Tolnay8f16ae72020-10-08 18:21:13 -0700302#endif // CXXBRIDGE05_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700303
David Tolnay75dca2e2020-03-25 20:17:52 -0700304template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700305Ret Fn<Ret(Args...), Throws>::operator()(Args... args) const noexcept(!Throws) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700306 return (*this->trampoline)(std::move(args)..., this->fn);
307}
308
David Tolnaya23129c2020-04-08 20:08:21 -0700309template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700310Fn<Ret(Args...), Throws> Fn<Ret(Args...), Throws>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700311 return *this;
312}
313
David Tolnay8f16ae72020-10-08 18:21:13 -0700314#ifndef CXXBRIDGE05_RUST_BITCOPY
315#define CXXBRIDGE05_RUST_BITCOPY
David Tolnay48521222020-10-31 14:59:42 -0700316struct unsafe_bitcopy_t final {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700317 explicit unsafe_bitcopy_t() = default;
318};
319
320constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnay8f16ae72020-10-08 18:21:13 -0700321#endif // CXXBRIDGE05_RUST_BITCOPY
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700322
David Tolnay8f16ae72020-10-08 18:21:13 -0700323#ifndef CXXBRIDGE05_RUST_SLICE
324#define CXXBRIDGE05_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700325template <typename T>
326Slice<T>::Slice() noexcept : repr(Repr{reinterpret_cast<const T *>(this), 0}) {}
327
328template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700329Slice<T>::Slice(const T *s, size_t count) noexcept : repr(Repr{s, count}) {}
330
331template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700332const T *Slice<T>::data() const noexcept {
333 return this->repr.ptr;
334}
335
336template <typename T>
337size_t Slice<T>::size() const noexcept {
338 return this->repr.len;
339}
340
341template <typename T>
342size_t Slice<T>::length() const noexcept {
343 return this->repr.len;
344}
345
346template <typename T>
347Slice<T>::Slice(Repr repr_) noexcept : repr(repr_) {}
348
349template <typename T>
David Tolnaycedcde12020-10-31 11:47:14 -0700350Slice<T>::operator Repr() const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700351 return this->repr;
352}
David Tolnay8f16ae72020-10-08 18:21:13 -0700353#endif // CXXBRIDGE05_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700354
David Tolnay8f16ae72020-10-08 18:21:13 -0700355#ifndef CXXBRIDGE05_RUST_BOX
356#define CXXBRIDGE05_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700357template <typename T>
358Box<T>::Box(const Box &other) : Box(*other) {}
359
360template <typename T>
361Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
362 other.ptr = nullptr;
363}
364
365template <typename T>
366Box<T>::Box(const T &val) {
367 this->uninit();
368 ::new (this->ptr) T(val);
369}
370
371template <typename T>
372Box<T>::Box(T &&val) {
373 this->uninit();
374 ::new (this->ptr) T(std::move(val));
375}
376
377template <typename T>
378Box<T>::~Box() noexcept {
379 if (this->ptr) {
380 this->drop();
381 }
382}
383
384template <typename T>
385Box<T> &Box<T>::operator=(const Box &other) {
386 if (this != &other) {
387 if (this->ptr) {
388 **this = *other;
389 } else {
390 this->uninit();
391 ::new (this->ptr) T(*other);
392 }
393 }
394 return *this;
395}
396
397template <typename T>
398Box<T> &Box<T>::operator=(Box &&other) noexcept {
399 if (this->ptr) {
400 this->drop();
401 }
402 this->ptr = other.ptr;
403 other.ptr = nullptr;
404 return *this;
405}
406
407template <typename T>
408const T *Box<T>::operator->() const noexcept {
409 return this->ptr;
410}
411
412template <typename T>
413const T &Box<T>::operator*() const noexcept {
414 return *this->ptr;
415}
416
417template <typename T>
418T *Box<T>::operator->() noexcept {
419 return this->ptr;
420}
421
422template <typename T>
423T &Box<T>::operator*() noexcept {
424 return *this->ptr;
425}
426
427template <typename T>
428template <typename... Fields>
429Box<T> Box<T>::in_place(Fields &&... fields) {
430 Box box;
431 box.uninit();
432 ::new (box.ptr) T{std::forward<Fields>(fields)...};
433 return box;
434}
435
436template <typename T>
437Box<T> Box<T>::from_raw(T *raw) noexcept {
438 Box box;
439 box.ptr = raw;
440 return box;
441}
442
443template <typename T>
444T *Box<T>::into_raw() noexcept {
445 T *raw = this->ptr;
446 this->ptr = nullptr;
447 return raw;
448}
449
450template <typename T>
451Box<T>::Box() noexcept {}
David Tolnay8f16ae72020-10-08 18:21:13 -0700452#endif // CXXBRIDGE05_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700453
David Tolnay8f16ae72020-10-08 18:21:13 -0700454#ifndef CXXBRIDGE05_RUST_VEC
455#define CXXBRIDGE05_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700456template <typename T>
457Vec<T>::Vec(Vec &&other) noexcept {
458 this->repr = other.repr;
459 new (&other) Vec();
460}
461
462template <typename T>
463Vec<T>::~Vec() noexcept {
464 this->drop();
465}
466
467template <typename T>
468Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
469 if (this != &other) {
470 this->drop();
471 this->repr = other.repr;
472 new (&other) Vec();
473 }
474 return *this;
475}
476
477template <typename T>
478bool Vec<T>::empty() const noexcept {
479 return size() == 0;
480}
481
482template <typename T>
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700483const T &Vec<T>::operator[](size_t n) const noexcept {
484 auto data = reinterpret_cast<const char *>(this->data());
485 return *reinterpret_cast<const T *>(data + n * this->stride());
486}
487
488template <typename T>
489const T &Vec<T>::at(size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700490 if (n >= this->size()) {
491 panic<std::out_of_range>("rust::Vec index out of range");
492 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700493 return (*this)[n];
494}
495
496template <typename T>
497const T &Vec<T>::front() const {
498 return (*this)[0];
499}
500
501template <typename T>
502const T &Vec<T>::back() const {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700503 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700504}
505
506template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700507const T &Vec<T>::const_iterator::operator*() const noexcept {
508 return *static_cast<const T *>(this->pos);
509}
510
511template <typename T>
512const T *Vec<T>::const_iterator::operator->() const noexcept {
513 return static_cast<const T *>(this->pos);
514}
515
516template <typename T>
517typename Vec<T>::const_iterator &Vec<T>::const_iterator::operator++() noexcept {
518 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
519 return *this;
520}
521
522template <typename T>
523typename Vec<T>::const_iterator
524Vec<T>::const_iterator::operator++(int) noexcept {
525 auto ret = const_iterator(*this);
526 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
527 return ret;
528}
529
530template <typename T>
David Tolnayb10c4bc2020-08-26 21:55:29 -0700531bool Vec<T>::const_iterator::operator==(
532 const const_iterator &other) const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700533 return this->pos == other.pos;
534}
535
536template <typename T>
David Tolnayb10c4bc2020-08-26 21:55:29 -0700537bool Vec<T>::const_iterator::operator!=(
538 const const_iterator &other) const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700539 return this->pos != other.pos;
540}
541
542template <typename T>
543typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
544 const_iterator it;
545 it.pos = this->data();
546 it.stride = this->stride();
547 return it;
548}
549
550template <typename T>
551typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
552 const_iterator it = this->begin();
553 it.pos = static_cast<const uint8_t *>(it.pos) + it.stride * this->size();
554 return it;
555}
556
557// Internal API only intended for the cxxbridge code generator.
558template <typename T>
559Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay8f16ae72020-10-08 18:21:13 -0700560#endif // CXXBRIDGE05_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700561
David Tolnay8f16ae72020-10-08 18:21:13 -0700562} // namespace cxxbridge05
David Tolnay750755e2020-03-01 13:04:08 -0800563} // namespace rust