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