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