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