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