| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1 | #pragma once |
| David Tolnay | f799b37 | 2020-11-29 23:57:42 -0800 | [diff] [blame] | 2 | #include <algorithm> |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 3 | #include <array> |
| David Tolnay | 30430f1 | 2020-03-19 20:49:00 -0700 | [diff] [blame] | 4 | #include <cstddef> |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 5 | #include <cstdint> |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 6 | #include <exception> |
| David Tolnay | f799b37 | 2020-11-29 23:57:42 -0800 | [diff] [blame] | 7 | #include <initializer_list> |
| David Tolnay | 001102a | 2020-03-01 20:05:04 -0800 | [diff] [blame] | 8 | #include <iosfwd> |
| David Tolnay | d1df4c7 | 2020-11-25 20:38:05 -0800 | [diff] [blame] | 9 | #include <iterator> |
| David Tolnay | 0ecd05a | 2020-07-29 16:32:03 -0700 | [diff] [blame] | 10 | #include <new> |
| Stephen Crane | 9e48d5b | 2020-08-21 12:17:02 -0700 | [diff] [blame] | 11 | #include <stdexcept> |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 12 | #include <string> |
| David Tolnay | f629237 | 2020-03-01 21:09:11 -0800 | [diff] [blame] | 13 | #include <type_traits> |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 14 | #include <utility> |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 15 | #include <vector> |
| David Tolnay | 59b5ba1 | 2020-04-10 11:32:19 -0700 | [diff] [blame] | 16 | #if defined(_WIN32) |
| David Tolnay | da38b7c | 2020-09-16 11:50:04 -0400 | [diff] [blame] | 17 | #include <basetsd.h> |
| David Tolnay | 59b5ba1 | 2020-04-10 11:32:19 -0700 | [diff] [blame] | 18 | #endif |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 19 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 20 | namespace rust { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 21 | inline namespace cxxbridge1 { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 22 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 23 | struct unsafe_bitcopy_t; |
| David Tolnay | d1e2efc | 2020-03-03 22:25:43 -0800 | [diff] [blame] | 24 | |
| David Tolnay | 84ddf9e | 2020-10-31 15:36:48 -0700 | [diff] [blame] | 25 | namespace { |
| 26 | template <typename T> |
| 27 | class impl; |
| 28 | } |
| 29 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 30 | #ifndef CXXBRIDGE1_RUST_STRING |
| 31 | #define CXXBRIDGE1_RUST_STRING |
| David Tolnay | e468f05 | 2020-11-29 19:39:35 -0800 | [diff] [blame] | 32 | // https://cxx.rs/binding/string.html |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 33 | class String final { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 34 | public: |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 35 | String() noexcept; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 36 | String(const String &) noexcept; |
| 37 | String(String &&) noexcept; |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 38 | ~String() noexcept; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 39 | |
| 40 | String(const std::string &); |
| 41 | String(const char *); |
| David Tolnay | c2bbd95 | 2020-07-29 18:15:26 -0700 | [diff] [blame] | 42 | String(const char *, size_t); |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 43 | |
| 44 | String &operator=(const String &) noexcept; |
| 45 | String &operator=(String &&) noexcept; |
| 46 | |
| David Tolnay | 404d689 | 2020-03-01 20:19:41 -0800 | [diff] [blame] | 47 | explicit operator std::string() const; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 48 | |
| 49 | // Note: no null terminator. |
| 50 | const char *data() const noexcept; |
| 51 | size_t size() const noexcept; |
| 52 | size_t length() const noexcept; |
| 53 | |
| David Tolnay | ff7f5fb | 2020-11-25 20:50:32 -0800 | [diff] [blame] | 54 | using iterator = char *; |
| 55 | iterator begin() noexcept; |
| 56 | iterator end() noexcept; |
| 57 | |
| 58 | using const_iterator = const char *; |
| 59 | const_iterator begin() const noexcept; |
| 60 | const_iterator end() const noexcept; |
| 61 | const_iterator cbegin() const noexcept; |
| 62 | const_iterator cend() const noexcept; |
| 63 | |
| David Tolnay | ff86dce | 2020-11-29 19:45:13 -0800 | [diff] [blame] | 64 | bool operator==(const String &) const noexcept; |
| 65 | bool operator!=(const String &) const noexcept; |
| 66 | bool operator<(const String &) const noexcept; |
| 67 | bool operator<=(const String &) const noexcept; |
| 68 | bool operator>(const String &) const noexcept; |
| 69 | bool operator>=(const String &) const noexcept; |
| 70 | |
| David Tolnay | d1e2efc | 2020-03-03 22:25:43 -0800 | [diff] [blame] | 71 | // Internal API only intended for the cxxbridge code generator. |
| 72 | String(unsafe_bitcopy_t, const String &) noexcept; |
| 73 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 74 | private: |
| 75 | // Size and alignment statically verified by rust_string.rs. |
| 76 | std::array<uintptr_t, 3> repr; |
| 77 | }; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 78 | #endif // CXXBRIDGE1_RUST_STRING |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 79 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 80 | #ifndef CXXBRIDGE1_RUST_STR |
| David Tolnay | e468f05 | 2020-11-29 19:39:35 -0800 | [diff] [blame] | 81 | // https://cxx.rs/binding/str.html |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 82 | class Str final { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 83 | public: |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 84 | Str() noexcept; |
| David Tolnay | 828e513 | 2020-11-29 20:40:40 -0800 | [diff] [blame] | 85 | Str(const String &) noexcept; |
| David Tolnay | 851677c | 2020-03-01 23:49:46 -0800 | [diff] [blame] | 86 | Str(const std::string &); |
| 87 | Str(const char *); |
| David Tolnay | 894c5e4 | 2020-07-29 18:20:00 -0700 | [diff] [blame] | 88 | Str(const char *, size_t); |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 89 | |
| David Tolnay | 2d7f117 | 2020-10-31 17:58:31 -0700 | [diff] [blame] | 90 | Str &operator=(const Str &) noexcept = default; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 91 | |
| David Tolnay | 404d689 | 2020-03-01 20:19:41 -0800 | [diff] [blame] | 92 | explicit operator std::string() const; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 93 | |
| 94 | // Note: no null terminator. |
| 95 | const char *data() const noexcept; |
| 96 | size_t size() const noexcept; |
| 97 | size_t length() const noexcept; |
| 98 | |
| David Tolnay | de9a5b1 | 2020-10-31 12:15:43 -0700 | [diff] [blame] | 99 | // Important in order for System V ABI to pass in registers. |
| 100 | Str(const Str &) noexcept = default; |
| 101 | ~Str() noexcept = default; |
| 102 | |
| David Tolnay | ff7f5fb | 2020-11-25 20:50:32 -0800 | [diff] [blame] | 103 | using iterator = const char *; |
| 104 | using const_iterator = const char *; |
| 105 | const_iterator begin() const noexcept; |
| 106 | const_iterator end() const noexcept; |
| 107 | const_iterator cbegin() const noexcept; |
| 108 | const_iterator cend() const noexcept; |
| 109 | |
| David Tolnay | ff86dce | 2020-11-29 19:45:13 -0800 | [diff] [blame] | 110 | bool operator==(const Str &) const noexcept; |
| 111 | bool operator!=(const Str &) const noexcept; |
| 112 | bool operator<(const Str &) const noexcept; |
| 113 | bool operator<=(const Str &) const noexcept; |
| 114 | bool operator>(const Str &) const noexcept; |
| 115 | bool operator>=(const Str &) const noexcept; |
| 116 | |
| David Tolnay | 5df1f06 | 2020-10-31 12:31:10 -0700 | [diff] [blame] | 117 | private: |
| David Tolnay | 0356d33 | 2020-10-31 19:46:41 -0700 | [diff] [blame] | 118 | friend impl<Str>; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 119 | // Not necessarily ABI compatible with &str. Codegen will translate to |
| 120 | // cxx::rust_str::RustStr which matches this layout. |
| David Tolnay | 5df1f06 | 2020-10-31 12:31:10 -0700 | [diff] [blame] | 121 | const char *ptr; |
| 122 | size_t len; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 123 | }; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 124 | #endif // CXXBRIDGE1_RUST_STR |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 125 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 126 | #ifndef CXXBRIDGE1_RUST_SLICE |
| David Tolnay | c5629f0 | 2020-11-23 18:32:46 -0800 | [diff] [blame] | 127 | namespace detail { |
| David Tolnay | ee9b9ee | 2020-11-25 08:28:50 -0800 | [diff] [blame] | 128 | template <bool> |
| David Tolnay | c5629f0 | 2020-11-23 18:32:46 -0800 | [diff] [blame] | 129 | struct copy_assignable_if {}; |
| David Tolnay | ce29823 | 2020-11-11 10:08:54 -0800 | [diff] [blame] | 130 | |
| David Tolnay | c5629f0 | 2020-11-23 18:32:46 -0800 | [diff] [blame] | 131 | template <> |
| 132 | struct copy_assignable_if<false> { |
| 133 | copy_assignable_if() noexcept = default; |
| 134 | copy_assignable_if(const copy_assignable_if &) noexcept = default; |
| 135 | copy_assignable_if &operator=(const copy_assignable_if &) noexcept = delete; |
| 136 | copy_assignable_if &operator=(copy_assignable_if &&) noexcept = default; |
| 137 | }; |
| 138 | } // namespace detail |
| 139 | |
| David Tolnay | e468f05 | 2020-11-29 19:39:35 -0800 | [diff] [blame] | 140 | // https://cxx.rs/binding/slice.html |
| David Tolnay | c5629f0 | 2020-11-23 18:32:46 -0800 | [diff] [blame] | 141 | template <typename T> |
| 142 | class Slice final |
| 143 | : private detail::copy_assignable_if<std::is_const<T>::value> { |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 144 | public: |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 145 | Slice() noexcept; |
| David Tolnay | ce29823 | 2020-11-11 10:08:54 -0800 | [diff] [blame] | 146 | Slice(T *, size_t count) noexcept; |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 147 | |
| David Tolnay | 2d7f117 | 2020-10-31 17:58:31 -0700 | [diff] [blame] | 148 | Slice &operator=(const Slice<T> &) noexcept = default; |
| David Tolnay | c5629f0 | 2020-11-23 18:32:46 -0800 | [diff] [blame] | 149 | Slice &operator=(Slice<T> &&) noexcept = default; |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 150 | |
| David Tolnay | ce29823 | 2020-11-11 10:08:54 -0800 | [diff] [blame] | 151 | T *data() const noexcept; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 152 | size_t size() const noexcept; |
| 153 | size_t length() const noexcept; |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 154 | |
| David Tolnay | de9a5b1 | 2020-10-31 12:15:43 -0700 | [diff] [blame] | 155 | // Important in order for System V ABI to pass in registers. |
| 156 | Slice(const Slice<T> &) noexcept = default; |
| 157 | ~Slice() noexcept = default; |
| 158 | |
| David Tolnay | ac6cb54 | 2020-11-25 20:18:55 -0800 | [diff] [blame] | 159 | class iterator; |
| 160 | iterator begin() const noexcept; |
| 161 | iterator end() const noexcept; |
| 162 | |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 163 | private: |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 164 | friend impl<Slice>; |
| 165 | // Not necessarily ABI compatible with &[T]. Codegen will translate to |
| David Tolnay | 5515a9e | 2020-11-25 19:07:54 -0800 | [diff] [blame] | 166 | // cxx::rust_slice::RustSlice which matches this layout. |
| David Tolnay | ce29823 | 2020-11-11 10:08:54 -0800 | [diff] [blame] | 167 | T *ptr; |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 168 | size_t len; |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 169 | }; |
| David Tolnay | ac6cb54 | 2020-11-25 20:18:55 -0800 | [diff] [blame] | 170 | |
| 171 | template <typename T> |
| 172 | class Slice<T>::iterator final { |
| 173 | public: |
| 174 | using difference_type = ptrdiff_t; |
| 175 | using value_type = T; |
| 176 | using pointer = typename std::add_pointer<T>::type; |
| 177 | using reference = typename std::add_lvalue_reference<T>::type; |
| 178 | using iterator_category = std::forward_iterator_tag; |
| 179 | |
| 180 | T &operator*() const noexcept; |
| 181 | T *operator->() const noexcept; |
| 182 | iterator &operator++() noexcept; |
| 183 | iterator operator++(int) noexcept; |
| 184 | bool operator==(const iterator &) const noexcept; |
| 185 | bool operator!=(const iterator &) const noexcept; |
| 186 | |
| 187 | private: |
| 188 | friend class Slice; |
| 189 | T *pos; |
| 190 | }; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 191 | #endif // CXXBRIDGE1_RUST_SLICE |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 192 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 193 | #ifndef CXXBRIDGE1_RUST_BOX |
| David Tolnay | e468f05 | 2020-11-29 19:39:35 -0800 | [diff] [blame] | 194 | // https://cxx.rs/binding/box.html |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 195 | template <typename T> |
| 196 | class Box final { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 197 | public: |
| David Tolnay | f629237 | 2020-03-01 21:09:11 -0800 | [diff] [blame] | 198 | using value_type = T; |
| David Tolnay | 9706a51 | 2020-04-24 17:09:01 -0700 | [diff] [blame] | 199 | using const_pointer = |
| 200 | typename std::add_pointer<typename std::add_const<T>::type>::type; |
| 201 | using pointer = typename std::add_pointer<T>::type; |
| David Tolnay | f629237 | 2020-03-01 21:09:11 -0800 | [diff] [blame] | 202 | |
| David Tolnay | c4b3422 | 2020-12-12 13:06:26 -0800 | [diff] [blame] | 203 | Box() = delete; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 204 | Box(const Box &); |
| 205 | Box(Box &&) noexcept; |
| 206 | ~Box() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 207 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 208 | explicit Box(const T &); |
| 209 | explicit Box(T &&); |
| 210 | |
| 211 | Box &operator=(const Box &); |
| 212 | Box &operator=(Box &&) noexcept; |
| 213 | |
| 214 | const T *operator->() const noexcept; |
| 215 | const T &operator*() const noexcept; |
| 216 | T *operator->() noexcept; |
| 217 | T &operator*() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 218 | |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 219 | template <typename... Fields> |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 220 | static Box in_place(Fields &&...); |
| David Tolnay | 7ce59fc | 2020-04-11 11:46:33 -0700 | [diff] [blame] | 221 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 222 | // Important: requires that `raw` came from an into_raw call. Do not pass a |
| 223 | // pointer from `new` or any other source. |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 224 | static Box from_raw(T *) noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 225 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 226 | T *into_raw() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 227 | |
| 228 | private: |
| David Tolnay | c4b3422 | 2020-12-12 13:06:26 -0800 | [diff] [blame] | 229 | class uninit; |
| David Tolnay | e570316 | 2020-12-12 16:26:35 -0800 | [diff] [blame] | 230 | class allocation; |
| David Tolnay | c4b3422 | 2020-12-12 13:06:26 -0800 | [diff] [blame] | 231 | Box(uninit) noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 232 | void drop() noexcept; |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 233 | T *ptr; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 234 | }; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 235 | #endif // CXXBRIDGE1_RUST_BOX |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 236 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 237 | #ifndef CXXBRIDGE1_RUST_VEC |
| David Tolnay | e468f05 | 2020-11-29 19:39:35 -0800 | [diff] [blame] | 238 | // https://cxx.rs/binding/vec.html |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 239 | template <typename T> |
| 240 | class Vec final { |
| 241 | public: |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 242 | using value_type = T; |
| 243 | |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 244 | Vec() noexcept; |
| David Tolnay | f799b37 | 2020-11-29 23:57:42 -0800 | [diff] [blame] | 245 | Vec(std::initializer_list<T>); |
| David Tolnay | 9007e46 | 2020-12-11 13:59:08 -0800 | [diff] [blame] | 246 | Vec(const Vec &); |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 247 | Vec(Vec &&) noexcept; |
| 248 | ~Vec() noexcept; |
| David Tolnay | cb80057 | 2020-04-24 20:30:43 -0700 | [diff] [blame] | 249 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 250 | Vec &operator=(Vec &&) noexcept; |
| David Tolnay | dd42c72 | 2020-12-11 14:05:26 -0800 | [diff] [blame] | 251 | Vec &operator=(const Vec &); |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 252 | |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 253 | size_t size() const noexcept; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 254 | bool empty() const noexcept; |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 255 | const T *data() const noexcept; |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 256 | T *data() noexcept; |
| David Tolnay | dc62d71 | 2020-12-11 13:51:53 -0800 | [diff] [blame] | 257 | size_t capacity() const noexcept; |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 258 | |
| Stephen Crane | 9e48d5b | 2020-08-21 12:17:02 -0700 | [diff] [blame] | 259 | const T &operator[](size_t n) const noexcept; |
| 260 | const T &at(size_t n) const; |
| Stephen Crane | 9e48d5b | 2020-08-21 12:17:02 -0700 | [diff] [blame] | 261 | const T &front() const; |
| 262 | const T &back() const; |
| 263 | |
| David Tolnay | 908d5e5 | 2020-11-30 00:30:59 -0800 | [diff] [blame] | 264 | T &operator[](size_t n) noexcept; |
| 265 | T &at(size_t n); |
| 266 | T &front(); |
| 267 | T &back(); |
| 268 | |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 269 | void reserve(size_t new_cap); |
| 270 | void push_back(const T &value); |
| 271 | void push_back(T &&value); |
| David Tolnay | 4e8c49a | 2020-11-11 10:00:18 -0800 | [diff] [blame] | 272 | template <typename... Args> |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 273 | void emplace_back(Args &&... args); |
| 274 | |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 275 | class iterator; |
| 276 | iterator begin() noexcept; |
| 277 | iterator end() noexcept; |
| 278 | |
| David Tolnay | a5d72c6 | 2020-11-25 13:57:16 -0800 | [diff] [blame] | 279 | using const_iterator = typename Vec<const T>::iterator; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 280 | const_iterator begin() const noexcept; |
| 281 | const_iterator end() const noexcept; |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 282 | const_iterator cbegin() const noexcept; |
| 283 | const_iterator cend() const noexcept; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 284 | |
| David Tolnay | 313b10e | 2020-04-25 16:30:51 -0700 | [diff] [blame] | 285 | // Internal API only intended for the cxxbridge code generator. |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 286 | Vec(unsafe_bitcopy_t, const Vec &) noexcept; |
| David Tolnay | 313b10e | 2020-04-25 16:30:51 -0700 | [diff] [blame] | 287 | |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 288 | private: |
| David Tolnay | 503d019 | 2020-04-24 22:18:56 -0700 | [diff] [blame] | 289 | static size_t stride() noexcept; |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 290 | void reserve_total(size_t cap) noexcept; |
| 291 | void set_len(size_t len) noexcept; |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 292 | void drop() noexcept; |
| 293 | |
| 294 | // Size and alignment statically verified by rust_vec.rs. |
| 295 | std::array<uintptr_t, 3> repr; |
| 296 | }; |
| David Tolnay | 66f216c | 2020-11-25 13:21:00 -0800 | [diff] [blame] | 297 | |
| 298 | template <typename T> |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 299 | class Vec<T>::iterator final { |
| 300 | public: |
| Yan Zaretskiy | efd5cd4 | 2020-12-03 09:22:22 -0500 | [diff] [blame] | 301 | using iterator_category = std::random_access_iterator_tag; |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 302 | using value_type = T; |
| Yan Zaretskiy | efd5cd4 | 2020-12-03 09:22:22 -0500 | [diff] [blame] | 303 | using difference_type = ptrdiff_t; |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 304 | using pointer = typename std::add_pointer<T>::type; |
| 305 | using reference = typename std::add_lvalue_reference<T>::type; |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 306 | |
| Yan Zaretskiy | efd5cd4 | 2020-12-03 09:22:22 -0500 | [diff] [blame] | 307 | reference operator*() const noexcept; |
| 308 | pointer operator->() const noexcept; |
| 309 | reference operator[](difference_type) const noexcept; |
| 310 | |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 311 | iterator &operator++() noexcept; |
| 312 | iterator operator++(int) noexcept; |
| Yan Zaretskiy | efd5cd4 | 2020-12-03 09:22:22 -0500 | [diff] [blame] | 313 | iterator &operator--() noexcept; |
| 314 | iterator operator--(int) noexcept; |
| 315 | |
| 316 | iterator &operator+=(difference_type) noexcept; |
| 317 | iterator &operator-=(difference_type) noexcept; |
| 318 | iterator operator+(difference_type) const noexcept; |
| 319 | iterator operator-(difference_type) const noexcept; |
| 320 | difference_type operator-(const iterator &) const noexcept; |
| 321 | |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 322 | bool operator==(const iterator &) const noexcept; |
| 323 | bool operator!=(const iterator &) const noexcept; |
| Yan Zaretskiy | efd5cd4 | 2020-12-03 09:22:22 -0500 | [diff] [blame] | 324 | bool operator<(const iterator &) const noexcept; |
| 325 | bool operator>(const iterator &) const noexcept; |
| 326 | bool operator<=(const iterator &) const noexcept; |
| 327 | bool operator>=(const iterator &) const noexcept; |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 328 | |
| 329 | private: |
| 330 | friend class Vec; |
| David Tolnay | a5d72c6 | 2020-11-25 13:57:16 -0800 | [diff] [blame] | 331 | friend class Vec<typename std::remove_const<T>::type>; |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 332 | void *pos; |
| 333 | size_t stride; |
| 334 | }; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 335 | #endif // CXXBRIDGE1_RUST_VEC |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 336 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 337 | #ifndef CXXBRIDGE1_RUST_FN |
| David Tolnay | e468f05 | 2020-11-29 19:39:35 -0800 | [diff] [blame] | 338 | // https://cxx.rs/binding/fn.html |
| David Tolnay | f031c32 | 2020-11-29 19:41:33 -0800 | [diff] [blame] | 339 | template <typename Signature> |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 340 | class Fn; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 341 | |
| David Tolnay | f031c32 | 2020-11-29 19:41:33 -0800 | [diff] [blame] | 342 | template <typename Ret, typename... Args> |
| 343 | class Fn<Ret(Args...)> final { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 344 | public: |
| David Tolnay | f031c32 | 2020-11-29 19:41:33 -0800 | [diff] [blame] | 345 | Ret operator()(Args... args) const noexcept; |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 346 | Fn operator*() const noexcept; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 347 | |
| 348 | private: |
| David Tolnay | f031c32 | 2020-11-29 19:41:33 -0800 | [diff] [blame] | 349 | Ret (*trampoline)(Args..., void *fn) noexcept; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 350 | void *fn; |
| 351 | }; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 352 | #endif // CXXBRIDGE1_RUST_FN |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 353 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 354 | #ifndef CXXBRIDGE1_RUST_ERROR |
| 355 | #define CXXBRIDGE1_RUST_ERROR |
| David Tolnay | e468f05 | 2020-11-29 19:39:35 -0800 | [diff] [blame] | 356 | // https://cxx.rs/binding/result.html |
| David Tolnay | e4fa873 | 2020-09-08 15:04:56 -0700 | [diff] [blame] | 357 | class Error final : public std::exception { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 358 | public: |
| 359 | Error(const Error &); |
| 360 | Error(Error &&) noexcept; |
| David Tolnay | 2714d2c | 2020-11-23 18:17:43 -0800 | [diff] [blame] | 361 | ~Error() noexcept override; |
| David Tolnay | 7c6ac71 | 2020-10-31 17:22:28 -0700 | [diff] [blame] | 362 | |
| 363 | Error &operator=(const Error &); |
| David Tolnay | 1549106 | 2020-10-31 17:25:13 -0700 | [diff] [blame] | 364 | Error &operator=(Error &&) noexcept; |
| David Tolnay | 7c6ac71 | 2020-10-31 17:22:28 -0700 | [diff] [blame] | 365 | |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 366 | const char *what() const noexcept override; |
| 367 | |
| 368 | private: |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 369 | Error() noexcept = default; |
| David Tolnay | 84ddf9e | 2020-10-31 15:36:48 -0700 | [diff] [blame] | 370 | friend impl<Error>; |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 371 | const char *msg; |
| 372 | size_t len; |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 373 | }; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 374 | #endif // CXXBRIDGE1_RUST_ERROR |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 375 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 376 | #ifndef CXXBRIDGE1_RUST_ISIZE |
| 377 | #define CXXBRIDGE1_RUST_ISIZE |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 378 | #if defined(_WIN32) |
| 379 | using isize = SSIZE_T; |
| 380 | #else |
| 381 | using isize = ssize_t; |
| 382 | #endif |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 383 | #endif // CXXBRIDGE1_RUST_ISIZE |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 384 | |
| David Tolnay | 851677c | 2020-03-01 23:49:46 -0800 | [diff] [blame] | 385 | std::ostream &operator<<(std::ostream &, const String &); |
| 386 | std::ostream &operator<<(std::ostream &, const Str &); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 387 | |
| David Tolnay | 365fc7c | 2020-11-25 16:08:13 -0800 | [diff] [blame] | 388 | #ifndef CXXBRIDGE1_RUST_OPAQUE |
| 389 | #define CXXBRIDGE1_RUST_OPAQUE |
| 390 | // Base class of generated opaque Rust types. |
| 391 | class Opaque { |
| David Tolnay | a857c32 | 2020-11-25 16:27:19 -0800 | [diff] [blame] | 392 | public: |
| David Tolnay | 365fc7c | 2020-11-25 16:08:13 -0800 | [diff] [blame] | 393 | Opaque() = delete; |
| 394 | Opaque(const Opaque &) = delete; |
| 395 | ~Opaque() = delete; |
| 396 | }; |
| 397 | #endif // CXXBRIDGE1_RUST_OPAQUE |
| 398 | |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame] | 399 | // IsRelocatable<T> is used in assertions that a C++ type passed by value |
| 400 | // between Rust and C++ is soundly relocatable by Rust. |
| 401 | // |
| 402 | // There may be legitimate reasons to opt out of the check for support of types |
| 403 | // that the programmer knows are soundly Rust-movable despite not being |
| 404 | // recognized as such by the C++ type system due to a move constructor or |
| 405 | // destructor. To opt out of the relocatability check, do either of the |
| 406 | // following things in any header used by `include!` in the bridge. |
| 407 | // |
| 408 | // --- if you define the type: |
| 409 | // struct MyType { |
| 410 | // ... |
| 411 | // + using IsRelocatable = std::true_type; |
| 412 | // }; |
| 413 | // |
| 414 | // --- otherwise: |
| 415 | // + template <> |
| 416 | // + struct rust::IsRelocatable<MyType> : std::true_type {}; |
| 417 | template <typename T> |
| 418 | struct IsRelocatable; |
| 419 | |
| David Tolnay | 3b0c988 | 2020-03-01 14:08:57 -0800 | [diff] [blame] | 420 | // Snake case aliases for use in code that uses this style for type names. |
| 421 | using string = String; |
| 422 | using str = Str; |
| David Tolnay | 4e8c49a | 2020-11-11 10:00:18 -0800 | [diff] [blame] | 423 | template <typename T> |
| David Tolnay | 38c8764 | 2020-09-06 22:18:08 -0700 | [diff] [blame] | 424 | using slice = Slice<T>; |
| David Tolnay | 4e8c49a | 2020-11-11 10:00:18 -0800 | [diff] [blame] | 425 | template <typename T> |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 426 | using box = Box<T>; |
| David Tolnay | 4e8c49a | 2020-11-11 10:00:18 -0800 | [diff] [blame] | 427 | template <typename T> |
| David Tolnay | 38c8764 | 2020-09-06 22:18:08 -0700 | [diff] [blame] | 428 | using vec = Vec<T>; |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 429 | using error = Error; |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 430 | template <typename Signature> |
| David Tolnay | f031c32 | 2020-11-29 19:41:33 -0800 | [diff] [blame] | 431 | using fn = Fn<Signature>; |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame] | 432 | template <typename T> |
| 433 | using is_relocatable = IsRelocatable<T>; |
| David Tolnay | 3b0c988 | 2020-03-01 14:08:57 -0800 | [diff] [blame] | 434 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 435 | |
| 436 | |
| 437 | //////////////////////////////////////////////////////////////////////////////// |
| 438 | /// end public API, begin implementation details |
| 439 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 440 | #ifndef CXXBRIDGE1_PANIC |
| 441 | #define CXXBRIDGE1_PANIC |
| David Tolnay | 521d99d | 2020-08-26 20:45:40 -0700 | [diff] [blame] | 442 | template <typename Exception> |
| 443 | void panic [[noreturn]] (const char *msg); |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 444 | #endif // CXXBRIDGE1_PANIC |
| David Tolnay | 521d99d | 2020-08-26 20:45:40 -0700 | [diff] [blame] | 445 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 446 | #ifndef CXXBRIDGE1_RUST_FN |
| 447 | #define CXXBRIDGE1_RUST_FN |
| David Tolnay | f031c32 | 2020-11-29 19:41:33 -0800 | [diff] [blame] | 448 | template <typename Ret, typename... Args> |
| 449 | Ret Fn<Ret(Args...)>::operator()(Args... args) const noexcept { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 450 | return (*this->trampoline)(std::move(args)..., this->fn); |
| 451 | } |
| 452 | |
| David Tolnay | f031c32 | 2020-11-29 19:41:33 -0800 | [diff] [blame] | 453 | template <typename Ret, typename... Args> |
| 454 | Fn<Ret(Args...)> Fn<Ret(Args...)>::operator*() const noexcept { |
| David Tolnay | a23129c | 2020-04-08 20:08:21 -0700 | [diff] [blame] | 455 | return *this; |
| 456 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 457 | #endif // CXXBRIDGE1_RUST_FN |
| David Tolnay | a23129c | 2020-04-08 20:08:21 -0700 | [diff] [blame] | 458 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 459 | #ifndef CXXBRIDGE1_RUST_BITCOPY |
| 460 | #define CXXBRIDGE1_RUST_BITCOPY |
| David Tolnay | 4852122 | 2020-10-31 14:59:42 -0700 | [diff] [blame] | 461 | struct unsafe_bitcopy_t final { |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 462 | explicit unsafe_bitcopy_t() = default; |
| 463 | }; |
| 464 | |
| 465 | constexpr unsafe_bitcopy_t unsafe_bitcopy{}; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 466 | #endif // CXXBRIDGE1_RUST_BITCOPY |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 467 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 468 | #ifndef CXXBRIDGE1_RUST_STR |
| 469 | #define CXXBRIDGE1_RUST_STR |
| David Tolnay | 5b1ee1f | 2020-10-31 18:21:39 -0700 | [diff] [blame] | 470 | inline const char *Str::data() const noexcept { return this->ptr; } |
| 471 | |
| 472 | inline size_t Str::size() const noexcept { return this->len; } |
| 473 | |
| 474 | inline size_t Str::length() const noexcept { return this->len; } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 475 | #endif // CXXBRIDGE1_RUST_STR |
| David Tolnay | 5b1ee1f | 2020-10-31 18:21:39 -0700 | [diff] [blame] | 476 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 477 | #ifndef CXXBRIDGE1_RUST_SLICE |
| 478 | #define CXXBRIDGE1_RUST_SLICE |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 479 | template <typename T> |
| David Tolnay | 7b16a39 | 2020-11-25 20:23:10 -0800 | [diff] [blame] | 480 | Slice<T>::Slice() noexcept : ptr(reinterpret_cast<T *>(alignof(T))), len(0) {} |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 481 | |
| 482 | template <typename T> |
| David Tolnay | ce29823 | 2020-11-11 10:08:54 -0800 | [diff] [blame] | 483 | Slice<T>::Slice(T *s, size_t count) noexcept : ptr(s), len(count) {} |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 484 | |
| 485 | template <typename T> |
| David Tolnay | ce29823 | 2020-11-11 10:08:54 -0800 | [diff] [blame] | 486 | T *Slice<T>::data() const noexcept { |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 487 | return this->ptr; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | template <typename T> |
| 491 | size_t Slice<T>::size() const noexcept { |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 492 | return this->len; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | template <typename T> |
| 496 | size_t Slice<T>::length() const noexcept { |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 497 | return this->len; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 498 | } |
| David Tolnay | ac6cb54 | 2020-11-25 20:18:55 -0800 | [diff] [blame] | 499 | |
| 500 | template <typename T> |
| 501 | T &Slice<T>::iterator::operator*() const noexcept { |
| 502 | return *this->pos; |
| 503 | } |
| 504 | |
| 505 | template <typename T> |
| 506 | T *Slice<T>::iterator::operator->() const noexcept { |
| 507 | return this->pos; |
| 508 | } |
| 509 | |
| 510 | template <typename T> |
| 511 | typename Slice<T>::iterator &Slice<T>::iterator::operator++() noexcept { |
| 512 | ++this->pos; |
| 513 | return *this; |
| 514 | } |
| 515 | |
| 516 | template <typename T> |
| 517 | typename Slice<T>::iterator Slice<T>::iterator::operator++(int) noexcept { |
| 518 | auto ret = iterator(*this); |
| 519 | ++this->pos; |
| 520 | return ret; |
| 521 | } |
| 522 | |
| 523 | template <typename T> |
| 524 | bool Slice<T>::iterator::operator==(const iterator &other) const noexcept { |
| 525 | return this->pos == other.pos; |
| 526 | } |
| 527 | |
| 528 | template <typename T> |
| 529 | bool Slice<T>::iterator::operator!=(const iterator &other) const noexcept { |
| 530 | return this->pos != other.pos; |
| 531 | } |
| 532 | |
| 533 | template <typename T> |
| 534 | typename Slice<T>::iterator Slice<T>::begin() const noexcept { |
| 535 | iterator it; |
| 536 | it.pos = this->ptr; |
| 537 | return it; |
| 538 | } |
| 539 | |
| 540 | template <typename T> |
| 541 | typename Slice<T>::iterator Slice<T>::end() const noexcept { |
| 542 | iterator it = this->begin(); |
| 543 | it.pos += this->len; |
| 544 | return it; |
| 545 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 546 | #endif // CXXBRIDGE1_RUST_SLICE |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 547 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 548 | #ifndef CXXBRIDGE1_RUST_BOX |
| 549 | #define CXXBRIDGE1_RUST_BOX |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 550 | template <typename T> |
| David Tolnay | c4b3422 | 2020-12-12 13:06:26 -0800 | [diff] [blame] | 551 | class Box<T>::uninit {}; |
| 552 | |
| 553 | template <typename T> |
| David Tolnay | e570316 | 2020-12-12 16:26:35 -0800 | [diff] [blame] | 554 | class Box<T>::allocation { |
| David Tolnay | f448e20 | 2020-12-12 16:42:45 -0800 | [diff] [blame] | 555 | static T *alloc() noexcept; |
| 556 | static void dealloc(T *) noexcept; |
| David Tolnay | 2e637d9 | 2020-12-12 21:08:01 -0800 | [diff] [blame^] | 557 | |
| David Tolnay | e570316 | 2020-12-12 16:26:35 -0800 | [diff] [blame] | 558 | public: |
| David Tolnay | e7d662d | 2020-12-12 16:38:22 -0800 | [diff] [blame] | 559 | allocation() noexcept : ptr(alloc()) {} |
| 560 | ~allocation() noexcept { |
| 561 | if (this->ptr) { |
| 562 | dealloc(this->ptr); |
| 563 | } |
| 564 | } |
| David Tolnay | e570316 | 2020-12-12 16:26:35 -0800 | [diff] [blame] | 565 | T *ptr; |
| 566 | }; |
| 567 | |
| 568 | template <typename T> |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 569 | Box<T>::Box(const Box &other) : Box(*other) {} |
| 570 | |
| 571 | template <typename T> |
| 572 | Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) { |
| 573 | other.ptr = nullptr; |
| 574 | } |
| 575 | |
| 576 | template <typename T> |
| David Tolnay | e7d662d | 2020-12-12 16:38:22 -0800 | [diff] [blame] | 577 | Box<T>::Box(const T &val) { |
| 578 | allocation alloc; |
| 579 | ::new (alloc.ptr) T(val); |
| 580 | this->ptr = alloc.ptr; |
| 581 | alloc.ptr = nullptr; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | template <typename T> |
| David Tolnay | e7d662d | 2020-12-12 16:38:22 -0800 | [diff] [blame] | 585 | Box<T>::Box(T &&val) { |
| 586 | allocation alloc; |
| 587 | ::new (alloc.ptr) T(std::move(val)); |
| 588 | this->ptr = alloc.ptr; |
| 589 | alloc.ptr = nullptr; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | template <typename T> |
| 593 | Box<T>::~Box() noexcept { |
| 594 | if (this->ptr) { |
| 595 | this->drop(); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | template <typename T> |
| 600 | Box<T> &Box<T>::operator=(const Box &other) { |
| David Tolnay | 439ed0b | 2020-12-12 17:01:04 -0800 | [diff] [blame] | 601 | if (this->ptr) { |
| 602 | **this = *other; |
| 603 | } else { |
| 604 | allocation alloc; |
| 605 | ::new (alloc.ptr) T(*other); |
| 606 | this->ptr = alloc.ptr; |
| 607 | alloc.ptr = nullptr; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 608 | } |
| 609 | return *this; |
| 610 | } |
| 611 | |
| 612 | template <typename T> |
| 613 | Box<T> &Box<T>::operator=(Box &&other) noexcept { |
| 614 | if (this->ptr) { |
| 615 | this->drop(); |
| 616 | } |
| 617 | this->ptr = other.ptr; |
| 618 | other.ptr = nullptr; |
| 619 | return *this; |
| 620 | } |
| 621 | |
| 622 | template <typename T> |
| 623 | const T *Box<T>::operator->() const noexcept { |
| 624 | return this->ptr; |
| 625 | } |
| 626 | |
| 627 | template <typename T> |
| 628 | const T &Box<T>::operator*() const noexcept { |
| 629 | return *this->ptr; |
| 630 | } |
| 631 | |
| 632 | template <typename T> |
| 633 | T *Box<T>::operator->() noexcept { |
| 634 | return this->ptr; |
| 635 | } |
| 636 | |
| 637 | template <typename T> |
| 638 | T &Box<T>::operator*() noexcept { |
| 639 | return *this->ptr; |
| 640 | } |
| 641 | |
| 642 | template <typename T> |
| 643 | template <typename... Fields> |
| 644 | Box<T> Box<T>::in_place(Fields &&... fields) { |
| David Tolnay | e7d662d | 2020-12-12 16:38:22 -0800 | [diff] [blame] | 645 | allocation alloc; |
| 646 | auto ptr = alloc.ptr; |
| 647 | ::new (ptr) T{std::forward<Fields>(fields)...}; |
| 648 | alloc.ptr = nullptr; |
| 649 | return from_raw(ptr); |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | template <typename T> |
| 653 | Box<T> Box<T>::from_raw(T *raw) noexcept { |
| David Tolnay | c4b3422 | 2020-12-12 13:06:26 -0800 | [diff] [blame] | 654 | Box box = uninit{}; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 655 | box.ptr = raw; |
| 656 | return box; |
| 657 | } |
| 658 | |
| 659 | template <typename T> |
| 660 | T *Box<T>::into_raw() noexcept { |
| 661 | T *raw = this->ptr; |
| 662 | this->ptr = nullptr; |
| 663 | return raw; |
| 664 | } |
| 665 | |
| 666 | template <typename T> |
| David Tolnay | c4b3422 | 2020-12-12 13:06:26 -0800 | [diff] [blame] | 667 | Box<T>::Box(uninit) noexcept {} |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 668 | #endif // CXXBRIDGE1_RUST_BOX |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 669 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 670 | #ifndef CXXBRIDGE1_RUST_VEC |
| 671 | #define CXXBRIDGE1_RUST_VEC |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 672 | template <typename T> |
| David Tolnay | f799b37 | 2020-11-29 23:57:42 -0800 | [diff] [blame] | 673 | Vec<T>::Vec(std::initializer_list<T> init) : Vec{} { |
| 674 | this->reserve_total(init.size()); |
| 675 | std::move(init.begin(), init.end(), std::back_inserter(*this)); |
| 676 | } |
| 677 | |
| 678 | template <typename T> |
| David Tolnay | 9007e46 | 2020-12-11 13:59:08 -0800 | [diff] [blame] | 679 | Vec<T>::Vec(const Vec &other) : Vec() { |
| 680 | this->reserve_total(other.size()); |
| 681 | std::copy(other.begin(), other.end(), std::back_inserter(*this)); |
| 682 | } |
| 683 | |
| 684 | template <typename T> |
| David Tolnay | 1567186 | 2020-11-23 18:13:56 -0800 | [diff] [blame] | 685 | Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) { |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 686 | new (&other) Vec(); |
| 687 | } |
| 688 | |
| 689 | template <typename T> |
| 690 | Vec<T>::~Vec() noexcept { |
| 691 | this->drop(); |
| 692 | } |
| 693 | |
| 694 | template <typename T> |
| 695 | Vec<T> &Vec<T>::operator=(Vec &&other) noexcept { |
| 696 | if (this != &other) { |
| 697 | this->drop(); |
| 698 | this->repr = other.repr; |
| 699 | new (&other) Vec(); |
| 700 | } |
| 701 | return *this; |
| 702 | } |
| 703 | |
| 704 | template <typename T> |
| David Tolnay | dd42c72 | 2020-12-11 14:05:26 -0800 | [diff] [blame] | 705 | Vec<T> &Vec<T>::operator=(const Vec &other) { |
| 706 | if (this != &other) { |
| 707 | this->drop(); |
| 708 | new (this) Vec(other); |
| 709 | } |
| 710 | return *this; |
| 711 | } |
| 712 | |
| 713 | template <typename T> |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 714 | bool Vec<T>::empty() const noexcept { |
| 715 | return size() == 0; |
| 716 | } |
| 717 | |
| 718 | template <typename T> |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 719 | T *Vec<T>::data() noexcept { |
| 720 | return const_cast<T *>(const_cast<const Vec<T> *>(this)->data()); |
| 721 | } |
| 722 | |
| 723 | template <typename T> |
| Stephen Crane | 9e48d5b | 2020-08-21 12:17:02 -0700 | [diff] [blame] | 724 | const T &Vec<T>::operator[](size_t n) const noexcept { |
| 725 | auto data = reinterpret_cast<const char *>(this->data()); |
| 726 | return *reinterpret_cast<const T *>(data + n * this->stride()); |
| 727 | } |
| 728 | |
| 729 | template <typename T> |
| 730 | const T &Vec<T>::at(size_t n) const { |
| David Tolnay | 8e1e6ac | 2020-08-26 20:51:43 -0700 | [diff] [blame] | 731 | if (n >= this->size()) { |
| 732 | panic<std::out_of_range>("rust::Vec index out of range"); |
| 733 | } |
| Stephen Crane | 9e48d5b | 2020-08-21 12:17:02 -0700 | [diff] [blame] | 734 | return (*this)[n]; |
| 735 | } |
| 736 | |
| 737 | template <typename T> |
| 738 | const T &Vec<T>::front() const { |
| 739 | return (*this)[0]; |
| 740 | } |
| 741 | |
| 742 | template <typename T> |
| 743 | const T &Vec<T>::back() const { |
| David Tolnay | b10c4bc | 2020-08-26 21:55:29 -0700 | [diff] [blame] | 744 | return (*this)[this->size() - 1]; |
| Stephen Crane | 9e48d5b | 2020-08-21 12:17:02 -0700 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | template <typename T> |
| David Tolnay | 908d5e5 | 2020-11-30 00:30:59 -0800 | [diff] [blame] | 748 | T &Vec<T>::operator[](size_t n) noexcept { |
| 749 | auto data = reinterpret_cast<char *>(this->data()); |
| 750 | return *reinterpret_cast<T *>(data + n * this->stride()); |
| 751 | } |
| 752 | |
| 753 | template <typename T> |
| 754 | T &Vec<T>::at(size_t n) { |
| 755 | if (n >= this->size()) { |
| 756 | panic<std::out_of_range>("rust::Vec index out of range"); |
| 757 | } |
| 758 | return (*this)[n]; |
| 759 | } |
| 760 | |
| 761 | template <typename T> |
| 762 | T &Vec<T>::front() { |
| 763 | return (*this)[0]; |
| 764 | } |
| 765 | |
| 766 | template <typename T> |
| 767 | T &Vec<T>::back() { |
| 768 | return (*this)[this->size() - 1]; |
| 769 | } |
| 770 | |
| 771 | template <typename T> |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 772 | void Vec<T>::reserve(size_t new_cap) { |
| 773 | this->reserve_total(new_cap); |
| 774 | } |
| 775 | |
| 776 | template <typename T> |
| 777 | void Vec<T>::push_back(const T &value) { |
| 778 | this->emplace_back(value); |
| 779 | } |
| 780 | |
| 781 | template <typename T> |
| 782 | void Vec<T>::push_back(T &&value) { |
| 783 | this->emplace_back(std::move(value)); |
| 784 | } |
| 785 | |
| 786 | template <typename T> |
| 787 | template <typename... Args> |
| 788 | void Vec<T>::emplace_back(Args &&... args) { |
| 789 | auto size = this->size(); |
| 790 | this->reserve_total(size + 1); |
| 791 | ::new (reinterpret_cast<T *>(reinterpret_cast<char *>(this->data()) + |
| 792 | size * this->stride())) |
| 793 | T(std::forward<Args>(args)...); |
| 794 | this->set_len(size + 1); |
| 795 | } |
| 796 | |
| 797 | template <typename T> |
| David Tolnay | 300072b | 2020-12-03 12:12:24 -0800 | [diff] [blame] | 798 | typename Vec<T>::iterator::reference |
| 799 | Vec<T>::iterator::operator*() const noexcept { |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 800 | return *static_cast<T *>(this->pos); |
| 801 | } |
| 802 | |
| 803 | template <typename T> |
| David Tolnay | 300072b | 2020-12-03 12:12:24 -0800 | [diff] [blame] | 804 | typename Vec<T>::iterator::pointer |
| 805 | Vec<T>::iterator::operator->() const noexcept { |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 806 | return static_cast<T *>(this->pos); |
| 807 | } |
| 808 | |
| 809 | template <typename T> |
| Yan Zaretskiy | efd5cd4 | 2020-12-03 09:22:22 -0500 | [diff] [blame] | 810 | typename Vec<T>::iterator::reference Vec<T>::iterator::operator[]( |
| David Tolnay | 665178e | 2020-12-12 08:49:59 -0800 | [diff] [blame] | 811 | typename Vec<T>::iterator::difference_type n) const noexcept { |
| Yan Zaretskiy | efd5cd4 | 2020-12-03 09:22:22 -0500 | [diff] [blame] | 812 | auto pos = static_cast<char *>(this->pos) + this->stride * n; |
| 813 | return *static_cast<T *>(pos); |
| 814 | } |
| 815 | |
| 816 | template <typename T> |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 817 | typename Vec<T>::iterator &Vec<T>::iterator::operator++() noexcept { |
| David Tolnay | 248215e | 2020-11-25 19:38:26 -0800 | [diff] [blame] | 818 | this->pos = static_cast<char *>(this->pos) + this->stride; |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 819 | return *this; |
| 820 | } |
| 821 | |
| 822 | template <typename T> |
| 823 | typename Vec<T>::iterator Vec<T>::iterator::operator++(int) noexcept { |
| 824 | auto ret = iterator(*this); |
| David Tolnay | 248215e | 2020-11-25 19:38:26 -0800 | [diff] [blame] | 825 | this->pos = static_cast<char *>(this->pos) + this->stride; |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 826 | return ret; |
| 827 | } |
| 828 | |
| 829 | template <typename T> |
| Yan Zaretskiy | efd5cd4 | 2020-12-03 09:22:22 -0500 | [diff] [blame] | 830 | typename Vec<T>::iterator &Vec<T>::iterator::operator--() noexcept { |
| 831 | this->pos = static_cast<char *>(this->pos) - this->stride; |
| 832 | return *this; |
| 833 | } |
| 834 | |
| 835 | template <typename T> |
| 836 | typename Vec<T>::iterator Vec<T>::iterator::operator--(int) noexcept { |
| 837 | auto ret = iterator(*this); |
| 838 | this->pos = static_cast<char *>(this->pos) - this->stride; |
| 839 | return ret; |
| 840 | } |
| 841 | |
| 842 | template <typename T> |
| David Tolnay | 665178e | 2020-12-12 08:49:59 -0800 | [diff] [blame] | 843 | typename Vec<T>::iterator &Vec<T>::iterator::operator+=( |
| 844 | typename Vec<T>::iterator::difference_type n) noexcept { |
| Yan Zaretskiy | efd5cd4 | 2020-12-03 09:22:22 -0500 | [diff] [blame] | 845 | this->pos = static_cast<char *>(this->pos) + this->stride * n; |
| 846 | return *this; |
| 847 | } |
| 848 | |
| 849 | template <typename T> |
| David Tolnay | 665178e | 2020-12-12 08:49:59 -0800 | [diff] [blame] | 850 | typename Vec<T>::iterator &Vec<T>::iterator::operator-=( |
| 851 | typename Vec<T>::iterator::difference_type n) noexcept { |
| Yan Zaretskiy | efd5cd4 | 2020-12-03 09:22:22 -0500 | [diff] [blame] | 852 | this->pos = static_cast<char *>(this->pos) - this->stride * n; |
| 853 | return *this; |
| 854 | } |
| 855 | |
| 856 | template <typename T> |
| 857 | typename Vec<T>::iterator Vec<T>::iterator::operator+( |
| David Tolnay | 665178e | 2020-12-12 08:49:59 -0800 | [diff] [blame] | 858 | typename Vec<T>::iterator::difference_type n) const noexcept { |
| David Tolnay | 300072b | 2020-12-03 12:12:24 -0800 | [diff] [blame] | 859 | auto ret = iterator(*this); |
| 860 | ret.pos = static_cast<char *>(this->pos) + this->stride * n; |
| 861 | return ret; |
| Yan Zaretskiy | efd5cd4 | 2020-12-03 09:22:22 -0500 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | template <typename T> |
| 865 | typename Vec<T>::iterator Vec<T>::iterator::operator-( |
| David Tolnay | 665178e | 2020-12-12 08:49:59 -0800 | [diff] [blame] | 866 | typename Vec<T>::iterator::difference_type n) const noexcept { |
| David Tolnay | 300072b | 2020-12-03 12:12:24 -0800 | [diff] [blame] | 867 | auto ret = iterator(*this); |
| 868 | ret.pos = static_cast<char *>(this->pos) - this->stride * n; |
| 869 | return ret; |
| Yan Zaretskiy | efd5cd4 | 2020-12-03 09:22:22 -0500 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | template <typename T> |
| 873 | typename Vec<T>::iterator::difference_type |
| 874 | Vec<T>::iterator::operator-(const iterator &other) const noexcept { |
| 875 | auto diff = std::distance(static_cast<char *>(other.pos), |
| 876 | static_cast<char *>(this->pos)); |
| David Tolnay | 300072b | 2020-12-03 12:12:24 -0800 | [diff] [blame] | 877 | return diff / this->stride; |
| Yan Zaretskiy | efd5cd4 | 2020-12-03 09:22:22 -0500 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | template <typename T> |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 881 | bool Vec<T>::iterator::operator==(const iterator &other) const noexcept { |
| 882 | return this->pos == other.pos; |
| 883 | } |
| 884 | |
| 885 | template <typename T> |
| 886 | bool Vec<T>::iterator::operator!=(const iterator &other) const noexcept { |
| 887 | return this->pos != other.pos; |
| 888 | } |
| 889 | |
| 890 | template <typename T> |
| Yan Zaretskiy | efd5cd4 | 2020-12-03 09:22:22 -0500 | [diff] [blame] | 891 | bool Vec<T>::iterator::operator>(const iterator &other) const noexcept { |
| 892 | return this->pos > other.pos; |
| 893 | } |
| 894 | |
| 895 | template <typename T> |
| 896 | bool Vec<T>::iterator::operator<(const iterator &other) const noexcept { |
| 897 | return this->pos < other.pos; |
| 898 | } |
| 899 | |
| 900 | template <typename T> |
| 901 | bool Vec<T>::iterator::operator>=(const iterator &other) const noexcept { |
| 902 | return this->pos >= other.pos; |
| 903 | } |
| 904 | |
| 905 | template <typename T> |
| 906 | bool Vec<T>::iterator::operator<=(const iterator &other) const noexcept { |
| 907 | return this->pos <= other.pos; |
| 908 | } |
| 909 | |
| 910 | template <typename T> |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 911 | typename Vec<T>::iterator Vec<T>::begin() noexcept { |
| 912 | iterator it; |
| David Tolnay | a5d72c6 | 2020-11-25 13:57:16 -0800 | [diff] [blame] | 913 | it.pos = const_cast<typename std::remove_const<T>::type *>(this->data()); |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 914 | it.stride = this->stride(); |
| 915 | return it; |
| 916 | } |
| 917 | |
| 918 | template <typename T> |
| 919 | typename Vec<T>::iterator Vec<T>::end() noexcept { |
| 920 | iterator it = this->begin(); |
| David Tolnay | 248215e | 2020-11-25 19:38:26 -0800 | [diff] [blame] | 921 | it.pos = static_cast<char *>(it.pos) + it.stride * this->size(); |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 922 | return it; |
| 923 | } |
| 924 | |
| 925 | template <typename T> |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 926 | typename Vec<T>::const_iterator Vec<T>::begin() const noexcept { |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 927 | return this->cbegin(); |
| 928 | } |
| 929 | |
| 930 | template <typename T> |
| 931 | typename Vec<T>::const_iterator Vec<T>::end() const noexcept { |
| 932 | return this->cend(); |
| 933 | } |
| 934 | |
| 935 | template <typename T> |
| 936 | typename Vec<T>::const_iterator Vec<T>::cbegin() const noexcept { |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 937 | const_iterator it; |
| David Tolnay | a5d72c6 | 2020-11-25 13:57:16 -0800 | [diff] [blame] | 938 | it.pos = const_cast<typename std::remove_const<T>::type *>(this->data()); |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 939 | it.stride = this->stride(); |
| 940 | return it; |
| 941 | } |
| 942 | |
| 943 | template <typename T> |
| David Tolnay | 960b511 | 2020-11-25 13:18:28 -0800 | [diff] [blame] | 944 | typename Vec<T>::const_iterator Vec<T>::cend() const noexcept { |
| 945 | const_iterator it = this->cbegin(); |
| David Tolnay | 248215e | 2020-11-25 19:38:26 -0800 | [diff] [blame] | 946 | it.pos = static_cast<char *>(it.pos) + it.stride * this->size(); |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 947 | return it; |
| 948 | } |
| 949 | |
| 950 | // Internal API only intended for the cxxbridge code generator. |
| 951 | template <typename T> |
| 952 | Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {} |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 953 | #endif // CXXBRIDGE1_RUST_VEC |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 954 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 955 | #ifndef CXXBRIDGE1_RELOCATABLE |
| 956 | #define CXXBRIDGE1_RELOCATABLE |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame] | 957 | namespace detail { |
| 958 | template <typename... Ts> |
| 959 | struct make_void { |
| 960 | using type = void; |
| 961 | }; |
| 962 | |
| 963 | template <typename... Ts> |
| 964 | using void_t = typename make_void<Ts...>::type; |
| 965 | |
| 966 | template <typename Void, template <typename...> class, typename...> |
| 967 | struct detect : std::false_type {}; |
| 968 | template <template <typename...> class T, typename... A> |
| 969 | struct detect<void_t<T<A...>>, T, A...> : std::true_type {}; |
| 970 | |
| 971 | template <template <typename...> class T, typename... A> |
| 972 | using is_detected = detect<void, T, A...>; |
| 973 | |
| 974 | template <typename T> |
| 975 | using detect_IsRelocatable = typename T::IsRelocatable; |
| 976 | |
| 977 | template <typename T> |
| 978 | struct get_IsRelocatable |
| 979 | : std::is_same<typename T::IsRelocatable, std::true_type> {}; |
| 980 | } // namespace detail |
| 981 | |
| 982 | template <typename T> |
| 983 | struct IsRelocatable |
| 984 | : std::conditional< |
| 985 | detail::is_detected<detail::detect_IsRelocatable, T>::value, |
| 986 | detail::get_IsRelocatable<T>, |
| 987 | std::integral_constant< |
| 988 | bool, std::is_trivially_move_constructible<T>::value && |
| 989 | std::is_trivially_destructible<T>::value>>::type {}; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 990 | #endif // CXXBRIDGE1_RELOCATABLE |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame] | 991 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 992 | } // namespace cxxbridge1 |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 993 | } // namespace rust |