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