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