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