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