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