blob: 04a63d6fbff290b1389616b03c9c51d2b3d14709 [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 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 Tolnay2a2b9ad2020-05-12 20:07:26 -070097 Slice &operator=(Slice<T>) noexcept;
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>
332Slice<T> &Slice<T>::operator=(Slice<T> other) noexcept {
333 this->repr = other.repr;
334 return *this;
335}
336
337template <typename T>
338const T *Slice<T>::data() const noexcept {
339 return this->repr.ptr;
340}
341
342template <typename T>
343size_t Slice<T>::size() const noexcept {
344 return this->repr.len;
345}
346
347template <typename T>
348size_t Slice<T>::length() const noexcept {
349 return this->repr.len;
350}
351
352template <typename T>
353Slice<T>::Slice(Repr repr_) noexcept : repr(repr_) {}
354
355template <typename T>
David Tolnaycedcde12020-10-31 11:47:14 -0700356Slice<T>::operator Repr() const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700357 return this->repr;
358}
David Tolnay8f16ae72020-10-08 18:21:13 -0700359#endif // CXXBRIDGE05_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700360
David Tolnay8f16ae72020-10-08 18:21:13 -0700361#ifndef CXXBRIDGE05_RUST_BOX
362#define CXXBRIDGE05_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700363template <typename T>
364Box<T>::Box(const Box &other) : Box(*other) {}
365
366template <typename T>
367Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
368 other.ptr = nullptr;
369}
370
371template <typename T>
372Box<T>::Box(const T &val) {
373 this->uninit();
374 ::new (this->ptr) T(val);
375}
376
377template <typename T>
378Box<T>::Box(T &&val) {
379 this->uninit();
380 ::new (this->ptr) T(std::move(val));
381}
382
383template <typename T>
384Box<T>::~Box() noexcept {
385 if (this->ptr) {
386 this->drop();
387 }
388}
389
390template <typename T>
391Box<T> &Box<T>::operator=(const Box &other) {
392 if (this != &other) {
393 if (this->ptr) {
394 **this = *other;
395 } else {
396 this->uninit();
397 ::new (this->ptr) T(*other);
398 }
399 }
400 return *this;
401}
402
403template <typename T>
404Box<T> &Box<T>::operator=(Box &&other) noexcept {
405 if (this->ptr) {
406 this->drop();
407 }
408 this->ptr = other.ptr;
409 other.ptr = nullptr;
410 return *this;
411}
412
413template <typename T>
414const T *Box<T>::operator->() const noexcept {
415 return this->ptr;
416}
417
418template <typename T>
419const T &Box<T>::operator*() const noexcept {
420 return *this->ptr;
421}
422
423template <typename T>
424T *Box<T>::operator->() noexcept {
425 return this->ptr;
426}
427
428template <typename T>
429T &Box<T>::operator*() noexcept {
430 return *this->ptr;
431}
432
433template <typename T>
434template <typename... Fields>
435Box<T> Box<T>::in_place(Fields &&... fields) {
436 Box box;
437 box.uninit();
438 ::new (box.ptr) T{std::forward<Fields>(fields)...};
439 return box;
440}
441
442template <typename T>
443Box<T> Box<T>::from_raw(T *raw) noexcept {
444 Box box;
445 box.ptr = raw;
446 return box;
447}
448
449template <typename T>
450T *Box<T>::into_raw() noexcept {
451 T *raw = this->ptr;
452 this->ptr = nullptr;
453 return raw;
454}
455
456template <typename T>
457Box<T>::Box() noexcept {}
David Tolnay8f16ae72020-10-08 18:21:13 -0700458#endif // CXXBRIDGE05_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700459
David Tolnay8f16ae72020-10-08 18:21:13 -0700460#ifndef CXXBRIDGE05_RUST_VEC
461#define CXXBRIDGE05_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700462template <typename T>
463Vec<T>::Vec(Vec &&other) noexcept {
464 this->repr = other.repr;
465 new (&other) Vec();
466}
467
468template <typename T>
469Vec<T>::~Vec() noexcept {
470 this->drop();
471}
472
473template <typename T>
474Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
475 if (this != &other) {
476 this->drop();
477 this->repr = other.repr;
478 new (&other) Vec();
479 }
480 return *this;
481}
482
483template <typename T>
484bool Vec<T>::empty() const noexcept {
485 return size() == 0;
486}
487
488template <typename T>
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700489const T &Vec<T>::operator[](size_t n) const noexcept {
490 auto data = reinterpret_cast<const char *>(this->data());
491 return *reinterpret_cast<const T *>(data + n * this->stride());
492}
493
494template <typename T>
495const T &Vec<T>::at(size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700496 if (n >= this->size()) {
497 panic<std::out_of_range>("rust::Vec index out of range");
498 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700499 return (*this)[n];
500}
501
502template <typename T>
503const T &Vec<T>::front() const {
504 return (*this)[0];
505}
506
507template <typename T>
508const T &Vec<T>::back() const {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700509 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700510}
511
512template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700513const T &Vec<T>::const_iterator::operator*() const noexcept {
514 return *static_cast<const T *>(this->pos);
515}
516
517template <typename T>
518const T *Vec<T>::const_iterator::operator->() const noexcept {
519 return static_cast<const T *>(this->pos);
520}
521
522template <typename T>
523typename Vec<T>::const_iterator &Vec<T>::const_iterator::operator++() noexcept {
524 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
525 return *this;
526}
527
528template <typename T>
529typename Vec<T>::const_iterator
530Vec<T>::const_iterator::operator++(int) noexcept {
531 auto ret = const_iterator(*this);
532 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
533 return ret;
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>
David Tolnayb10c4bc2020-08-26 21:55:29 -0700543bool Vec<T>::const_iterator::operator!=(
544 const const_iterator &other) const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700545 return this->pos != other.pos;
546}
547
548template <typename T>
549typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
550 const_iterator it;
551 it.pos = this->data();
552 it.stride = this->stride();
553 return it;
554}
555
556template <typename T>
557typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
558 const_iterator it = this->begin();
559 it.pos = static_cast<const uint8_t *>(it.pos) + it.stride * this->size();
560 return it;
561}
562
563// Internal API only intended for the cxxbridge code generator.
564template <typename T>
565Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay8f16ae72020-10-08 18:21:13 -0700566#endif // CXXBRIDGE05_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700567
David Tolnay8f16ae72020-10-08 18:21:13 -0700568} // namespace cxxbridge05
David Tolnay750755e2020-03-01 13:04:08 -0800569} // namespace rust