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