| 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 |
| 218 | #define CXXBRIDGE05_RUST_FN |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 219 | template <typename Signature, bool Throws = false> |
| 220 | class Fn; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 221 | |
| 222 | template <typename Ret, typename... Args, bool Throws> |
| David Tolnay | 4852122 | 2020-10-31 14:59:42 -0700 | [diff] [blame] | 223 | class Fn<Ret(Args...), Throws> final { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 224 | public: |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 225 | Ret operator()(Args... args) const noexcept(!Throws); |
| 226 | Fn operator*() const noexcept; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 227 | |
| 228 | private: |
| 229 | Ret (*trampoline)(Args..., void *fn) noexcept(!Throws); |
| 230 | void *fn; |
| 231 | }; |
| 232 | |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 233 | template <typename Signature> |
| 234 | using TryFn = Fn<Signature, true>; |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 235 | #endif // CXXBRIDGE05_RUST_FN |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 236 | |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 237 | #ifndef CXXBRIDGE05_RUST_ERROR |
| 238 | #define CXXBRIDGE05_RUST_ERROR |
| David Tolnay | e4fa873 | 2020-09-08 15:04:56 -0700 | [diff] [blame] | 239 | class Error final : public std::exception { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 240 | public: |
| 241 | Error(const Error &); |
| 242 | Error(Error &&) noexcept; |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 243 | ~Error() noexcept; |
| David Tolnay | 7c6ac71 | 2020-10-31 17:22:28 -0700 | [diff] [blame] | 244 | |
| 245 | Error &operator=(const Error &); |
| David Tolnay | 1549106 | 2020-10-31 17:25:13 -0700 | [diff] [blame] | 246 | Error &operator=(Error &&) noexcept; |
| David Tolnay | 7c6ac71 | 2020-10-31 17:22:28 -0700 | [diff] [blame] | 247 | |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 248 | const char *what() const noexcept override; |
| 249 | |
| 250 | private: |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 251 | Error() noexcept = default; |
| David Tolnay | 84ddf9e | 2020-10-31 15:36:48 -0700 | [diff] [blame] | 252 | friend impl<Error>; |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 253 | const char *msg; |
| 254 | size_t len; |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 255 | }; |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 256 | #endif // CXXBRIDGE05_RUST_ERROR |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 257 | |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 258 | #ifndef CXXBRIDGE05_RUST_ISIZE |
| 259 | #define CXXBRIDGE05_RUST_ISIZE |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 260 | #if defined(_WIN32) |
| 261 | using isize = SSIZE_T; |
| 262 | #else |
| 263 | using isize = ssize_t; |
| 264 | #endif |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 265 | #endif // CXXBRIDGE05_RUST_ISIZE |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 266 | |
| David Tolnay | 851677c | 2020-03-01 23:49:46 -0800 | [diff] [blame] | 267 | std::ostream &operator<<(std::ostream &, const String &); |
| 268 | std::ostream &operator<<(std::ostream &, const Str &); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 269 | |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame^] | 270 | // IsRelocatable<T> is used in assertions that a C++ type passed by value |
| 271 | // between Rust and C++ is soundly relocatable by Rust. |
| 272 | // |
| 273 | // There may be legitimate reasons to opt out of the check for support of types |
| 274 | // that the programmer knows are soundly Rust-movable despite not being |
| 275 | // recognized as such by the C++ type system due to a move constructor or |
| 276 | // destructor. To opt out of the relocatability check, do either of the |
| 277 | // following things in any header used by `include!` in the bridge. |
| 278 | // |
| 279 | // --- if you define the type: |
| 280 | // struct MyType { |
| 281 | // ... |
| 282 | // + using IsRelocatable = std::true_type; |
| 283 | // }; |
| 284 | // |
| 285 | // --- otherwise: |
| 286 | // + template <> |
| 287 | // + struct rust::IsRelocatable<MyType> : std::true_type {}; |
| 288 | template <typename T> |
| 289 | struct IsRelocatable; |
| 290 | |
| David Tolnay | 3b0c988 | 2020-03-01 14:08:57 -0800 | [diff] [blame] | 291 | // Snake case aliases for use in code that uses this style for type names. |
| 292 | using string = String; |
| 293 | using str = Str; |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 294 | template <class T> |
| David Tolnay | 38c8764 | 2020-09-06 22:18:08 -0700 | [diff] [blame] | 295 | using slice = Slice<T>; |
| 296 | template <class T> |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 297 | using box = Box<T>; |
| David Tolnay | 38c8764 | 2020-09-06 22:18:08 -0700 | [diff] [blame] | 298 | template <class T> |
| 299 | using vec = Vec<T>; |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 300 | using error = Error; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 301 | template <typename Signature, bool Throws = false> |
| 302 | using fn = Fn<Signature, Throws>; |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 303 | template <typename Signature> |
| 304 | using try_fn = TryFn<Signature>; |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame^] | 305 | template <typename T> |
| 306 | using is_relocatable = IsRelocatable<T>; |
| David Tolnay | 3b0c988 | 2020-03-01 14:08:57 -0800 | [diff] [blame] | 307 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 308 | |
| 309 | |
| 310 | //////////////////////////////////////////////////////////////////////////////// |
| 311 | /// end public API, begin implementation details |
| 312 | |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 313 | #ifndef CXXBRIDGE05_PANIC |
| 314 | #define CXXBRIDGE05_PANIC |
| David Tolnay | 521d99d | 2020-08-26 20:45:40 -0700 | [diff] [blame] | 315 | template <typename Exception> |
| 316 | void panic [[noreturn]] (const char *msg); |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 317 | #endif // CXXBRIDGE05_PANIC |
| David Tolnay | 521d99d | 2020-08-26 20:45:40 -0700 | [diff] [blame] | 318 | |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 319 | template <typename Ret, typename... Args, bool Throws> |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 320 | Ret Fn<Ret(Args...), Throws>::operator()(Args... args) const noexcept(!Throws) { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 321 | return (*this->trampoline)(std::move(args)..., this->fn); |
| 322 | } |
| 323 | |
| David Tolnay | a23129c | 2020-04-08 20:08:21 -0700 | [diff] [blame] | 324 | template <typename Ret, typename... Args, bool Throws> |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 325 | Fn<Ret(Args...), Throws> Fn<Ret(Args...), Throws>::operator*() const noexcept { |
| David Tolnay | a23129c | 2020-04-08 20:08:21 -0700 | [diff] [blame] | 326 | return *this; |
| 327 | } |
| 328 | |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 329 | #ifndef CXXBRIDGE05_RUST_BITCOPY |
| 330 | #define CXXBRIDGE05_RUST_BITCOPY |
| David Tolnay | 4852122 | 2020-10-31 14:59:42 -0700 | [diff] [blame] | 331 | struct unsafe_bitcopy_t final { |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 332 | explicit unsafe_bitcopy_t() = default; |
| 333 | }; |
| 334 | |
| 335 | constexpr unsafe_bitcopy_t unsafe_bitcopy{}; |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 336 | #endif // CXXBRIDGE05_RUST_BITCOPY |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 337 | |
| David Tolnay | 5b1ee1f | 2020-10-31 18:21:39 -0700 | [diff] [blame] | 338 | #ifndef CXXBRIDGE05_RUST_STR |
| 339 | #define CXXBRIDGE05_RUST_STR |
| 340 | inline const char *Str::data() const noexcept { return this->ptr; } |
| 341 | |
| 342 | inline size_t Str::size() const noexcept { return this->len; } |
| 343 | |
| 344 | inline size_t Str::length() const noexcept { return this->len; } |
| 345 | #endif // CXXBRIDGE05_RUST_STR |
| 346 | |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 347 | #ifndef CXXBRIDGE05_RUST_SLICE |
| 348 | #define CXXBRIDGE05_RUST_SLICE |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 349 | template <typename T> |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 350 | Slice<T>::Slice() noexcept : ptr(reinterpret_cast<const T *>(this)), len(0) {} |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 351 | |
| 352 | template <typename T> |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 353 | 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] | 354 | |
| 355 | template <typename T> |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 356 | const T *Slice<T>::data() const noexcept { |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 357 | return this->ptr; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | template <typename T> |
| 361 | size_t Slice<T>::size() const noexcept { |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 362 | return this->len; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | template <typename T> |
| 366 | size_t Slice<T>::length() const noexcept { |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 367 | return this->len; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 368 | } |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 369 | #endif // CXXBRIDGE05_RUST_SLICE |
| 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 | #ifndef CXXBRIDGE05_RUST_BOX |
| 372 | #define CXXBRIDGE05_RUST_BOX |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 373 | template <typename T> |
| 374 | Box<T>::Box(const Box &other) : Box(*other) {} |
| 375 | |
| 376 | template <typename T> |
| 377 | Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) { |
| 378 | other.ptr = nullptr; |
| 379 | } |
| 380 | |
| 381 | template <typename T> |
| 382 | Box<T>::Box(const T &val) { |
| 383 | this->uninit(); |
| 384 | ::new (this->ptr) T(val); |
| 385 | } |
| 386 | |
| 387 | template <typename T> |
| 388 | Box<T>::Box(T &&val) { |
| 389 | this->uninit(); |
| 390 | ::new (this->ptr) T(std::move(val)); |
| 391 | } |
| 392 | |
| 393 | template <typename T> |
| 394 | Box<T>::~Box() noexcept { |
| 395 | if (this->ptr) { |
| 396 | this->drop(); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | template <typename T> |
| 401 | Box<T> &Box<T>::operator=(const Box &other) { |
| 402 | if (this != &other) { |
| 403 | if (this->ptr) { |
| 404 | **this = *other; |
| 405 | } else { |
| 406 | this->uninit(); |
| 407 | ::new (this->ptr) T(*other); |
| 408 | } |
| 409 | } |
| 410 | return *this; |
| 411 | } |
| 412 | |
| 413 | template <typename T> |
| 414 | Box<T> &Box<T>::operator=(Box &&other) noexcept { |
| 415 | if (this->ptr) { |
| 416 | this->drop(); |
| 417 | } |
| 418 | this->ptr = other.ptr; |
| 419 | other.ptr = nullptr; |
| 420 | return *this; |
| 421 | } |
| 422 | |
| 423 | template <typename T> |
| 424 | const T *Box<T>::operator->() const noexcept { |
| 425 | return this->ptr; |
| 426 | } |
| 427 | |
| 428 | template <typename T> |
| 429 | const T &Box<T>::operator*() const noexcept { |
| 430 | return *this->ptr; |
| 431 | } |
| 432 | |
| 433 | template <typename T> |
| 434 | T *Box<T>::operator->() noexcept { |
| 435 | return this->ptr; |
| 436 | } |
| 437 | |
| 438 | template <typename T> |
| 439 | T &Box<T>::operator*() noexcept { |
| 440 | return *this->ptr; |
| 441 | } |
| 442 | |
| 443 | template <typename T> |
| 444 | template <typename... Fields> |
| 445 | Box<T> Box<T>::in_place(Fields &&... fields) { |
| 446 | Box box; |
| 447 | box.uninit(); |
| 448 | ::new (box.ptr) T{std::forward<Fields>(fields)...}; |
| 449 | return box; |
| 450 | } |
| 451 | |
| 452 | template <typename T> |
| 453 | Box<T> Box<T>::from_raw(T *raw) noexcept { |
| 454 | Box box; |
| 455 | box.ptr = raw; |
| 456 | return box; |
| 457 | } |
| 458 | |
| 459 | template <typename T> |
| 460 | T *Box<T>::into_raw() noexcept { |
| 461 | T *raw = this->ptr; |
| 462 | this->ptr = nullptr; |
| 463 | return raw; |
| 464 | } |
| 465 | |
| 466 | template <typename T> |
| 467 | Box<T>::Box() noexcept {} |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 468 | #endif // CXXBRIDGE05_RUST_BOX |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 469 | |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 470 | #ifndef CXXBRIDGE05_RUST_VEC |
| 471 | #define CXXBRIDGE05_RUST_VEC |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 472 | template <typename T> |
| 473 | Vec<T>::Vec(Vec &&other) noexcept { |
| 474 | this->repr = other.repr; |
| 475 | new (&other) Vec(); |
| 476 | } |
| 477 | |
| 478 | template <typename T> |
| 479 | Vec<T>::~Vec() noexcept { |
| 480 | this->drop(); |
| 481 | } |
| 482 | |
| 483 | template <typename T> |
| 484 | Vec<T> &Vec<T>::operator=(Vec &&other) noexcept { |
| 485 | if (this != &other) { |
| 486 | this->drop(); |
| 487 | this->repr = other.repr; |
| 488 | new (&other) Vec(); |
| 489 | } |
| 490 | return *this; |
| 491 | } |
| 492 | |
| 493 | template <typename T> |
| 494 | bool Vec<T>::empty() const noexcept { |
| 495 | return size() == 0; |
| 496 | } |
| 497 | |
| 498 | template <typename T> |
| Stephen Crane | 9e48d5b | 2020-08-21 12:17:02 -0700 | [diff] [blame] | 499 | const T &Vec<T>::operator[](size_t n) const noexcept { |
| 500 | auto data = reinterpret_cast<const char *>(this->data()); |
| 501 | return *reinterpret_cast<const T *>(data + n * this->stride()); |
| 502 | } |
| 503 | |
| 504 | template <typename T> |
| 505 | const T &Vec<T>::at(size_t n) const { |
| David Tolnay | 8e1e6ac | 2020-08-26 20:51:43 -0700 | [diff] [blame] | 506 | if (n >= this->size()) { |
| 507 | panic<std::out_of_range>("rust::Vec index out of range"); |
| 508 | } |
| Stephen Crane | 9e48d5b | 2020-08-21 12:17:02 -0700 | [diff] [blame] | 509 | return (*this)[n]; |
| 510 | } |
| 511 | |
| 512 | template <typename T> |
| 513 | const T &Vec<T>::front() const { |
| 514 | return (*this)[0]; |
| 515 | } |
| 516 | |
| 517 | template <typename T> |
| 518 | const T &Vec<T>::back() const { |
| David Tolnay | b10c4bc | 2020-08-26 21:55:29 -0700 | [diff] [blame] | 519 | return (*this)[this->size() - 1]; |
| Stephen Crane | 9e48d5b | 2020-08-21 12:17:02 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | template <typename T> |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 523 | const T &Vec<T>::const_iterator::operator*() const noexcept { |
| 524 | return *static_cast<const T *>(this->pos); |
| 525 | } |
| 526 | |
| 527 | template <typename T> |
| 528 | const T *Vec<T>::const_iterator::operator->() const noexcept { |
| 529 | return static_cast<const T *>(this->pos); |
| 530 | } |
| 531 | |
| 532 | template <typename T> |
| 533 | typename Vec<T>::const_iterator &Vec<T>::const_iterator::operator++() noexcept { |
| 534 | this->pos = static_cast<const uint8_t *>(this->pos) + this->stride; |
| 535 | return *this; |
| 536 | } |
| 537 | |
| 538 | template <typename T> |
| 539 | typename Vec<T>::const_iterator |
| 540 | Vec<T>::const_iterator::operator++(int) noexcept { |
| 541 | auto ret = const_iterator(*this); |
| 542 | this->pos = static_cast<const uint8_t *>(this->pos) + this->stride; |
| 543 | return ret; |
| 544 | } |
| 545 | |
| 546 | template <typename T> |
| David Tolnay | b10c4bc | 2020-08-26 21:55:29 -0700 | [diff] [blame] | 547 | bool Vec<T>::const_iterator::operator==( |
| 548 | const const_iterator &other) const noexcept { |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 549 | return this->pos == other.pos; |
| 550 | } |
| 551 | |
| 552 | template <typename T> |
| David Tolnay | b10c4bc | 2020-08-26 21:55:29 -0700 | [diff] [blame] | 553 | bool Vec<T>::const_iterator::operator!=( |
| 554 | const const_iterator &other) const noexcept { |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 555 | return this->pos != other.pos; |
| 556 | } |
| 557 | |
| 558 | template <typename T> |
| 559 | typename Vec<T>::const_iterator Vec<T>::begin() const noexcept { |
| 560 | const_iterator it; |
| 561 | it.pos = this->data(); |
| 562 | it.stride = this->stride(); |
| 563 | return it; |
| 564 | } |
| 565 | |
| 566 | template <typename T> |
| 567 | typename Vec<T>::const_iterator Vec<T>::end() const noexcept { |
| 568 | const_iterator it = this->begin(); |
| 569 | it.pos = static_cast<const uint8_t *>(it.pos) + it.stride * this->size(); |
| 570 | return it; |
| 571 | } |
| 572 | |
| 573 | // Internal API only intended for the cxxbridge code generator. |
| 574 | template <typename T> |
| 575 | 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] | 576 | #endif // CXXBRIDGE05_RUST_VEC |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 577 | |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame^] | 578 | #ifndef CXXBRIDGE05_RELOCATABLE |
| 579 | #define CXXBRIDGE05_RELOCATABLE |
| 580 | namespace detail { |
| 581 | template <typename... Ts> |
| 582 | struct make_void { |
| 583 | using type = void; |
| 584 | }; |
| 585 | |
| 586 | template <typename... Ts> |
| 587 | using void_t = typename make_void<Ts...>::type; |
| 588 | |
| 589 | template <typename Void, template <typename...> class, typename...> |
| 590 | struct detect : std::false_type {}; |
| 591 | template <template <typename...> class T, typename... A> |
| 592 | struct detect<void_t<T<A...>>, T, A...> : std::true_type {}; |
| 593 | |
| 594 | template <template <typename...> class T, typename... A> |
| 595 | using is_detected = detect<void, T, A...>; |
| 596 | |
| 597 | template <typename T> |
| 598 | using detect_IsRelocatable = typename T::IsRelocatable; |
| 599 | |
| 600 | template <typename T> |
| 601 | struct get_IsRelocatable |
| 602 | : std::is_same<typename T::IsRelocatable, std::true_type> {}; |
| 603 | } // namespace detail |
| 604 | |
| 605 | template <typename T> |
| 606 | struct IsRelocatable |
| 607 | : std::conditional< |
| 608 | detail::is_detected<detail::detect_IsRelocatable, T>::value, |
| 609 | detail::get_IsRelocatable<T>, |
| 610 | std::integral_constant< |
| 611 | bool, std::is_trivially_move_constructible<T>::value && |
| 612 | std::is_trivially_destructible<T>::value>>::type {}; |
| 613 | #endif // CXXBRIDGE05_RELOCATABLE |
| 614 | |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 615 | } // namespace cxxbridge05 |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 616 | } // namespace rust |