| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | #include <array> |
| David Tolnay | 30430f1 | 2020-03-19 20:49:00 -0700 | [diff] [blame] | 3 | #include <cstddef> |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 4 | #include <cstdint> |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 5 | #include <exception> |
| David Tolnay | 001102a | 2020-03-01 20:05:04 -0800 | [diff] [blame] | 6 | #include <iosfwd> |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 7 | #include <string> |
| David Tolnay | f629237 | 2020-03-01 21:09:11 -0800 | [diff] [blame] | 8 | #include <type_traits> |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 9 | #include <utility> |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 10 | #include <vector> |
| David Tolnay | 59b5ba1 | 2020-04-10 11:32:19 -0700 | [diff] [blame] | 11 | #if defined(_WIN32) |
| 12 | #include <BaseTsd.h> |
| 13 | #endif |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 14 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 15 | namespace rust { |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 16 | inline namespace cxxbridge03 { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 17 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame^] | 18 | struct unsafe_bitcopy_t; |
| David Tolnay | d1e2efc | 2020-03-03 22:25:43 -0800 | [diff] [blame] | 19 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 20 | #ifndef CXXBRIDGE03_RUST_STRING |
| 21 | #define CXXBRIDGE03_RUST_STRING |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 22 | class String final { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 23 | public: |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 24 | String() noexcept; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 25 | String(const String &) noexcept; |
| 26 | String(String &&) noexcept; |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 27 | ~String() noexcept; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 28 | |
| 29 | String(const std::string &); |
| 30 | String(const char *); |
| 31 | |
| 32 | String &operator=(const String &) noexcept; |
| 33 | String &operator=(String &&) noexcept; |
| 34 | |
| David Tolnay | 404d689 | 2020-03-01 20:19:41 -0800 | [diff] [blame] | 35 | explicit operator std::string() const; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 36 | |
| 37 | // Note: no null terminator. |
| 38 | const char *data() const noexcept; |
| 39 | size_t size() const noexcept; |
| 40 | size_t length() const noexcept; |
| 41 | |
| David Tolnay | d1e2efc | 2020-03-03 22:25:43 -0800 | [diff] [blame] | 42 | // Internal API only intended for the cxxbridge code generator. |
| 43 | String(unsafe_bitcopy_t, const String &) noexcept; |
| 44 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 45 | private: |
| 46 | // Size and alignment statically verified by rust_string.rs. |
| 47 | std::array<uintptr_t, 3> repr; |
| 48 | }; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 49 | #endif // CXXBRIDGE03_RUST_STRING |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 50 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 51 | #ifndef CXXBRIDGE03_RUST_STR |
| 52 | #define CXXBRIDGE03_RUST_STR |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 53 | class Str final { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 54 | public: |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 55 | Str() noexcept; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 56 | Str(const Str &) noexcept; |
| 57 | |
| David Tolnay | 851677c | 2020-03-01 23:49:46 -0800 | [diff] [blame] | 58 | Str(const std::string &); |
| 59 | Str(const char *); |
| 60 | Str(std::string &&) = delete; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 61 | |
| 62 | Str &operator=(Str) noexcept; |
| 63 | |
| David Tolnay | 404d689 | 2020-03-01 20:19:41 -0800 | [diff] [blame] | 64 | explicit operator std::string() const; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 65 | |
| 66 | // Note: no null terminator. |
| 67 | const char *data() const noexcept; |
| 68 | size_t size() const noexcept; |
| 69 | size_t length() const noexcept; |
| 70 | |
| 71 | // Repr is PRIVATE; must not be used other than by our generated code. |
| 72 | // |
| 73 | // Not necessarily ABI compatible with &str. Codegen will translate to |
| 74 | // cxx::rust_str::RustStr which matches this layout. |
| 75 | struct Repr { |
| 76 | const char *ptr; |
| 77 | size_t len; |
| 78 | }; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 79 | Str(Repr) noexcept; |
| David Tolnay | baae443 | 2020-03-01 20:20:10 -0800 | [diff] [blame] | 80 | explicit operator Repr() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 81 | |
| 82 | private: |
| 83 | Repr repr; |
| 84 | }; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 85 | #endif // CXXBRIDGE03_RUST_STR |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 86 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 87 | #ifndef CXXBRIDGE03_RUST_SLICE |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 88 | template <typename T> |
| 89 | class Slice final { |
| 90 | public: |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame^] | 91 | Slice() noexcept; |
| 92 | Slice(const Slice<T> &) noexcept; |
| 93 | Slice(const T *, size_t count) noexcept; |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 94 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame^] | 95 | Slice &operator=(Slice<T>) noexcept; |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 96 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame^] | 97 | const T *data() const noexcept; |
| 98 | size_t size() const noexcept; |
| 99 | size_t length() const noexcept; |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 100 | |
| 101 | // Repr is PRIVATE; must not be used other than by our generated code. |
| 102 | // |
| 103 | // At present this class is only used for &[u8] slices. |
| 104 | // Not necessarily ABI compatible with &[u8]. Codegen will translate to |
| David Tolnay | e710af1 | 2020-04-14 16:31:54 -0700 | [diff] [blame] | 105 | // cxx::rust_sliceu8::RustSliceU8 which matches this layout. |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 106 | struct Repr { |
| 107 | const T *ptr; |
| 108 | size_t len; |
| 109 | }; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame^] | 110 | Slice(Repr) noexcept; |
| 111 | explicit operator Repr() noexcept; |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 112 | |
| 113 | private: |
| 114 | Repr repr; |
| 115 | }; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 116 | #endif // CXXBRIDGE03_RUST_SLICE |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 117 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 118 | #ifndef CXXBRIDGE03_RUST_BOX |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 119 | template <typename T> |
| 120 | class Box final { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 121 | public: |
| David Tolnay | f629237 | 2020-03-01 21:09:11 -0800 | [diff] [blame] | 122 | using value_type = T; |
| David Tolnay | 9706a51 | 2020-04-24 17:09:01 -0700 | [diff] [blame] | 123 | using const_pointer = |
| 124 | typename std::add_pointer<typename std::add_const<T>::type>::type; |
| 125 | using pointer = typename std::add_pointer<T>::type; |
| David Tolnay | f629237 | 2020-03-01 21:09:11 -0800 | [diff] [blame] | 126 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame^] | 127 | Box(const Box &); |
| 128 | Box(Box &&) noexcept; |
| 129 | ~Box() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 130 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame^] | 131 | explicit Box(const T &); |
| 132 | explicit Box(T &&); |
| 133 | |
| 134 | Box &operator=(const Box &); |
| 135 | Box &operator=(Box &&) noexcept; |
| 136 | |
| 137 | const T *operator->() const noexcept; |
| 138 | const T &operator*() const noexcept; |
| 139 | T *operator->() noexcept; |
| 140 | T &operator*() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 141 | |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 142 | template <typename... Fields> |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame^] | 143 | static Box in_place(Fields &&...); |
| David Tolnay | 7ce59fc | 2020-04-11 11:46:33 -0700 | [diff] [blame] | 144 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 145 | // Important: requires that `raw` came from an into_raw call. Do not pass a |
| 146 | // pointer from `new` or any other source. |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame^] | 147 | static Box from_raw(T *) noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 148 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame^] | 149 | T *into_raw() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 150 | |
| 151 | private: |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame^] | 152 | Box() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 153 | void uninit() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 154 | void drop() noexcept; |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 155 | T *ptr; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 156 | }; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 157 | #endif // CXXBRIDGE03_RUST_BOX |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 158 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 159 | #ifndef CXXBRIDGE03_RUST_VEC |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 160 | template <typename T> |
| 161 | class Vec final { |
| 162 | public: |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 163 | using value_type = T; |
| 164 | |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 165 | Vec() noexcept; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame^] | 166 | Vec(Vec &&) noexcept; |
| 167 | ~Vec() noexcept; |
| David Tolnay | cb80057 | 2020-04-24 20:30:43 -0700 | [diff] [blame] | 168 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame^] | 169 | Vec &operator=(Vec &&) noexcept; |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 170 | |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 171 | size_t size() const noexcept; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame^] | 172 | bool empty() const noexcept; |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 173 | const T *data() const noexcept; |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 174 | |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 175 | class const_iterator { |
| 176 | public: |
| myronahn | da9be50 | 2020-04-29 05:47:23 +0700 | [diff] [blame] | 177 | using difference_type = ptrdiff_t; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 178 | using value_type = typename std::add_const<T>::type; |
| David Tolnay | 74dd379 | 2020-04-30 07:45:24 -0700 | [diff] [blame] | 179 | using pointer = |
| 180 | typename std::add_pointer<typename std::add_const<T>::type>::type; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 181 | using reference = typename std::add_lvalue_reference< |
| 182 | typename std::add_const<T>::type>::type; |
| myronahn | da9be50 | 2020-04-29 05:47:23 +0700 | [diff] [blame] | 183 | using iterator_category = std::forward_iterator_tag; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 184 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame^] | 185 | const T &operator*() const noexcept; |
| 186 | const T *operator->() const noexcept; |
| 187 | const_iterator &operator++() noexcept; |
| 188 | const_iterator operator++(int) noexcept; |
| 189 | bool operator==(const const_iterator &) const noexcept; |
| 190 | bool operator!=(const const_iterator &) const noexcept; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 191 | |
| 192 | private: |
| 193 | friend class Vec; |
| 194 | const void *pos; |
| 195 | size_t stride; |
| 196 | }; |
| 197 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame^] | 198 | const_iterator begin() const noexcept; |
| 199 | const_iterator end() const noexcept; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 200 | |
| David Tolnay | 313b10e | 2020-04-25 16:30:51 -0700 | [diff] [blame] | 201 | // Internal API only intended for the cxxbridge code generator. |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame^] | 202 | Vec(unsafe_bitcopy_t, const Vec &) noexcept; |
| David Tolnay | 313b10e | 2020-04-25 16:30:51 -0700 | [diff] [blame] | 203 | |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 204 | private: |
| David Tolnay | 503d019 | 2020-04-24 22:18:56 -0700 | [diff] [blame] | 205 | static size_t stride() noexcept; |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 206 | void drop() noexcept; |
| 207 | |
| 208 | // Size and alignment statically verified by rust_vec.rs. |
| 209 | std::array<uintptr_t, 3> repr; |
| 210 | }; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 211 | #endif // CXXBRIDGE03_RUST_VEC |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 212 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 213 | #ifndef CXXBRIDGE03_RUST_FN |
| 214 | #define CXXBRIDGE03_RUST_FN |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 215 | template <typename Signature, bool Throws = false> |
| 216 | class Fn; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 217 | |
| 218 | template <typename Ret, typename... Args, bool Throws> |
| 219 | class Fn<Ret(Args...), Throws> { |
| 220 | public: |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 221 | Ret operator()(Args... args) const noexcept(!Throws); |
| 222 | Fn operator*() const noexcept; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 223 | |
| 224 | private: |
| 225 | Ret (*trampoline)(Args..., void *fn) noexcept(!Throws); |
| 226 | void *fn; |
| 227 | }; |
| 228 | |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 229 | template <typename Signature> |
| 230 | using TryFn = Fn<Signature, true>; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 231 | #endif // CXXBRIDGE03_RUST_FN |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 232 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 233 | #ifndef CXXBRIDGE03_RUST_ERROR |
| 234 | #define CXXBRIDGE03_RUST_ERROR |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 235 | class Error final : std::exception { |
| 236 | public: |
| 237 | Error(const Error &); |
| 238 | Error(Error &&) noexcept; |
| 239 | Error(Str::Repr) noexcept; |
| 240 | ~Error() noexcept; |
| 241 | const char *what() const noexcept override; |
| 242 | |
| 243 | private: |
| 244 | Str::Repr msg; |
| 245 | }; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 246 | #endif // CXXBRIDGE03_RUST_ERROR |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 247 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 248 | #ifndef CXXBRIDGE03_RUST_ISIZE |
| 249 | #define CXXBRIDGE03_RUST_ISIZE |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 250 | #if defined(_WIN32) |
| 251 | using isize = SSIZE_T; |
| 252 | #else |
| 253 | using isize = ssize_t; |
| 254 | #endif |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 255 | #endif // CXXBRIDGE03_RUST_ISIZE |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 256 | |
| David Tolnay | 851677c | 2020-03-01 23:49:46 -0800 | [diff] [blame] | 257 | std::ostream &operator<<(std::ostream &, const String &); |
| 258 | std::ostream &operator<<(std::ostream &, const Str &); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 259 | |
| David Tolnay | 3b0c988 | 2020-03-01 14:08:57 -0800 | [diff] [blame] | 260 | // Snake case aliases for use in code that uses this style for type names. |
| 261 | using string = String; |
| 262 | using str = Str; |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 263 | template <class T> |
| 264 | using box = Box<T>; |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 265 | using error = Error; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 266 | template <typename Signature, bool Throws = false> |
| 267 | using fn = Fn<Signature, Throws>; |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 268 | template <typename Signature> |
| 269 | using try_fn = TryFn<Signature>; |
| David Tolnay | 3b0c988 | 2020-03-01 14:08:57 -0800 | [diff] [blame] | 270 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame^] | 271 | |
| 272 | |
| 273 | //////////////////////////////////////////////////////////////////////////////// |
| 274 | /// end public API, begin implementation details |
| 275 | |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 276 | template <typename Ret, typename... Args, bool Throws> |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 277 | Ret Fn<Ret(Args...), Throws>::operator()(Args... args) const noexcept(!Throws) { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 278 | return (*this->trampoline)(std::move(args)..., this->fn); |
| 279 | } |
| 280 | |
| David Tolnay | a23129c | 2020-04-08 20:08:21 -0700 | [diff] [blame] | 281 | template <typename Ret, typename... Args, bool Throws> |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 282 | Fn<Ret(Args...), Throws> Fn<Ret(Args...), Throws>::operator*() const noexcept { |
| David Tolnay | a23129c | 2020-04-08 20:08:21 -0700 | [diff] [blame] | 283 | return *this; |
| 284 | } |
| 285 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame^] | 286 | #ifndef CXXBRIDGE03_RUST_BITCOPY |
| 287 | #define CXXBRIDGE03_RUST_BITCOPY |
| 288 | struct unsafe_bitcopy_t { |
| 289 | explicit unsafe_bitcopy_t() = default; |
| 290 | }; |
| 291 | |
| 292 | constexpr unsafe_bitcopy_t unsafe_bitcopy{}; |
| 293 | #endif // CXXBRIDGE03_RUST_BITCOPY |
| 294 | |
| 295 | #ifndef CXXBRIDGE03_RUST_SLICE |
| 296 | #define CXXBRIDGE03_RUST_SLICE |
| 297 | template <typename T> |
| 298 | Slice<T>::Slice() noexcept : repr(Repr{reinterpret_cast<const T *>(this), 0}) {} |
| 299 | |
| 300 | template <typename T> |
| 301 | Slice<T>::Slice(const Slice<T> &) noexcept = default; |
| 302 | |
| 303 | template <typename T> |
| 304 | Slice<T>::Slice(const T *s, size_t count) noexcept : repr(Repr{s, count}) {} |
| 305 | |
| 306 | template <typename T> |
| 307 | Slice<T> &Slice<T>::operator=(Slice<T> other) noexcept { |
| 308 | this->repr = other.repr; |
| 309 | return *this; |
| 310 | } |
| 311 | |
| 312 | template <typename T> |
| 313 | const T *Slice<T>::data() const noexcept { |
| 314 | return this->repr.ptr; |
| 315 | } |
| 316 | |
| 317 | template <typename T> |
| 318 | size_t Slice<T>::size() const noexcept { |
| 319 | return this->repr.len; |
| 320 | } |
| 321 | |
| 322 | template <typename T> |
| 323 | size_t Slice<T>::length() const noexcept { |
| 324 | return this->repr.len; |
| 325 | } |
| 326 | |
| 327 | template <typename T> |
| 328 | Slice<T>::Slice(Repr repr_) noexcept : repr(repr_) {} |
| 329 | |
| 330 | template <typename T> |
| 331 | Slice<T>::operator Repr() noexcept { |
| 332 | return this->repr; |
| 333 | } |
| 334 | #endif // CXXBRIDGE03_RUST_SLICE |
| 335 | |
| 336 | #ifndef CXXBRIDGE03_RUST_BOX |
| 337 | #define CXXBRIDGE03_RUST_BOX |
| 338 | template <typename T> |
| 339 | Box<T>::Box(const Box &other) : Box(*other) {} |
| 340 | |
| 341 | template <typename T> |
| 342 | Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) { |
| 343 | other.ptr = nullptr; |
| 344 | } |
| 345 | |
| 346 | template <typename T> |
| 347 | Box<T>::Box(const T &val) { |
| 348 | this->uninit(); |
| 349 | ::new (this->ptr) T(val); |
| 350 | } |
| 351 | |
| 352 | template <typename T> |
| 353 | Box<T>::Box(T &&val) { |
| 354 | this->uninit(); |
| 355 | ::new (this->ptr) T(std::move(val)); |
| 356 | } |
| 357 | |
| 358 | template <typename T> |
| 359 | Box<T>::~Box() noexcept { |
| 360 | if (this->ptr) { |
| 361 | this->drop(); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | template <typename T> |
| 366 | Box<T> &Box<T>::operator=(const Box &other) { |
| 367 | if (this != &other) { |
| 368 | if (this->ptr) { |
| 369 | **this = *other; |
| 370 | } else { |
| 371 | this->uninit(); |
| 372 | ::new (this->ptr) T(*other); |
| 373 | } |
| 374 | } |
| 375 | return *this; |
| 376 | } |
| 377 | |
| 378 | template <typename T> |
| 379 | Box<T> &Box<T>::operator=(Box &&other) noexcept { |
| 380 | if (this->ptr) { |
| 381 | this->drop(); |
| 382 | } |
| 383 | this->ptr = other.ptr; |
| 384 | other.ptr = nullptr; |
| 385 | return *this; |
| 386 | } |
| 387 | |
| 388 | template <typename T> |
| 389 | const T *Box<T>::operator->() const noexcept { |
| 390 | return this->ptr; |
| 391 | } |
| 392 | |
| 393 | template <typename T> |
| 394 | const T &Box<T>::operator*() const noexcept { |
| 395 | return *this->ptr; |
| 396 | } |
| 397 | |
| 398 | template <typename T> |
| 399 | T *Box<T>::operator->() noexcept { |
| 400 | return this->ptr; |
| 401 | } |
| 402 | |
| 403 | template <typename T> |
| 404 | T &Box<T>::operator*() noexcept { |
| 405 | return *this->ptr; |
| 406 | } |
| 407 | |
| 408 | template <typename T> |
| 409 | template <typename... Fields> |
| 410 | Box<T> Box<T>::in_place(Fields &&... fields) { |
| 411 | Box box; |
| 412 | box.uninit(); |
| 413 | ::new (box.ptr) T{std::forward<Fields>(fields)...}; |
| 414 | return box; |
| 415 | } |
| 416 | |
| 417 | template <typename T> |
| 418 | Box<T> Box<T>::from_raw(T *raw) noexcept { |
| 419 | Box box; |
| 420 | box.ptr = raw; |
| 421 | return box; |
| 422 | } |
| 423 | |
| 424 | template <typename T> |
| 425 | T *Box<T>::into_raw() noexcept { |
| 426 | T *raw = this->ptr; |
| 427 | this->ptr = nullptr; |
| 428 | return raw; |
| 429 | } |
| 430 | |
| 431 | template <typename T> |
| 432 | Box<T>::Box() noexcept {} |
| 433 | #endif // CXXBRIDGE03_RUST_BOX |
| 434 | |
| 435 | #ifndef CXXBRIDGE03_RUST_VEC |
| 436 | #define CXXBRIDGE03_RUST_VEC |
| 437 | template <typename T> |
| 438 | Vec<T>::Vec(Vec &&other) noexcept { |
| 439 | this->repr = other.repr; |
| 440 | new (&other) Vec(); |
| 441 | } |
| 442 | |
| 443 | template <typename T> |
| 444 | Vec<T>::~Vec() noexcept { |
| 445 | this->drop(); |
| 446 | } |
| 447 | |
| 448 | template <typename T> |
| 449 | Vec<T> &Vec<T>::operator=(Vec &&other) noexcept { |
| 450 | if (this != &other) { |
| 451 | this->drop(); |
| 452 | this->repr = other.repr; |
| 453 | new (&other) Vec(); |
| 454 | } |
| 455 | return *this; |
| 456 | } |
| 457 | |
| 458 | template <typename T> |
| 459 | bool Vec<T>::empty() const noexcept { |
| 460 | return size() == 0; |
| 461 | } |
| 462 | |
| 463 | template <typename T> |
| 464 | const T &Vec<T>::const_iterator::operator*() const noexcept { |
| 465 | return *static_cast<const T *>(this->pos); |
| 466 | } |
| 467 | |
| 468 | template <typename T> |
| 469 | const T *Vec<T>::const_iterator::operator->() const noexcept { |
| 470 | return static_cast<const T *>(this->pos); |
| 471 | } |
| 472 | |
| 473 | template <typename T> |
| 474 | typename Vec<T>::const_iterator &Vec<T>::const_iterator::operator++() noexcept { |
| 475 | this->pos = static_cast<const uint8_t *>(this->pos) + this->stride; |
| 476 | return *this; |
| 477 | } |
| 478 | |
| 479 | template <typename T> |
| 480 | typename Vec<T>::const_iterator |
| 481 | Vec<T>::const_iterator::operator++(int) noexcept { |
| 482 | auto ret = const_iterator(*this); |
| 483 | this->pos = static_cast<const uint8_t *>(this->pos) + this->stride; |
| 484 | return ret; |
| 485 | } |
| 486 | |
| 487 | template <typename T> |
| 488 | bool Vec<T>::const_iterator::operator==(const const_iterator &other) const |
| 489 | noexcept { |
| 490 | return this->pos == other.pos; |
| 491 | } |
| 492 | |
| 493 | template <typename T> |
| 494 | bool Vec<T>::const_iterator::operator!=(const const_iterator &other) const |
| 495 | noexcept { |
| 496 | return this->pos != other.pos; |
| 497 | } |
| 498 | |
| 499 | template <typename T> |
| 500 | typename Vec<T>::const_iterator Vec<T>::begin() const noexcept { |
| 501 | const_iterator it; |
| 502 | it.pos = this->data(); |
| 503 | it.stride = this->stride(); |
| 504 | return it; |
| 505 | } |
| 506 | |
| 507 | template <typename T> |
| 508 | typename Vec<T>::const_iterator Vec<T>::end() const noexcept { |
| 509 | const_iterator it = this->begin(); |
| 510 | it.pos = static_cast<const uint8_t *>(it.pos) + it.stride * this->size(); |
| 511 | return it; |
| 512 | } |
| 513 | |
| 514 | // Internal API only intended for the cxxbridge code generator. |
| 515 | template <typename T> |
| 516 | Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {} |
| 517 | #endif // CXXBRIDGE03_RUST_VEC |
| 518 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 519 | } // namespace cxxbridge03 |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 520 | } // namespace rust |