blob: 7dfbbcafd98b088baa014ada061ef5e4d418649a [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 Tolnay0356d332020-10-31 19:46:41 -070082 friend impl<Str>;
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 -0700107private:
David Tolnay36aa9e02020-10-31 23:08:21 -0700108 friend impl<Slice>;
109 // Not necessarily ABI compatible with &[T]. Codegen will translate to
110 // cxx::rust_sliceu8::RustSliceU8 which matches this layout.
111 const T *ptr;
112 size_t len;
David Tolnayefe81052020-04-14 16:28:24 -0700113};
David Tolnay8f16ae72020-10-08 18:21:13 -0700114#endif // CXXBRIDGE05_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -0700115
David Tolnay8f16ae72020-10-08 18:21:13 -0700116#ifndef CXXBRIDGE05_RUST_BOX
David Tolnayf262d382020-04-11 22:12:40 -0700117template <typename T>
118class Box final {
David Tolnay7db73692019-10-20 14:51:12 -0400119public:
David Tolnayf6292372020-03-01 21:09:11 -0800120 using value_type = T;
David Tolnay9706a512020-04-24 17:09:01 -0700121 using const_pointer =
122 typename std::add_pointer<typename std::add_const<T>::type>::type;
123 using pointer = typename std::add_pointer<T>::type;
David Tolnayf6292372020-03-01 21:09:11 -0800124
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700125 Box(const Box &);
126 Box(Box &&) noexcept;
127 ~Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400128
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700129 explicit Box(const T &);
130 explicit Box(T &&);
131
132 Box &operator=(const Box &);
133 Box &operator=(Box &&) noexcept;
134
135 const T *operator->() const noexcept;
136 const T &operator*() const noexcept;
137 T *operator->() noexcept;
138 T &operator*() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400139
David Tolnayf262d382020-04-11 22:12:40 -0700140 template <typename... Fields>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700141 static Box in_place(Fields &&...);
David Tolnay7ce59fc2020-04-11 11:46:33 -0700142
David Tolnay7db73692019-10-20 14:51:12 -0400143 // Important: requires that `raw` came from an into_raw call. Do not pass a
144 // pointer from `new` or any other source.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700145 static Box from_raw(T *) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400146
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700147 T *into_raw() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400148
149private:
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700150 Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400151 void uninit() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400152 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800153 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400154};
David Tolnay8f16ae72020-10-08 18:21:13 -0700155#endif // CXXBRIDGE05_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400156
David Tolnay8f16ae72020-10-08 18:21:13 -0700157#ifndef CXXBRIDGE05_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700158template <typename T>
159class Vec final {
160public:
David Tolnayc87c2152020-04-24 17:07:41 -0700161 using value_type = T;
162
David Tolnayf97c2d52020-04-25 16:37:48 -0700163 Vec() noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700164 Vec(Vec &&) noexcept;
165 ~Vec() noexcept;
David Tolnaycb800572020-04-24 20:30:43 -0700166
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700167 Vec &operator=(Vec &&) noexcept;
David Tolnayf97c2d52020-04-25 16:37:48 -0700168
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700169 size_t size() const noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700170 bool empty() const noexcept;
David Tolnay219c0792020-04-24 20:31:37 -0700171 const T *data() const noexcept;
David Tolnayfb6b73c2020-11-10 14:32:16 -0800172 T *data() noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700173
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700174 const T &operator[](size_t n) const noexcept;
175 const T &at(size_t n) const;
176
177 const T &front() const;
178 const T &back() const;
179
David Tolnayfb6b73c2020-11-10 14:32:16 -0800180 void reserve(size_t new_cap);
181 void push_back(const T &value);
182 void push_back(T &&value);
183 template <class... Args>
184 void emplace_back(Args &&... args);
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 Tolnayfb6b73c2020-11-10 14:32:16 -0800217 void reserve_total(size_t cap) noexcept;
218 void set_len(size_t len) noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700219 void drop() noexcept;
220
221 // Size and alignment statically verified by rust_vec.rs.
222 std::array<uintptr_t, 3> repr;
223};
David Tolnay8f16ae72020-10-08 18:21:13 -0700224#endif // CXXBRIDGE05_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700225
David Tolnay8f16ae72020-10-08 18:21:13 -0700226#ifndef 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 Tolnay174bd952020-11-02 09:23:12 -0800278// IsRelocatable<T> is used in assertions that a C++ type passed by value
279// between Rust and C++ is soundly relocatable by Rust.
280//
281// There may be legitimate reasons to opt out of the check for support of types
282// that the programmer knows are soundly Rust-movable despite not being
283// recognized as such by the C++ type system due to a move constructor or
284// destructor. To opt out of the relocatability check, do either of the
285// following things in any header used by `include!` in the bridge.
286//
287// --- if you define the type:
288// struct MyType {
289// ...
290// + using IsRelocatable = std::true_type;
291// };
292//
293// --- otherwise:
294// + template <>
295// + struct rust::IsRelocatable<MyType> : std::true_type {};
296template <typename T>
297struct IsRelocatable;
298
David Tolnay3b0c9882020-03-01 14:08:57 -0800299// Snake case aliases for use in code that uses this style for type names.
300using string = String;
301using str = Str;
David Tolnayf262d382020-04-11 22:12:40 -0700302template <class T>
David Tolnay38c87642020-09-06 22:18:08 -0700303using slice = Slice<T>;
304template <class T>
David Tolnayf262d382020-04-11 22:12:40 -0700305using box = Box<T>;
David Tolnay38c87642020-09-06 22:18:08 -0700306template <class T>
307using vec = Vec<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700308using error = Error;
David Tolnay75dca2e2020-03-25 20:17:52 -0700309template <typename Signature, bool Throws = false>
310using fn = Fn<Signature, Throws>;
David Tolnayf262d382020-04-11 22:12:40 -0700311template <typename Signature>
312using try_fn = TryFn<Signature>;
David Tolnay174bd952020-11-02 09:23:12 -0800313template <typename T>
314using is_relocatable = IsRelocatable<T>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800315
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700316
317
318////////////////////////////////////////////////////////////////////////////////
319/// end public API, begin implementation details
320
David Tolnay8f16ae72020-10-08 18:21:13 -0700321#ifndef CXXBRIDGE05_PANIC
322#define CXXBRIDGE05_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700323template <typename Exception>
324void panic [[noreturn]] (const char *msg);
David Tolnay8f16ae72020-10-08 18:21:13 -0700325#endif // CXXBRIDGE05_PANIC
David Tolnay521d99d2020-08-26 20:45:40 -0700326
David Tolnaya5a13012020-11-10 15:03:09 -0800327#ifndef CXXBRIDGE05_RUST_FN
328#define CXXBRIDGE05_RUST_FN
David Tolnay75dca2e2020-03-25 20:17:52 -0700329template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700330Ret Fn<Ret(Args...), Throws>::operator()(Args... args) const noexcept(!Throws) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700331 return (*this->trampoline)(std::move(args)..., this->fn);
332}
333
David Tolnaya23129c2020-04-08 20:08:21 -0700334template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700335Fn<Ret(Args...), Throws> Fn<Ret(Args...), Throws>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700336 return *this;
337}
David Tolnaya5a13012020-11-10 15:03:09 -0800338#endif // CXXBRIDGE05_RUST_FN
David Tolnaya23129c2020-04-08 20:08:21 -0700339
David Tolnay8f16ae72020-10-08 18:21:13 -0700340#ifndef CXXBRIDGE05_RUST_BITCOPY
341#define CXXBRIDGE05_RUST_BITCOPY
David Tolnay48521222020-10-31 14:59:42 -0700342struct unsafe_bitcopy_t final {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700343 explicit unsafe_bitcopy_t() = default;
344};
345
346constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnay8f16ae72020-10-08 18:21:13 -0700347#endif // CXXBRIDGE05_RUST_BITCOPY
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700348
David Tolnay5b1ee1f2020-10-31 18:21:39 -0700349#ifndef CXXBRIDGE05_RUST_STR
350#define CXXBRIDGE05_RUST_STR
351inline const char *Str::data() const noexcept { return this->ptr; }
352
353inline size_t Str::size() const noexcept { return this->len; }
354
355inline size_t Str::length() const noexcept { return this->len; }
356#endif // CXXBRIDGE05_RUST_STR
357
David Tolnay8f16ae72020-10-08 18:21:13 -0700358#ifndef CXXBRIDGE05_RUST_SLICE
359#define CXXBRIDGE05_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700360template <typename T>
David Tolnay36aa9e02020-10-31 23:08:21 -0700361Slice<T>::Slice() noexcept : ptr(reinterpret_cast<const T *>(this)), len(0) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700362
363template <typename T>
David Tolnay36aa9e02020-10-31 23:08:21 -0700364Slice<T>::Slice(const T *s, size_t count) noexcept : ptr(s), len(count) {}
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700365
366template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700367const T *Slice<T>::data() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700368 return this->ptr;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700369}
370
371template <typename T>
372size_t Slice<T>::size() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700373 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700374}
375
376template <typename T>
377size_t Slice<T>::length() const noexcept {
David Tolnay36aa9e02020-10-31 23:08:21 -0700378 return this->len;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700379}
David Tolnay8f16ae72020-10-08 18:21:13 -0700380#endif // CXXBRIDGE05_RUST_SLICE
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700381
David Tolnay8f16ae72020-10-08 18:21:13 -0700382#ifndef CXXBRIDGE05_RUST_BOX
383#define CXXBRIDGE05_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700384template <typename T>
385Box<T>::Box(const Box &other) : Box(*other) {}
386
387template <typename T>
388Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
389 other.ptr = nullptr;
390}
391
392template <typename T>
393Box<T>::Box(const T &val) {
394 this->uninit();
395 ::new (this->ptr) T(val);
396}
397
398template <typename T>
399Box<T>::Box(T &&val) {
400 this->uninit();
401 ::new (this->ptr) T(std::move(val));
402}
403
404template <typename T>
405Box<T>::~Box() noexcept {
406 if (this->ptr) {
407 this->drop();
408 }
409}
410
411template <typename T>
412Box<T> &Box<T>::operator=(const Box &other) {
413 if (this != &other) {
414 if (this->ptr) {
415 **this = *other;
416 } else {
417 this->uninit();
418 ::new (this->ptr) T(*other);
419 }
420 }
421 return *this;
422}
423
424template <typename T>
425Box<T> &Box<T>::operator=(Box &&other) noexcept {
426 if (this->ptr) {
427 this->drop();
428 }
429 this->ptr = other.ptr;
430 other.ptr = nullptr;
431 return *this;
432}
433
434template <typename T>
435const T *Box<T>::operator->() const noexcept {
436 return this->ptr;
437}
438
439template <typename T>
440const T &Box<T>::operator*() const noexcept {
441 return *this->ptr;
442}
443
444template <typename T>
445T *Box<T>::operator->() noexcept {
446 return this->ptr;
447}
448
449template <typename T>
450T &Box<T>::operator*() noexcept {
451 return *this->ptr;
452}
453
454template <typename T>
455template <typename... Fields>
456Box<T> Box<T>::in_place(Fields &&... fields) {
457 Box box;
458 box.uninit();
459 ::new (box.ptr) T{std::forward<Fields>(fields)...};
460 return box;
461}
462
463template <typename T>
464Box<T> Box<T>::from_raw(T *raw) noexcept {
465 Box box;
466 box.ptr = raw;
467 return box;
468}
469
470template <typename T>
471T *Box<T>::into_raw() noexcept {
472 T *raw = this->ptr;
473 this->ptr = nullptr;
474 return raw;
475}
476
477template <typename T>
478Box<T>::Box() noexcept {}
David Tolnay8f16ae72020-10-08 18:21:13 -0700479#endif // CXXBRIDGE05_RUST_BOX
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700480
David Tolnay8f16ae72020-10-08 18:21:13 -0700481#ifndef CXXBRIDGE05_RUST_VEC
482#define CXXBRIDGE05_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700483template <typename T>
484Vec<T>::Vec(Vec &&other) noexcept {
485 this->repr = other.repr;
486 new (&other) Vec();
487}
488
489template <typename T>
490Vec<T>::~Vec() noexcept {
491 this->drop();
492}
493
494template <typename T>
495Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
496 if (this != &other) {
497 this->drop();
498 this->repr = other.repr;
499 new (&other) Vec();
500 }
501 return *this;
502}
503
504template <typename T>
505bool Vec<T>::empty() const noexcept {
506 return size() == 0;
507}
508
509template <typename T>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800510T *Vec<T>::data() noexcept {
511 return const_cast<T *>(const_cast<const Vec<T> *>(this)->data());
512}
513
514template <typename T>
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700515const T &Vec<T>::operator[](size_t n) const noexcept {
516 auto data = reinterpret_cast<const char *>(this->data());
517 return *reinterpret_cast<const T *>(data + n * this->stride());
518}
519
520template <typename T>
521const T &Vec<T>::at(size_t n) const {
David Tolnay8e1e6ac2020-08-26 20:51:43 -0700522 if (n >= this->size()) {
523 panic<std::out_of_range>("rust::Vec index out of range");
524 }
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700525 return (*this)[n];
526}
527
528template <typename T>
529const T &Vec<T>::front() const {
530 return (*this)[0];
531}
532
533template <typename T>
534const T &Vec<T>::back() const {
David Tolnayb10c4bc2020-08-26 21:55:29 -0700535 return (*this)[this->size() - 1];
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700536}
537
538template <typename T>
David Tolnayfb6b73c2020-11-10 14:32:16 -0800539void Vec<T>::reserve(size_t new_cap) {
540 this->reserve_total(new_cap);
541}
542
543template <typename T>
544void Vec<T>::push_back(const T &value) {
545 this->emplace_back(value);
546}
547
548template <typename T>
549void Vec<T>::push_back(T &&value) {
550 this->emplace_back(std::move(value));
551}
552
553template <typename T>
554template <typename... Args>
555void Vec<T>::emplace_back(Args &&... args) {
556 auto size = this->size();
557 this->reserve_total(size + 1);
558 ::new (reinterpret_cast<T *>(reinterpret_cast<char *>(this->data()) +
559 size * this->stride()))
560 T(std::forward<Args>(args)...);
561 this->set_len(size + 1);
562}
563
564template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700565const T &Vec<T>::const_iterator::operator*() const noexcept {
566 return *static_cast<const T *>(this->pos);
567}
568
569template <typename T>
570const T *Vec<T>::const_iterator::operator->() const noexcept {
571 return static_cast<const T *>(this->pos);
572}
573
574template <typename T>
575typename Vec<T>::const_iterator &Vec<T>::const_iterator::operator++() noexcept {
576 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
577 return *this;
578}
579
580template <typename T>
581typename Vec<T>::const_iterator
582Vec<T>::const_iterator::operator++(int) noexcept {
583 auto ret = const_iterator(*this);
584 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
585 return ret;
586}
587
588template <typename T>
David Tolnayb10c4bc2020-08-26 21:55:29 -0700589bool Vec<T>::const_iterator::operator==(
590 const const_iterator &other) const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700591 return this->pos == other.pos;
592}
593
594template <typename T>
David Tolnayb10c4bc2020-08-26 21:55:29 -0700595bool Vec<T>::const_iterator::operator!=(
596 const const_iterator &other) const noexcept {
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700597 return this->pos != other.pos;
598}
599
600template <typename T>
601typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
602 const_iterator it;
603 it.pos = this->data();
604 it.stride = this->stride();
605 return it;
606}
607
608template <typename T>
609typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
610 const_iterator it = this->begin();
611 it.pos = static_cast<const uint8_t *>(it.pos) + it.stride * this->size();
612 return it;
613}
614
615// Internal API only intended for the cxxbridge code generator.
616template <typename T>
617Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
David Tolnay8f16ae72020-10-08 18:21:13 -0700618#endif // CXXBRIDGE05_RUST_VEC
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700619
David Tolnay174bd952020-11-02 09:23:12 -0800620#ifndef CXXBRIDGE05_RELOCATABLE
621#define CXXBRIDGE05_RELOCATABLE
622namespace detail {
623template <typename... Ts>
624struct make_void {
625 using type = void;
626};
627
628template <typename... Ts>
629using void_t = typename make_void<Ts...>::type;
630
631template <typename Void, template <typename...> class, typename...>
632struct detect : std::false_type {};
633template <template <typename...> class T, typename... A>
634struct detect<void_t<T<A...>>, T, A...> : std::true_type {};
635
636template <template <typename...> class T, typename... A>
637using is_detected = detect<void, T, A...>;
638
639template <typename T>
640using detect_IsRelocatable = typename T::IsRelocatable;
641
642template <typename T>
643struct get_IsRelocatable
644 : std::is_same<typename T::IsRelocatable, std::true_type> {};
645} // namespace detail
646
647template <typename T>
648struct IsRelocatable
649 : std::conditional<
650 detail::is_detected<detail::detect_IsRelocatable, T>::value,
651 detail::get_IsRelocatable<T>,
652 std::integral_constant<
653 bool, std::is_trivially_move_constructible<T>::value &&
654 std::is_trivially_destructible<T>::value>>::type {};
655#endif // CXXBRIDGE05_RELOCATABLE
656
David Tolnay8f16ae72020-10-08 18:21:13 -0700657} // namespace cxxbridge05
David Tolnay750755e2020-03-01 13:04:08 -0800658} // namespace rust