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