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