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