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