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