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