| David Tolnay | 736cbca | 2020-03-11 16:49:18 -0700 | [diff] [blame] | 1 | #include "../include/cxx.h" |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 2 | #include <cstring> |
| David Tolnay | 001102a | 2020-03-01 20:05:04 -0800 | [diff] [blame] | 3 | #include <iostream> |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 4 | #include <memory> |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 5 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 6 | extern "C" { |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 7 | void cxxbridge1$cxx_string$init(std::string *s, const std::uint8_t *ptr, |
| 8 | std::size_t len) noexcept { |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame] | 9 | new (s) std::string(reinterpret_cast<const char *>(ptr), len); |
| 10 | } |
| 11 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 12 | void cxxbridge1$cxx_string$destroy(std::string *s) noexcept { |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame] | 13 | using std::string; |
| 14 | s->~string(); |
| 15 | } |
| 16 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 17 | const char *cxxbridge1$cxx_string$data(const std::string &s) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 18 | return s.data(); |
| 19 | } |
| 20 | |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 21 | std::size_t cxxbridge1$cxx_string$length(const std::string &s) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 22 | return s.length(); |
| 23 | } |
| 24 | |
| David Tolnay | f2a1466 | 2021-08-12 13:47:46 -0700 | [diff] [blame] | 25 | void cxxbridge1$cxx_string$clear(std::string &s) noexcept { s.clear(); } |
| 26 | |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 27 | void cxxbridge1$cxx_string$push(std::string &s, const std::uint8_t *ptr, |
| 28 | std::size_t len) noexcept { |
| David Tolnay | 90691f4 | 2020-11-14 20:01:46 -0800 | [diff] [blame] | 29 | s.append(reinterpret_cast<const char *>(ptr), len); |
| 30 | } |
| 31 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 32 | // rust::String |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 33 | void cxxbridge1$string$new(rust::String *self) noexcept; |
| 34 | void cxxbridge1$string$clone(rust::String *self, |
| 35 | const rust::String &other) noexcept; |
| Gerhard de Clercq | 31a4102 | 2021-07-31 15:17:27 +0200 | [diff] [blame] | 36 | bool cxxbridge1$string$from_utf8(rust::String *self, const char *ptr, |
| 37 | std::size_t len) noexcept; |
| 38 | bool cxxbridge1$string$from_utf16(rust::String *self, const char16_t *ptr, |
| 39 | std::size_t len) noexcept; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 40 | void cxxbridge1$string$drop(rust::String *self) noexcept; |
| 41 | const char *cxxbridge1$string$ptr(const rust::String *self) noexcept; |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 42 | std::size_t cxxbridge1$string$len(const rust::String *self) noexcept; |
| David Tolnay | 185c2cb | 2021-08-27 11:47:52 -0700 | [diff] [blame] | 43 | void cxxbridge1$string$reserve_additional(rust::String *self, |
| 44 | size_t additional) noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 45 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 46 | // rust::Str |
| David Tolnay | 1202de5 | 2021-01-02 01:26:33 -0800 | [diff] [blame] | 47 | void cxxbridge1$str$new(rust::Str *self) noexcept; |
| 48 | void cxxbridge1$str$ref(rust::Str *self, const rust::String *string) noexcept; |
| 49 | bool cxxbridge1$str$from(rust::Str *self, const char *ptr, |
| 50 | std::size_t len) noexcept; |
| 51 | const char *cxxbridge1$str$ptr(const rust::Str *self) noexcept; |
| 52 | std::size_t cxxbridge1$str$len(const rust::Str *self) noexcept; |
| David Tolnay | 9bffb93 | 2021-01-02 02:15:21 -0800 | [diff] [blame] | 53 | |
| 54 | // rust::Slice |
| 55 | void cxxbridge1$slice$new(void *self, const void *ptr, |
| 56 | std::size_t len) noexcept; |
| 57 | void *cxxbridge1$slice$ptr(const void *self) noexcept; |
| 58 | std::size_t cxxbridge1$slice$len(const void *self) noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 59 | } // extern "C" |
| 60 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 61 | namespace rust { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 62 | inline namespace cxxbridge1 { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 63 | |
| David Tolnay | 521d99d | 2020-08-26 20:45:40 -0700 | [diff] [blame] | 64 | template <typename Exception> |
| 65 | void panic [[noreturn]] (const char *msg) { |
| 66 | #if defined(RUST_CXX_NO_EXCEPTIONS) |
| 67 | std::cerr << "Error: " << msg << ". Aborting." << std::endl; |
| 68 | std::terminate(); |
| 69 | #else |
| 70 | throw Exception(msg); |
| 71 | #endif |
| 72 | } |
| 73 | |
| David Tolnay | de01edf | 2021-03-22 15:00:15 -0700 | [diff] [blame] | 74 | template void panic<std::out_of_range> [[noreturn]] (const char *msg); |
| David Tolnay | 521d99d | 2020-08-26 20:45:40 -0700 | [diff] [blame] | 75 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 76 | String::String() noexcept { cxxbridge1$string$new(this); } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 77 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 78 | String::String(const String &other) noexcept { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 79 | cxxbridge1$string$clone(this, other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| David Tolnay | 1567186 | 2020-11-23 18:13:56 -0800 | [diff] [blame] | 82 | String::String(String &&other) noexcept : repr(other.repr) { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 83 | cxxbridge1$string$new(&other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 84 | } |
| 85 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 86 | String::~String() noexcept { cxxbridge1$string$drop(this); } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 87 | |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 88 | static void initString(String *self, const char *s, std::size_t len) { |
| Gerhard de Clercq | 31a4102 | 2021-07-31 15:17:27 +0200 | [diff] [blame] | 89 | if (!cxxbridge1$string$from_utf8(self, s, len)) { |
| David Tolnay | 8d32366 | 2020-10-30 19:32:26 -0700 | [diff] [blame] | 90 | panic<std::invalid_argument>("data for rust::String is not utf-8"); |
| 91 | } |
| 92 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 93 | |
| Gerhard de Clercq | 31a4102 | 2021-07-31 15:17:27 +0200 | [diff] [blame] | 94 | static void initString(String *self, const char16_t *s, std::size_t len) { |
| 95 | if (!cxxbridge1$string$from_utf16(self, s, len)) { |
| 96 | panic<std::invalid_argument>("data for rust::String is not utf-16"); |
| 97 | } |
| 98 | } |
| 99 | |
| David Tolnay | 032d853 | 2020-10-30 20:47:31 -0700 | [diff] [blame] | 100 | String::String(const std::string &s) { initString(this, s.data(), s.length()); } |
| 101 | |
| David Tolnay | 54b1322 | 2020-10-30 20:58:32 -0700 | [diff] [blame] | 102 | String::String(const char *s) { |
| 103 | assert(s != nullptr); |
| 104 | initString(this, s, std::strlen(s)); |
| 105 | } |
| David Tolnay | c2bbd95 | 2020-07-29 18:15:26 -0700 | [diff] [blame] | 106 | |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 107 | String::String(const char *s, std::size_t len) { |
| David Tolnay | 54b1322 | 2020-10-30 20:58:32 -0700 | [diff] [blame] | 108 | assert(s != nullptr || len == 0); |
| David Tolnay | 032d853 | 2020-10-30 20:47:31 -0700 | [diff] [blame] | 109 | initString(this, |
| 110 | s == nullptr && len == 0 ? reinterpret_cast<const char *>(1) : s, |
| 111 | len); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| Gerhard de Clercq | 31a4102 | 2021-07-31 15:17:27 +0200 | [diff] [blame] | 114 | String::String(const char16_t *s) { |
| 115 | assert(s != nullptr); |
| 116 | initString(this, s, std::char_traits<char16_t>::length(s)); |
| 117 | } |
| 118 | |
| 119 | String::String(const char16_t *s, std::size_t len) { |
| 120 | assert(s != nullptr || len == 0); |
| 121 | initString(this, |
| 122 | s == nullptr && len == 0 ? reinterpret_cast<const char16_t *>(2) |
| 123 | : s, |
| 124 | len); |
| 125 | } |
| 126 | |
| David Tolnay | 5d12f1d | 2021-03-22 14:58:38 -0700 | [diff] [blame] | 127 | String &String::operator=(const String &other) &noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 128 | if (this != &other) { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 129 | cxxbridge1$string$drop(this); |
| 130 | cxxbridge1$string$clone(this, other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 131 | } |
| 132 | return *this; |
| 133 | } |
| 134 | |
| David Tolnay | 5d12f1d | 2021-03-22 14:58:38 -0700 | [diff] [blame] | 135 | String &String::operator=(String &&other) &noexcept { |
| David Tolnay | 7bc397c | 2021-02-11 23:07:58 -0800 | [diff] [blame] | 136 | cxxbridge1$string$drop(this); |
| 137 | this->repr = other.repr; |
| 138 | cxxbridge1$string$new(&other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 139 | return *this; |
| 140 | } |
| 141 | |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 142 | String::operator std::string() const { |
| 143 | return std::string(this->data(), this->size()); |
| 144 | } |
| 145 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 146 | const char *String::data() const noexcept { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 147 | return cxxbridge1$string$ptr(this); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 148 | } |
| 149 | |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 150 | std::size_t String::size() const noexcept { |
| 151 | return cxxbridge1$string$len(this); |
| 152 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 153 | |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 154 | std::size_t String::length() const noexcept { |
| 155 | return cxxbridge1$string$len(this); |
| 156 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 157 | |
| David Tolnay | 99f79c0 | 2021-04-28 00:29:55 -0700 | [diff] [blame] | 158 | bool String::empty() const noexcept { return this->size() == 0; } |
| 159 | |
| David Tolnay | cca2e61 | 2020-12-18 12:48:22 -0800 | [diff] [blame] | 160 | const char *String::c_str() noexcept { |
| 161 | auto len = this->length(); |
| David Tolnay | 185c2cb | 2021-08-27 11:47:52 -0700 | [diff] [blame] | 162 | cxxbridge1$string$reserve_additional(this, 1); |
| David Tolnay | cca2e61 | 2020-12-18 12:48:22 -0800 | [diff] [blame] | 163 | auto ptr = this->data(); |
| 164 | const_cast<char *>(ptr)[len] = '\0'; |
| 165 | return ptr; |
| 166 | } |
| 167 | |
| David Tolnay | ff7f5fb | 2020-11-25 20:50:32 -0800 | [diff] [blame] | 168 | String::iterator String::begin() noexcept { |
| 169 | return const_cast<char *>(this->data()); |
| 170 | } |
| 171 | |
| 172 | String::iterator String::end() noexcept { |
| 173 | return const_cast<char *>(this->data()) + this->size(); |
| 174 | } |
| 175 | |
| 176 | String::const_iterator String::begin() const noexcept { return this->cbegin(); } |
| 177 | |
| 178 | String::const_iterator String::end() const noexcept { return this->cend(); } |
| 179 | |
| 180 | String::const_iterator String::cbegin() const noexcept { return this->data(); } |
| 181 | |
| 182 | String::const_iterator String::cend() const noexcept { |
| 183 | return this->data() + this->size(); |
| 184 | } |
| 185 | |
| David Tolnay | ff86dce | 2020-11-29 19:45:13 -0800 | [diff] [blame] | 186 | bool String::operator==(const String &rhs) const noexcept { |
| 187 | return rust::Str(*this) == rust::Str(rhs); |
| 188 | } |
| 189 | |
| 190 | bool String::operator!=(const String &rhs) const noexcept { |
| 191 | return rust::Str(*this) != rust::Str(rhs); |
| 192 | } |
| 193 | |
| 194 | bool String::operator<(const String &rhs) const noexcept { |
| 195 | return rust::Str(*this) < rust::Str(rhs); |
| 196 | } |
| 197 | |
| 198 | bool String::operator<=(const String &rhs) const noexcept { |
| 199 | return rust::Str(*this) <= rust::Str(rhs); |
| 200 | } |
| 201 | |
| 202 | bool String::operator>(const String &rhs) const noexcept { |
| 203 | return rust::Str(*this) > rust::Str(rhs); |
| 204 | } |
| 205 | |
| 206 | bool String::operator>=(const String &rhs) const noexcept { |
| 207 | return rust::Str(*this) >= rust::Str(rhs); |
| 208 | } |
| 209 | |
| David Tolnay | fec1715 | 2021-01-02 12:51:29 -0800 | [diff] [blame] | 210 | void String::swap(String &rhs) noexcept { |
| 211 | using std::swap; |
| 212 | swap(this->repr, rhs.repr); |
| 213 | } |
| 214 | |
| David Tolnay | d1e2efc | 2020-03-03 22:25:43 -0800 | [diff] [blame] | 215 | String::String(unsafe_bitcopy_t, const String &bits) noexcept |
| 216 | : repr(bits.repr) {} |
| 217 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 218 | std::ostream &operator<<(std::ostream &os, const String &s) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 219 | os.write(s.data(), s.size()); |
| 220 | return os; |
| 221 | } |
| 222 | |
| David Tolnay | 1202de5 | 2021-01-02 01:26:33 -0800 | [diff] [blame] | 223 | Str::Str() noexcept { cxxbridge1$str$new(this); } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 224 | |
| David Tolnay | 1202de5 | 2021-01-02 01:26:33 -0800 | [diff] [blame] | 225 | Str::Str(const String &s) noexcept { cxxbridge1$str$ref(this, &s); } |
| David Tolnay | 828e513 | 2020-11-29 20:40:40 -0800 | [diff] [blame] | 226 | |
| David Tolnay | 1202de5 | 2021-01-02 01:26:33 -0800 | [diff] [blame] | 227 | static void initStr(Str *self, const char *ptr, std::size_t len) { |
| 228 | if (!cxxbridge1$str$from(self, ptr, len)) { |
| David Tolnay | 8d32366 | 2020-10-30 19:32:26 -0700 | [diff] [blame] | 229 | panic<std::invalid_argument>("data for rust::Str is not utf-8"); |
| 230 | } |
| 231 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 232 | |
| David Tolnay | 1202de5 | 2021-01-02 01:26:33 -0800 | [diff] [blame] | 233 | Str::Str(const std::string &s) { initStr(this, s.data(), s.length()); } |
| David Tolnay | 894c5e4 | 2020-07-29 18:20:00 -0700 | [diff] [blame] | 234 | |
| David Tolnay | 1202de5 | 2021-01-02 01:26:33 -0800 | [diff] [blame] | 235 | Str::Str(const char *s) { |
| David Tolnay | 54b1322 | 2020-10-30 20:58:32 -0700 | [diff] [blame] | 236 | assert(s != nullptr); |
| David Tolnay | 1202de5 | 2021-01-02 01:26:33 -0800 | [diff] [blame] | 237 | initStr(this, s, std::strlen(s)); |
| David Tolnay | 54b1322 | 2020-10-30 20:58:32 -0700 | [diff] [blame] | 238 | } |
| David Tolnay | 032d853 | 2020-10-30 20:47:31 -0700 | [diff] [blame] | 239 | |
| David Tolnay | 1202de5 | 2021-01-02 01:26:33 -0800 | [diff] [blame] | 240 | Str::Str(const char *s, std::size_t len) { |
| David Tolnay | 54b1322 | 2020-10-30 20:58:32 -0700 | [diff] [blame] | 241 | assert(s != nullptr || len == 0); |
| David Tolnay | 1202de5 | 2021-01-02 01:26:33 -0800 | [diff] [blame] | 242 | initStr(this, |
| 243 | s == nullptr && len == 0 ? reinterpret_cast<const char *>(1) : s, |
| 244 | len); |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 245 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 246 | |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 247 | Str::operator std::string() const { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 248 | return std::string(this->data(), this->size()); |
| 249 | } |
| 250 | |
| David Tolnay | 1202de5 | 2021-01-02 01:26:33 -0800 | [diff] [blame] | 251 | const char *Str::data() const noexcept { return cxxbridge1$str$ptr(this); } |
| 252 | |
| 253 | std::size_t Str::size() const noexcept { return cxxbridge1$str$len(this); } |
| 254 | |
| 255 | std::size_t Str::length() const noexcept { return this->size(); } |
| 256 | |
| David Tolnay | 99f79c0 | 2021-04-28 00:29:55 -0700 | [diff] [blame] | 257 | bool Str::empty() const noexcept { return this->size() == 0; } |
| 258 | |
| David Tolnay | ff7f5fb | 2020-11-25 20:50:32 -0800 | [diff] [blame] | 259 | Str::const_iterator Str::begin() const noexcept { return this->cbegin(); } |
| 260 | |
| 261 | Str::const_iterator Str::end() const noexcept { return this->cend(); } |
| 262 | |
| David Tolnay | 1202de5 | 2021-01-02 01:26:33 -0800 | [diff] [blame] | 263 | Str::const_iterator Str::cbegin() const noexcept { return this->data(); } |
| David Tolnay | ff7f5fb | 2020-11-25 20:50:32 -0800 | [diff] [blame] | 264 | |
| David Tolnay | 1202de5 | 2021-01-02 01:26:33 -0800 | [diff] [blame] | 265 | Str::const_iterator Str::cend() const noexcept { |
| 266 | return this->data() + this->size(); |
| 267 | } |
| David Tolnay | ff7f5fb | 2020-11-25 20:50:32 -0800 | [diff] [blame] | 268 | |
| David Tolnay | ff86dce | 2020-11-29 19:45:13 -0800 | [diff] [blame] | 269 | bool Str::operator==(const Str &rhs) const noexcept { |
| David Tolnay | 1202de5 | 2021-01-02 01:26:33 -0800 | [diff] [blame] | 270 | return this->size() == rhs.size() && |
| David Tolnay | ff86dce | 2020-11-29 19:45:13 -0800 | [diff] [blame] | 271 | std::equal(this->begin(), this->end(), rhs.begin()); |
| 272 | } |
| 273 | |
| 274 | bool Str::operator!=(const Str &rhs) const noexcept { return !(*this == rhs); } |
| 275 | |
| 276 | bool Str::operator<(const Str &rhs) const noexcept { |
| 277 | return std::lexicographical_compare(this->begin(), this->end(), rhs.begin(), |
| 278 | rhs.end()); |
| 279 | } |
| 280 | |
| 281 | bool Str::operator<=(const Str &rhs) const noexcept { |
| 282 | // std::mismatch(this->begin(), this->end(), rhs.begin(), rhs.end()), except |
| 283 | // without Undefined Behavior on C++11 if rhs is shorter than *this. |
| 284 | const_iterator liter = this->begin(), lend = this->end(), riter = rhs.begin(), |
| 285 | rend = rhs.end(); |
| 286 | while (liter != lend && riter != rend && *liter == *riter) { |
| 287 | ++liter, ++riter; |
| 288 | } |
| 289 | if (liter == lend) { |
| 290 | return true; // equal or *this is a prefix of rhs |
| 291 | } else if (riter == rend) { |
| 292 | return false; // rhs is a prefix of *this |
| 293 | } else { |
| 294 | return *liter <= *riter; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | bool Str::operator>(const Str &rhs) const noexcept { return rhs < *this; } |
| 299 | |
| 300 | bool Str::operator>=(const Str &rhs) const noexcept { return rhs <= *this; } |
| 301 | |
| David Tolnay | ba750b6 | 2021-01-02 16:05:48 -0800 | [diff] [blame] | 302 | void Str::swap(Str &rhs) noexcept { |
| 303 | using std::swap; |
| 304 | swap(this->repr, rhs.repr); |
| 305 | } |
| David Tolnay | 0413ee2 | 2021-01-02 13:55:16 -0800 | [diff] [blame] | 306 | |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 307 | std::ostream &operator<<(std::ostream &os, const Str &s) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 308 | os.write(s.data(), s.size()); |
| 309 | return os; |
| 310 | } |
| 311 | |
| David Tolnay | 9bffb93 | 2021-01-02 02:15:21 -0800 | [diff] [blame] | 312 | void sliceInit(void *self, const void *ptr, std::size_t len) noexcept { |
| 313 | cxxbridge1$slice$new(self, ptr, len); |
| 314 | } |
| 315 | |
| 316 | void *slicePtr(const void *self) noexcept { return cxxbridge1$slice$ptr(self); } |
| 317 | |
| 318 | std::size_t sliceLen(const void *self) noexcept { |
| 319 | return cxxbridge1$slice$len(self); |
| 320 | } |
| 321 | |
| David Tolnay | 03c43f5 | 2020-12-12 21:07:17 -0800 | [diff] [blame] | 322 | // Rust specifies that usize is ABI compatible with C's uintptr_t. |
| 323 | // https://rust-lang.github.io/unsafe-code-guidelines/layout/scalars.html#isize-and-usize |
| 324 | // However there is no direct Rust equivalent for size_t. C does not guarantee |
| 325 | // that size_t and uintptr_t are compatible. In practice though, on all |
| 326 | // platforms supported by Rust, they are identical for ABI purposes. See the |
| 327 | // libc crate which unconditionally defines libc::size_t = usize. We expect the |
| 328 | // same here and these assertions are just here to explicitly document that. |
| 329 | // *Note that no assumption is made about C++ name mangling of signatures |
| 330 | // containing these types, not here nor anywhere in CXX.* |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 331 | static_assert(sizeof(std::size_t) == sizeof(std::uintptr_t), |
| 332 | "unsupported size_t size"); |
| 333 | static_assert(alignof(std::size_t) == alignof(std::uintptr_t), |
| David Tolnay | 03c43f5 | 2020-12-12 21:07:17 -0800 | [diff] [blame] | 334 | "unsupported size_t alignment"); |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 335 | static_assert(sizeof(rust::isize) == sizeof(std::intptr_t), |
| David Tolnay | 03c43f5 | 2020-12-12 21:07:17 -0800 | [diff] [blame] | 336 | "unsupported ssize_t size"); |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 337 | static_assert(alignof(rust::isize) == alignof(std::intptr_t), |
| David Tolnay | 03c43f5 | 2020-12-12 21:07:17 -0800 | [diff] [blame] | 338 | "unsupported ssize_t alignment"); |
| 339 | |
| David Tolnay | 9ed15c6 | 2020-10-31 18:02:03 -0700 | [diff] [blame] | 340 | static_assert(std::is_trivially_copy_constructible<Str>::value, |
| 341 | "trivial Str(const Str &)"); |
| 342 | static_assert(std::is_trivially_copy_assignable<Str>::value, |
| 343 | "trivial operator=(const Str &)"); |
| 344 | static_assert(std::is_trivially_destructible<Str>::value, "trivial ~Str()"); |
| 345 | |
| David Tolnay | 9578cf1 | 2020-11-25 14:36:46 -0800 | [diff] [blame] | 346 | static_assert( |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 347 | std::is_trivially_copy_constructible<Slice<const std::uint8_t>>::value, |
| 348 | "trivial Slice(const Slice &)"); |
| 349 | static_assert( |
| 350 | std::is_trivially_move_constructible<Slice<const std::uint8_t>>::value, |
| 351 | "trivial Slice(Slice &&)"); |
| 352 | static_assert( |
| 353 | std::is_trivially_copy_assignable<Slice<const std::uint8_t>>::value, |
| 354 | "trivial Slice::operator=(const Slice &) for const slices"); |
| 355 | static_assert( |
| 356 | std::is_trivially_move_assignable<Slice<const std::uint8_t>>::value, |
| 357 | "trivial Slice::operator=(Slice &&)"); |
| 358 | static_assert(std::is_trivially_destructible<Slice<const std::uint8_t>>::value, |
| 359 | "trivial ~Slice()"); |
| 360 | |
| 361 | static_assert(std::is_trivially_copy_constructible<Slice<std::uint8_t>>::value, |
| 362 | "trivial Slice(const Slice &)"); |
| 363 | static_assert(std::is_trivially_move_constructible<Slice<std::uint8_t>>::value, |
| 364 | "trivial Slice(Slice &&)"); |
| 365 | static_assert(!std::is_copy_assignable<Slice<std::uint8_t>>::value, |
| 366 | "delete Slice::operator=(const Slice &) for mut slices"); |
| 367 | static_assert(std::is_trivially_move_assignable<Slice<std::uint8_t>>::value, |
| 368 | "trivial Slice::operator=(Slice &&)"); |
| 369 | static_assert(std::is_trivially_destructible<Slice<std::uint8_t>>::value, |
| 370 | "trivial ~Slice()"); |
| 371 | |
| 372 | static_assert(std::is_same<Vec<std::uint8_t>::const_iterator, |
| 373 | Vec<const std::uint8_t>::iterator>::value, |
| 374 | "Vec<T>::const_iterator == Vec<const T>::iterator"); |
| 375 | static_assert(std::is_same<Vec<const std::uint8_t>::const_iterator, |
| 376 | Vec<const std::uint8_t>::iterator>::value, |
| 377 | "Vec<const T>::const_iterator == Vec<const T>::iterator"); |
| 378 | static_assert(!std::is_same<Vec<std::uint8_t>::const_iterator, |
| 379 | Vec<std::uint8_t>::iterator>::value, |
| 380 | "Vec<T>::const_iterator != Vec<T>::iterator"); |
| David Tolnay | 9578cf1 | 2020-11-25 14:36:46 -0800 | [diff] [blame] | 381 | |
| David Tolnay | 4ddae80 | 2021-01-02 16:47:33 -0800 | [diff] [blame] | 382 | static const char *errorCopy(const char *ptr, std::size_t len) { |
| 383 | char *copy = new char[len]; |
| 384 | std::memcpy(copy, ptr, len); |
| 385 | return copy; |
| 386 | } |
| 387 | |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 388 | extern "C" { |
| David Tolnay | dc2d4c3 | 2021-01-02 16:13:27 -0800 | [diff] [blame] | 389 | const char *cxxbridge1$error(const char *ptr, std::size_t len) noexcept { |
| David Tolnay | 4ddae80 | 2021-01-02 16:47:33 -0800 | [diff] [blame] | 390 | return errorCopy(ptr, len); |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 391 | } |
| 392 | } // extern "C" |
| 393 | |
| David Tolnay | d5712ee | 2020-10-31 17:10:00 -0700 | [diff] [blame] | 394 | Error::Error(const Error &other) |
| David Tolnay | 4ddae80 | 2021-01-02 16:47:33 -0800 | [diff] [blame] | 395 | : std::exception(other), |
| 396 | msg(other.msg ? errorCopy(other.msg, other.len) : nullptr), |
| David Tolnay | 23c2319 | 2020-10-31 17:11:48 -0700 | [diff] [blame] | 397 | len(other.len) {} |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 398 | |
| David Tolnay | 23c2319 | 2020-10-31 17:11:48 -0700 | [diff] [blame] | 399 | Error::Error(Error &&other) noexcept |
| 400 | : std::exception(std::move(other)), msg(other.msg), len(other.len) { |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 401 | other.msg = nullptr; |
| 402 | other.len = 0; |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 405 | Error::~Error() noexcept { delete[] this->msg; } |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 406 | |
| David Tolnay | 63bec40 | 2021-02-11 23:18:04 -0800 | [diff] [blame] | 407 | Error &Error::operator=(const Error &other) & { |
| David Tolnay | 7c6ac71 | 2020-10-31 17:22:28 -0700 | [diff] [blame] | 408 | if (this != &other) { |
| 409 | std::exception::operator=(other); |
| 410 | delete[] this->msg; |
| 411 | this->msg = nullptr; |
| David Tolnay | 4ddae80 | 2021-01-02 16:47:33 -0800 | [diff] [blame] | 412 | if (other.msg) { |
| 413 | this->msg = errorCopy(other.msg, other.len); |
| 414 | this->len = other.len; |
| 415 | } |
| David Tolnay | 7c6ac71 | 2020-10-31 17:22:28 -0700 | [diff] [blame] | 416 | } |
| 417 | return *this; |
| 418 | } |
| 419 | |
| David Tolnay | 5d12f1d | 2021-03-22 14:58:38 -0700 | [diff] [blame] | 420 | Error &Error::operator=(Error &&other) &noexcept { |
| David Tolnay | 7bc397c | 2021-02-11 23:07:58 -0800 | [diff] [blame] | 421 | std::exception::operator=(std::move(other)); |
| 422 | this->msg = other.msg; |
| 423 | this->len = other.len; |
| 424 | other.msg = nullptr; |
| 425 | other.len = 0; |
| David Tolnay | 1549106 | 2020-10-31 17:25:13 -0700 | [diff] [blame] | 426 | return *this; |
| 427 | } |
| 428 | |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 429 | const char *Error::what() const noexcept { return this->msg; } |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 430 | |
| David Tolnay | 5b16340 | 2020-12-10 19:26:02 -0800 | [diff] [blame] | 431 | namespace { |
| 432 | template <typename T> |
| 433 | union MaybeUninit { |
| 434 | T value; |
| 435 | MaybeUninit() {} |
| 436 | ~MaybeUninit() {} |
| 437 | }; |
| 438 | } // namespace |
| 439 | |
| David Tolnay | 60f8359 | 2021-02-08 23:00:01 -0800 | [diff] [blame] | 440 | namespace detail { |
| 441 | // On some platforms size_t is the same C++ type as one of the sized integer |
| 442 | // types; on others it is a distinct type. Only in the latter case do we need to |
| 443 | // define a specialized impl of rust::Vec<size_t>, because in the former case it |
| 444 | // would collide with one of the other specializations. |
| 445 | using usize_if_unique = |
| 446 | typename std::conditional<std::is_same<size_t, uint64_t>::value || |
| 447 | std::is_same<size_t, uint32_t>::value, |
| 448 | struct usize_ignore, size_t>::type; |
| 449 | using isize_if_unique = |
| 450 | typename std::conditional<std::is_same<rust::isize, int64_t>::value || |
| 451 | std::is_same<rust::isize, int32_t>::value, |
| 452 | struct isize_ignore, rust::isize>::type; |
| 453 | } // namespace detail |
| 454 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 455 | } // namespace cxxbridge1 |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 456 | } // namespace rust |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 457 | |
| David Tolnay | fc26d6d | 2021-04-15 21:18:45 -0700 | [diff] [blame] | 458 | namespace { |
| 459 | template <typename T> |
| 460 | void destroy(T *ptr) { |
| 461 | ptr->~T(); |
| 462 | } |
| 463 | } // namespace |
| 464 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 465 | extern "C" { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 466 | void cxxbridge1$unique_ptr$std$string$null( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 467 | std::unique_ptr<std::string> *ptr) noexcept { |
| 468 | new (ptr) std::unique_ptr<std::string>(); |
| 469 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 470 | void cxxbridge1$unique_ptr$std$string$raw(std::unique_ptr<std::string> *ptr, |
| 471 | std::string *raw) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 472 | new (ptr) std::unique_ptr<std::string>(raw); |
| 473 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 474 | const std::string *cxxbridge1$unique_ptr$std$string$get( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 475 | const std::unique_ptr<std::string> &ptr) noexcept { |
| 476 | return ptr.get(); |
| 477 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 478 | std::string *cxxbridge1$unique_ptr$std$string$release( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 479 | std::unique_ptr<std::string> &ptr) noexcept { |
| 480 | return ptr.release(); |
| 481 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 482 | void cxxbridge1$unique_ptr$std$string$drop( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 483 | std::unique_ptr<std::string> *ptr) noexcept { |
| 484 | ptr->~unique_ptr(); |
| 485 | } |
| 486 | } // extern "C" |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 487 | |
| David Tolnay | 06677b3 | 2020-11-23 18:05:45 -0800 | [diff] [blame] | 488 | namespace { |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 489 | const std::size_t kMaxExpectedWordsInString = 8; |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame] | 490 | static_assert(alignof(std::string) <= alignof(void *), |
| 491 | "unexpectedly large std::string alignment"); |
| David Tolnay | 06677b3 | 2020-11-23 18:05:45 -0800 | [diff] [blame] | 492 | static_assert(sizeof(std::string) <= kMaxExpectedWordsInString * sizeof(void *), |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame] | 493 | "unexpectedly large std::string size"); |
| David Tolnay | c26de54 | 2020-11-23 18:18:19 -0800 | [diff] [blame] | 494 | } // namespace |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame] | 495 | |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 496 | #define STD_VECTOR_OPS(RUST_TYPE, CXX_TYPE) \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 497 | std::size_t cxxbridge1$std$vector$##RUST_TYPE##$size( \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 498 | const std::vector<CXX_TYPE> &s) noexcept { \ |
| 499 | return s.size(); \ |
| 500 | } \ |
| David Tolnay | 767e00d | 2020-12-21 17:12:27 -0800 | [diff] [blame] | 501 | CXX_TYPE *cxxbridge1$std$vector$##RUST_TYPE##$get_unchecked( \ |
| 502 | std::vector<CXX_TYPE> *s, std::size_t pos) noexcept { \ |
| 503 | return &(*s)[pos]; \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 504 | } \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 505 | void cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$null( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 506 | std::unique_ptr<std::vector<CXX_TYPE>> *ptr) noexcept { \ |
| 507 | new (ptr) std::unique_ptr<std::vector<CXX_TYPE>>(); \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 508 | } \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 509 | void cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$raw( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 510 | std::unique_ptr<std::vector<CXX_TYPE>> *ptr, \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 511 | std::vector<CXX_TYPE> *raw) noexcept { \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 512 | new (ptr) std::unique_ptr<std::vector<CXX_TYPE>>(raw); \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 513 | } \ |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 514 | const std::vector<CXX_TYPE> \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 515 | *cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$get( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 516 | const std::unique_ptr<std::vector<CXX_TYPE>> &ptr) noexcept { \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 517 | return ptr.get(); \ |
| 518 | } \ |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 519 | std::vector<CXX_TYPE> \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 520 | *cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$release( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 521 | std::unique_ptr<std::vector<CXX_TYPE>> &ptr) noexcept { \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 522 | return ptr.release(); \ |
| 523 | } \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 524 | void cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$drop( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 525 | std::unique_ptr<std::vector<CXX_TYPE>> *ptr) noexcept { \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 526 | ptr->~unique_ptr(); \ |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 527 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 528 | |
| David Tolnay | fc26d6d | 2021-04-15 21:18:45 -0700 | [diff] [blame] | 529 | #define STD_VECTOR_TRIVIAL_OPS(RUST_TYPE, CXX_TYPE) \ |
| 530 | void cxxbridge1$std$vector$##RUST_TYPE##$push_back( \ |
| 531 | std::vector<CXX_TYPE> *v, CXX_TYPE *value) noexcept { \ |
| 532 | v->push_back(std::move(*value)); \ |
| 533 | destroy(value); \ |
| David Tolnay | 9521519 | 2021-04-16 15:40:12 -0700 | [diff] [blame] | 534 | } \ |
| 535 | void cxxbridge1$std$vector$##RUST_TYPE##$pop_back(std::vector<CXX_TYPE> *v, \ |
| 536 | CXX_TYPE *out) noexcept { \ |
| 537 | new (out) CXX_TYPE(std::move(v->back())); \ |
| 538 | v->pop_back(); \ |
| David Tolnay | fc26d6d | 2021-04-15 21:18:45 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 541 | #define RUST_VEC_EXTERNS(RUST_TYPE, CXX_TYPE) \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 542 | void cxxbridge1$rust_vec$##RUST_TYPE##$new( \ |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 543 | rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 544 | void cxxbridge1$rust_vec$##RUST_TYPE##$drop( \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 545 | rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 546 | std::size_t cxxbridge1$rust_vec$##RUST_TYPE##$len( \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 547 | const rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 548 | std::size_t cxxbridge1$rust_vec$##RUST_TYPE##$capacity( \ |
| David Tolnay | dc62d71 | 2020-12-11 13:51:53 -0800 | [diff] [blame] | 549 | const rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 550 | const CXX_TYPE *cxxbridge1$rust_vec$##RUST_TYPE##$data( \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 551 | const rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 552 | void cxxbridge1$rust_vec$##RUST_TYPE##$reserve_total( \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 553 | rust::Vec<CXX_TYPE> *ptr, std::size_t cap) noexcept; \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 554 | void cxxbridge1$rust_vec$##RUST_TYPE##$set_len(rust::Vec<CXX_TYPE> *ptr, \ |
| David Tolnay | e1df7dd | 2020-12-27 02:51:51 -0800 | [diff] [blame] | 555 | std::size_t len) noexcept; |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 556 | |
| 557 | #define RUST_VEC_OPS(RUST_TYPE, CXX_TYPE) \ |
| 558 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 559 | Vec<CXX_TYPE>::Vec() noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 560 | cxxbridge1$rust_vec$##RUST_TYPE##$new(this); \ |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 561 | } \ |
| 562 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 563 | void Vec<CXX_TYPE>::drop() noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 564 | return cxxbridge1$rust_vec$##RUST_TYPE##$drop(this); \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 565 | } \ |
| 566 | template <> \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 567 | std::size_t Vec<CXX_TYPE>::size() const noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 568 | return cxxbridge1$rust_vec$##RUST_TYPE##$len(this); \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 569 | } \ |
| 570 | template <> \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 571 | std::size_t Vec<CXX_TYPE>::capacity() const noexcept { \ |
| David Tolnay | dc62d71 | 2020-12-11 13:51:53 -0800 | [diff] [blame] | 572 | return cxxbridge1$rust_vec$##RUST_TYPE##$capacity(this); \ |
| 573 | } \ |
| 574 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 575 | const CXX_TYPE *Vec<CXX_TYPE>::data() const noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 576 | return cxxbridge1$rust_vec$##RUST_TYPE##$data(this); \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 577 | } \ |
| 578 | template <> \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 579 | void Vec<CXX_TYPE>::reserve_total(std::size_t cap) noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 580 | cxxbridge1$rust_vec$##RUST_TYPE##$reserve_total(this, cap); \ |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 581 | } \ |
| 582 | template <> \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 583 | void Vec<CXX_TYPE>::set_len(std::size_t len) noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 584 | cxxbridge1$rust_vec$##RUST_TYPE##$set_len(this, len); \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| David Tolnay | 5b16340 | 2020-12-10 19:26:02 -0800 | [diff] [blame] | 587 | #define SHARED_PTR_OPS(RUST_TYPE, CXX_TYPE) \ |
| 588 | static_assert(sizeof(std::shared_ptr<CXX_TYPE>) == 2 * sizeof(void *), ""); \ |
| 589 | static_assert(alignof(std::shared_ptr<CXX_TYPE>) == alignof(void *), ""); \ |
| 590 | void cxxbridge1$std$shared_ptr$##RUST_TYPE##$null( \ |
| 591 | std::shared_ptr<CXX_TYPE> *ptr) noexcept { \ |
| 592 | new (ptr) std::shared_ptr<CXX_TYPE>(); \ |
| 593 | } \ |
| 594 | CXX_TYPE *cxxbridge1$std$shared_ptr$##RUST_TYPE##$uninit( \ |
| 595 | std::shared_ptr<CXX_TYPE> *ptr) noexcept { \ |
| 596 | CXX_TYPE *uninit = \ |
| 597 | reinterpret_cast<CXX_TYPE *>(new rust::MaybeUninit<CXX_TYPE>); \ |
| 598 | new (ptr) std::shared_ptr<CXX_TYPE>(uninit); \ |
| 599 | return uninit; \ |
| 600 | } \ |
| 601 | void cxxbridge1$std$shared_ptr$##RUST_TYPE##$clone( \ |
| 602 | const std::shared_ptr<CXX_TYPE> &self, \ |
| 603 | std::shared_ptr<CXX_TYPE> *ptr) noexcept { \ |
| 604 | new (ptr) std::shared_ptr<CXX_TYPE>(self); \ |
| 605 | } \ |
| 606 | const CXX_TYPE *cxxbridge1$std$shared_ptr$##RUST_TYPE##$get( \ |
| 607 | const std::shared_ptr<CXX_TYPE> &self) noexcept { \ |
| 608 | return self.get(); \ |
| 609 | } \ |
| 610 | void cxxbridge1$std$shared_ptr$##RUST_TYPE##$drop( \ |
| 611 | const std::shared_ptr<CXX_TYPE> *self) noexcept { \ |
| 612 | self->~shared_ptr(); \ |
| David Tolnay | 215e77f | 2020-12-28 17:09:48 -0800 | [diff] [blame] | 613 | } \ |
| 614 | static_assert(sizeof(std::weak_ptr<CXX_TYPE>) == 2 * sizeof(void *), ""); \ |
| 615 | static_assert(alignof(std::weak_ptr<CXX_TYPE>) == alignof(void *), ""); \ |
| 616 | void cxxbridge1$std$weak_ptr$##RUST_TYPE##$null( \ |
| 617 | std::weak_ptr<CXX_TYPE> *ptr) noexcept { \ |
| 618 | new (ptr) std::weak_ptr<CXX_TYPE>(); \ |
| 619 | } \ |
| 620 | void cxxbridge1$std$weak_ptr$##RUST_TYPE##$clone( \ |
| 621 | const std::weak_ptr<CXX_TYPE> &self, \ |
| 622 | std::weak_ptr<CXX_TYPE> *ptr) noexcept { \ |
| 623 | new (ptr) std::weak_ptr<CXX_TYPE>(self); \ |
| 624 | } \ |
| David Tolnay | 85b6bc4 | 2020-12-28 17:47:51 -0800 | [diff] [blame] | 625 | void cxxbridge1$std$weak_ptr$##RUST_TYPE##$downgrade( \ |
| 626 | const std::shared_ptr<CXX_TYPE> &shared, \ |
| 627 | std::weak_ptr<CXX_TYPE> *weak) noexcept { \ |
| 628 | new (weak) std::weak_ptr<CXX_TYPE>(shared); \ |
| 629 | } \ |
| David Tolnay | 7a48785 | 2020-12-28 18:02:25 -0800 | [diff] [blame] | 630 | void cxxbridge1$std$weak_ptr$##RUST_TYPE##$upgrade( \ |
| 631 | const std::weak_ptr<CXX_TYPE> &weak, \ |
| 632 | std::shared_ptr<CXX_TYPE> *shared) noexcept { \ |
| 633 | new (shared) std::shared_ptr<CXX_TYPE>(weak.lock()); \ |
| 634 | } \ |
| David Tolnay | 215e77f | 2020-12-28 17:09:48 -0800 | [diff] [blame] | 635 | void cxxbridge1$std$weak_ptr$##RUST_TYPE##$drop( \ |
| 636 | const std::weak_ptr<CXX_TYPE> *self) noexcept { \ |
| 637 | self->~weak_ptr(); \ |
| David Tolnay | 5b16340 | 2020-12-10 19:26:02 -0800 | [diff] [blame] | 638 | } |
| 639 | |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 640 | // Usize and isize are the same type as one of the below. |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 641 | #define FOR_EACH_NUMERIC(MACRO) \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 642 | MACRO(u8, std::uint8_t) \ |
| 643 | MACRO(u16, std::uint16_t) \ |
| 644 | MACRO(u32, std::uint32_t) \ |
| 645 | MACRO(u64, std::uint64_t) \ |
| 646 | MACRO(i8, std::int8_t) \ |
| 647 | MACRO(i16, std::int16_t) \ |
| 648 | MACRO(i32, std::int32_t) \ |
| 649 | MACRO(i64, std::int64_t) \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 650 | MACRO(f32, float) \ |
| 651 | MACRO(f64, double) |
| 652 | |
| David Tolnay | fc26d6d | 2021-04-15 21:18:45 -0700 | [diff] [blame] | 653 | #define FOR_EACH_TRIVIAL_STD_VECTOR(MACRO) \ |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 654 | FOR_EACH_NUMERIC(MACRO) \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 655 | MACRO(usize, std::size_t) \ |
| David Tolnay | fc26d6d | 2021-04-15 21:18:45 -0700 | [diff] [blame] | 656 | MACRO(isize, rust::isize) |
| 657 | |
| 658 | #define FOR_EACH_STD_VECTOR(MACRO) \ |
| 659 | FOR_EACH_TRIVIAL_STD_VECTOR(MACRO) \ |
| David Tolnay | 47e239d | 2020-08-28 00:32:04 -0700 | [diff] [blame] | 660 | MACRO(string, std::string) |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 661 | |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 662 | #define FOR_EACH_RUST_VEC(MACRO) \ |
| 663 | FOR_EACH_NUMERIC(MACRO) \ |
| David Tolnay | 33f56ad | 2020-08-27 17:06:35 -0700 | [diff] [blame] | 664 | MACRO(bool, bool) \ |
| David Tolnay | 93e71d0 | 2020-11-25 20:16:52 -0800 | [diff] [blame] | 665 | MACRO(char, char) \ |
| David Tolnay | 60f8359 | 2021-02-08 23:00:01 -0800 | [diff] [blame] | 666 | MACRO(usize, rust::detail::usize_if_unique) \ |
| 667 | MACRO(isize, rust::detail::isize_if_unique) \ |
| David Tolnay | 2d1fcc2 | 2021-01-03 22:43:19 -0800 | [diff] [blame] | 668 | MACRO(string, rust::String) \ |
| 669 | MACRO(str, rust::Str) |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 670 | |
| David Tolnay | 5b16340 | 2020-12-10 19:26:02 -0800 | [diff] [blame] | 671 | #define FOR_EACH_SHARED_PTR(MACRO) \ |
| 672 | FOR_EACH_NUMERIC(MACRO) \ |
| David Tolnay | cd1430c | 2020-12-28 17:17:14 -0800 | [diff] [blame] | 673 | MACRO(bool, bool) \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 674 | MACRO(usize, std::size_t) \ |
| David Tolnay | 5b16340 | 2020-12-10 19:26:02 -0800 | [diff] [blame] | 675 | MACRO(isize, rust::isize) \ |
| 676 | MACRO(string, std::string) |
| 677 | |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 678 | extern "C" { |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 679 | FOR_EACH_STD_VECTOR(STD_VECTOR_OPS) |
| David Tolnay | fc26d6d | 2021-04-15 21:18:45 -0700 | [diff] [blame] | 680 | FOR_EACH_TRIVIAL_STD_VECTOR(STD_VECTOR_TRIVIAL_OPS) |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 681 | FOR_EACH_RUST_VEC(RUST_VEC_EXTERNS) |
| David Tolnay | 5b16340 | 2020-12-10 19:26:02 -0800 | [diff] [blame] | 682 | FOR_EACH_SHARED_PTR(SHARED_PTR_OPS) |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 683 | } // extern "C" |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 684 | |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 685 | namespace rust { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 686 | inline namespace cxxbridge1 { |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 687 | FOR_EACH_RUST_VEC(RUST_VEC_OPS) |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 688 | } // namespace cxxbridge1 |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 689 | } // namespace rust |