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