| 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 | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 7 | #include <string> |
| David Tolnay | f629237 | 2020-03-01 21:09:11 -0800 | [diff] [blame] | 8 | #include <type_traits> |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 9 | #include <utility> |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 10 | #include <vector> |
| David Tolnay | 59b5ba1 | 2020-04-10 11:32:19 -0700 | [diff] [blame] | 11 | #if defined(_WIN32) |
| 12 | #include <BaseTsd.h> |
| 13 | #endif |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 14 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 15 | namespace rust { |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 16 | inline namespace cxxbridge03 { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 17 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 18 | #ifndef CXXBRIDGE03_RUST_BITCOPY |
| 19 | #define CXXBRIDGE03_RUST_BITCOPY |
| David Tolnay | 313b10e | 2020-04-25 16:30:51 -0700 | [diff] [blame] | 20 | struct unsafe_bitcopy_t { |
| 21 | explicit unsafe_bitcopy_t() = default; |
| 22 | }; |
| 23 | constexpr unsafe_bitcopy_t unsafe_bitcopy{}; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 24 | #endif // CXXBRIDGE03_RUST_BITCOPY |
| David Tolnay | d1e2efc | 2020-03-03 22:25:43 -0800 | [diff] [blame] | 25 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 26 | #ifndef CXXBRIDGE03_RUST_STRING |
| 27 | #define CXXBRIDGE03_RUST_STRING |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 28 | class String final { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 29 | public: |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 30 | String() noexcept; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 31 | String(const String &) noexcept; |
| 32 | String(String &&) noexcept; |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 33 | ~String() noexcept; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 34 | |
| 35 | String(const std::string &); |
| 36 | String(const char *); |
| 37 | |
| 38 | String &operator=(const String &) noexcept; |
| 39 | String &operator=(String &&) noexcept; |
| 40 | |
| David Tolnay | 404d689 | 2020-03-01 20:19:41 -0800 | [diff] [blame] | 41 | explicit operator std::string() const; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 42 | |
| 43 | // Note: no null terminator. |
| 44 | const char *data() const noexcept; |
| 45 | size_t size() const noexcept; |
| 46 | size_t length() const noexcept; |
| 47 | |
| David Tolnay | d1e2efc | 2020-03-03 22:25:43 -0800 | [diff] [blame] | 48 | // Internal API only intended for the cxxbridge code generator. |
| 49 | String(unsafe_bitcopy_t, const String &) noexcept; |
| 50 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 51 | private: |
| 52 | // Size and alignment statically verified by rust_string.rs. |
| 53 | std::array<uintptr_t, 3> repr; |
| 54 | }; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 55 | #endif // CXXBRIDGE03_RUST_STRING |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 56 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 57 | #ifndef CXXBRIDGE03_RUST_STR |
| 58 | #define CXXBRIDGE03_RUST_STR |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 59 | class Str final { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 60 | public: |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 61 | Str() noexcept; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 62 | Str(const Str &) noexcept; |
| 63 | |
| David Tolnay | 851677c | 2020-03-01 23:49:46 -0800 | [diff] [blame] | 64 | Str(const std::string &); |
| 65 | Str(const char *); |
| 66 | Str(std::string &&) = delete; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 67 | |
| 68 | Str &operator=(Str) noexcept; |
| 69 | |
| David Tolnay | 404d689 | 2020-03-01 20:19:41 -0800 | [diff] [blame] | 70 | explicit operator std::string() const; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 71 | |
| 72 | // Note: no null terminator. |
| 73 | const char *data() const noexcept; |
| 74 | size_t size() const noexcept; |
| 75 | size_t length() const noexcept; |
| 76 | |
| 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. |
| 81 | struct Repr { |
| 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 | baae443 | 2020-03-01 20:20:10 -0800 | [diff] [blame] | 86 | explicit operator Repr() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 87 | |
| 88 | private: |
| 89 | Repr repr; |
| 90 | }; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 91 | #endif // CXXBRIDGE03_RUST_STR |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 92 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 93 | #ifndef CXXBRIDGE03_RUST_SLICE |
| 94 | #define CXXBRIDGE03_RUST_SLICE |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 95 | template <typename T> |
| 96 | class Slice final { |
| 97 | public: |
| 98 | Slice() noexcept : repr(Repr{reinterpret_cast<const T *>(this), 0}) {} |
| 99 | Slice(const Slice<T> &) noexcept = default; |
| 100 | |
| David Tolnay | 8c54eec | 2020-05-12 20:08:20 -0700 | [diff] [blame] | 101 | Slice(const T *s, size_t size) noexcept : repr(Repr{s, size}) {} |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 102 | |
| 103 | Slice &operator=(Slice<T> other) noexcept { |
| 104 | this->repr = other.repr; |
| 105 | return *this; |
| 106 | } |
| 107 | |
| 108 | const T *data() const noexcept { return this->repr.ptr; } |
| 109 | size_t size() const noexcept { return this->repr.len; } |
| 110 | size_t length() const noexcept { return this->repr.len; } |
| 111 | |
| 112 | // Repr is PRIVATE; must not be used other than by our generated code. |
| 113 | // |
| 114 | // At present this class is only used for &[u8] slices. |
| 115 | // Not necessarily ABI compatible with &[u8]. Codegen will translate to |
| David Tolnay | e710af1 | 2020-04-14 16:31:54 -0700 | [diff] [blame] | 116 | // cxx::rust_sliceu8::RustSliceU8 which matches this layout. |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 117 | struct Repr { |
| 118 | const T *ptr; |
| 119 | size_t len; |
| 120 | }; |
| 121 | Slice(Repr repr_) noexcept : repr(repr_) {} |
| 122 | explicit operator Repr() noexcept { return this->repr; } |
| 123 | |
| 124 | private: |
| 125 | Repr repr; |
| 126 | }; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 127 | #endif // CXXBRIDGE03_RUST_SLICE |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 128 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 129 | #ifndef CXXBRIDGE03_RUST_BOX |
| 130 | #define CXXBRIDGE03_RUST_BOX |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 131 | template <typename T> |
| 132 | class Box final { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 133 | public: |
| David Tolnay | f629237 | 2020-03-01 21:09:11 -0800 | [diff] [blame] | 134 | using value_type = T; |
| David Tolnay | 9706a51 | 2020-04-24 17:09:01 -0700 | [diff] [blame] | 135 | using const_pointer = |
| 136 | typename std::add_pointer<typename std::add_const<T>::type>::type; |
| 137 | using pointer = typename std::add_pointer<T>::type; |
| David Tolnay | f629237 | 2020-03-01 21:09:11 -0800 | [diff] [blame] | 138 | |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 139 | Box(const Box &other) : Box(*other) {} |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 140 | Box(Box &&other) noexcept : ptr(other.ptr) { other.ptr = nullptr; } |
| David Tolnay | 7db7dad | 2020-04-11 14:12:49 -0700 | [diff] [blame] | 141 | explicit Box(const T &val) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 142 | this->uninit(); |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 143 | ::new (this->ptr) T(val); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 144 | } |
| David Tolnay | 7db7dad | 2020-04-11 14:12:49 -0700 | [diff] [blame] | 145 | explicit Box(T &&val) { |
| David Tolnay | 47b3cf2 | 2020-04-11 00:54:39 -0700 | [diff] [blame] | 146 | this->uninit(); |
| 147 | ::new (this->ptr) T(std::move(val)); |
| 148 | } |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 149 | Box &operator=(const Box &other) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 150 | if (this != &other) { |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 151 | if (this->ptr) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 152 | **this = *other; |
| 153 | } else { |
| 154 | this->uninit(); |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 155 | ::new (this->ptr) T(*other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | return *this; |
| 159 | } |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 160 | Box &operator=(Box &&other) noexcept { |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 161 | if (this->ptr) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 162 | this->drop(); |
| 163 | } |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 164 | this->ptr = other.ptr; |
| 165 | other.ptr = nullptr; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 166 | return *this; |
| 167 | } |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 168 | ~Box() noexcept { |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 169 | if (this->ptr) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 170 | this->drop(); |
| 171 | } |
| 172 | } |
| 173 | |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 174 | const T *operator->() const noexcept { return this->ptr; } |
| 175 | const T &operator*() const noexcept { return *this->ptr; } |
| 176 | T *operator->() noexcept { return this->ptr; } |
| 177 | T &operator*() noexcept { return *this->ptr; } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 178 | |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 179 | template <typename... Fields> |
| 180 | static Box in_place(Fields &&... fields) { |
| David Tolnay | 7ce59fc | 2020-04-11 11:46:33 -0700 | [diff] [blame] | 181 | Box box; |
| 182 | box.uninit(); |
| 183 | ::new (box.ptr) T{std::forward<Fields>(fields)...}; |
| 184 | return box; |
| 185 | } |
| 186 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 187 | // Important: requires that `raw` came from an into_raw call. Do not pass a |
| 188 | // pointer from `new` or any other source. |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 189 | static Box from_raw(T *raw) noexcept { |
| 190 | Box box; |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 191 | box.ptr = raw; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 192 | return box; |
| 193 | } |
| 194 | |
| 195 | T *into_raw() noexcept { |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 196 | T *raw = this->ptr; |
| 197 | this->ptr = nullptr; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 198 | return raw; |
| 199 | } |
| 200 | |
| 201 | private: |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 202 | Box() noexcept {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 203 | void uninit() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 204 | void drop() noexcept; |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 205 | T *ptr; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 206 | }; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 207 | #endif // CXXBRIDGE03_RUST_BOX |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 208 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 209 | #ifndef CXXBRIDGE03_RUST_VEC |
| 210 | #define CXXBRIDGE03_RUST_VEC |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 211 | template <typename T> |
| 212 | class Vec final { |
| 213 | public: |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 214 | using value_type = T; |
| 215 | |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 216 | Vec() noexcept; |
| 217 | Vec(Vec &&other) noexcept { |
| 218 | this->repr = other.repr; |
| 219 | new (&other) Vec(); |
| 220 | } |
| David Tolnay | cb80057 | 2020-04-24 20:30:43 -0700 | [diff] [blame] | 221 | ~Vec() noexcept { this->drop(); } |
| 222 | |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 223 | Vec &operator=(Vec &&other) noexcept { |
| 224 | if (this != &other) { |
| 225 | this->drop(); |
| 226 | this->repr = other.repr; |
| 227 | new (&other) Vec(); |
| 228 | } |
| 229 | return *this; |
| 230 | } |
| 231 | |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 232 | size_t size() const noexcept; |
| David Tolnay | 465305b | 2020-04-24 16:48:18 -0700 | [diff] [blame] | 233 | bool empty() const noexcept { return size() == 0; } |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 234 | const T *data() const noexcept; |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 235 | |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 236 | class const_iterator { |
| 237 | public: |
| myronahn | da9be50 | 2020-04-29 05:47:23 +0700 | [diff] [blame] | 238 | using difference_type = ptrdiff_t; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 239 | using value_type = typename std::add_const<T>::type; |
| David Tolnay | 74dd379 | 2020-04-30 07:45:24 -0700 | [diff] [blame] | 240 | using pointer = |
| 241 | typename std::add_pointer<typename std::add_const<T>::type>::type; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 242 | using reference = typename std::add_lvalue_reference< |
| 243 | typename std::add_const<T>::type>::type; |
| myronahn | da9be50 | 2020-04-29 05:47:23 +0700 | [diff] [blame] | 244 | using iterator_category = std::forward_iterator_tag; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 245 | |
| David Tolnay | 1385ca4 | 2020-05-12 20:15:20 -0700 | [diff] [blame] | 246 | const T &operator*() const noexcept { |
| 247 | return *static_cast<const T *>(this->pos); |
| 248 | } |
| 249 | const T *operator->() const noexcept { |
| 250 | return static_cast<const T *>(this->pos); |
| 251 | } |
| 252 | const_iterator &operator++() noexcept { |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 253 | this->pos = static_cast<const uint8_t *>(this->pos) + this->stride; |
| 254 | return *this; |
| 255 | } |
| David Tolnay | 1385ca4 | 2020-05-12 20:15:20 -0700 | [diff] [blame] | 256 | const_iterator operator++(int) noexcept { |
| myronahn | da9be50 | 2020-04-29 05:47:23 +0700 | [diff] [blame] | 257 | auto ret = const_iterator(*this); |
| 258 | this->pos = static_cast<const uint8_t *>(this->pos) + this->stride; |
| 259 | return ret; |
| 260 | } |
| David Tolnay | 1385ca4 | 2020-05-12 20:15:20 -0700 | [diff] [blame] | 261 | bool operator==(const const_iterator &other) const noexcept { |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 262 | return this->pos == other.pos; |
| 263 | } |
| David Tolnay | 1385ca4 | 2020-05-12 20:15:20 -0700 | [diff] [blame] | 264 | bool operator!=(const const_iterator &other) const noexcept { |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 265 | return this->pos != other.pos; |
| 266 | } |
| 267 | |
| 268 | private: |
| 269 | friend class Vec; |
| 270 | const void *pos; |
| 271 | size_t stride; |
| 272 | }; |
| 273 | |
| 274 | const_iterator begin() const noexcept { |
| 275 | const_iterator it; |
| 276 | it.pos = this->data(); |
| 277 | it.stride = this->stride(); |
| 278 | return it; |
| 279 | } |
| 280 | const_iterator end() const noexcept { |
| 281 | const_iterator it = this->begin(); |
| 282 | it.pos = static_cast<const uint8_t *>(it.pos) + it.stride * this->size(); |
| 283 | return it; |
| 284 | } |
| 285 | |
| David Tolnay | 313b10e | 2020-04-25 16:30:51 -0700 | [diff] [blame] | 286 | // Internal API only intended for the cxxbridge code generator. |
| 287 | Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {} |
| 288 | |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 289 | private: |
| David Tolnay | 503d019 | 2020-04-24 22:18:56 -0700 | [diff] [blame] | 290 | static size_t stride() noexcept; |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 291 | void drop() noexcept; |
| 292 | |
| 293 | // Size and alignment statically verified by rust_vec.rs. |
| 294 | std::array<uintptr_t, 3> repr; |
| 295 | }; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 296 | #endif // CXXBRIDGE03_RUST_VEC |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 297 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 298 | #ifndef CXXBRIDGE03_RUST_FN |
| 299 | #define CXXBRIDGE03_RUST_FN |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 300 | template <typename Signature, bool Throws = false> |
| 301 | class Fn; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 302 | |
| 303 | template <typename Ret, typename... Args, bool Throws> |
| 304 | class Fn<Ret(Args...), Throws> { |
| 305 | public: |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 306 | Ret operator()(Args... args) const noexcept(!Throws); |
| 307 | Fn operator*() const noexcept; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 308 | |
| 309 | private: |
| 310 | Ret (*trampoline)(Args..., void *fn) noexcept(!Throws); |
| 311 | void *fn; |
| 312 | }; |
| 313 | |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 314 | template <typename Signature> |
| 315 | using TryFn = Fn<Signature, true>; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 316 | #endif // CXXBRIDGE03_RUST_FN |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 317 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 318 | #ifndef CXXBRIDGE03_RUST_ERROR |
| 319 | #define CXXBRIDGE03_RUST_ERROR |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 320 | class Error final : std::exception { |
| 321 | public: |
| 322 | Error(const Error &); |
| 323 | Error(Error &&) noexcept; |
| 324 | Error(Str::Repr) noexcept; |
| 325 | ~Error() noexcept; |
| 326 | const char *what() const noexcept override; |
| 327 | |
| 328 | private: |
| 329 | Str::Repr msg; |
| 330 | }; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 331 | #endif // CXXBRIDGE03_RUST_ERROR |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 332 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 333 | #ifndef CXXBRIDGE03_RUST_ISIZE |
| 334 | #define CXXBRIDGE03_RUST_ISIZE |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 335 | #if defined(_WIN32) |
| 336 | using isize = SSIZE_T; |
| 337 | #else |
| 338 | using isize = ssize_t; |
| 339 | #endif |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 340 | #endif // CXXBRIDGE03_RUST_ISIZE |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 341 | |
| David Tolnay | 851677c | 2020-03-01 23:49:46 -0800 | [diff] [blame] | 342 | std::ostream &operator<<(std::ostream &, const String &); |
| 343 | std::ostream &operator<<(std::ostream &, const Str &); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 344 | |
| David Tolnay | 3b0c988 | 2020-03-01 14:08:57 -0800 | [diff] [blame] | 345 | // Snake case aliases for use in code that uses this style for type names. |
| 346 | using string = String; |
| 347 | using str = Str; |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 348 | template <class T> |
| 349 | using box = Box<T>; |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 350 | using error = Error; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 351 | template <typename Signature, bool Throws = false> |
| 352 | using fn = Fn<Signature, Throws>; |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 353 | template <typename Signature> |
| 354 | using try_fn = TryFn<Signature>; |
| David Tolnay | 3b0c988 | 2020-03-01 14:08:57 -0800 | [diff] [blame] | 355 | |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 356 | template <typename Ret, typename... Args, bool Throws> |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 357 | Ret Fn<Ret(Args...), Throws>::operator()(Args... args) const noexcept(!Throws) { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 358 | return (*this->trampoline)(std::move(args)..., this->fn); |
| 359 | } |
| 360 | |
| David Tolnay | a23129c | 2020-04-08 20:08:21 -0700 | [diff] [blame] | 361 | template <typename Ret, typename... Args, bool Throws> |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 362 | Fn<Ret(Args...), Throws> Fn<Ret(Args...), Throws>::operator*() const noexcept { |
| David Tolnay | a23129c | 2020-04-08 20:08:21 -0700 | [diff] [blame] | 363 | return *this; |
| 364 | } |
| 365 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 366 | } // namespace cxxbridge03 |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 367 | } // namespace rust |