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