| 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 | 0413ee2 | 2021-01-02 13:55:16 -0800 | [diff] [blame] | 276 | void Str::swap(Str &rhs) noexcept { std::swap(*this, rhs); } |
| 277 | |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 278 | std::ostream &operator<<(std::ostream &os, const Str &s) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 279 | os.write(s.data(), s.size()); |
| 280 | return os; |
| 281 | } |
| 282 | |
| David Tolnay | 9bffb93 | 2021-01-02 02:15:21 -0800 | [diff] [blame] | 283 | void sliceInit(void *self, const void *ptr, std::size_t len) noexcept { |
| 284 | cxxbridge1$slice$new(self, ptr, len); |
| 285 | } |
| 286 | |
| 287 | void *slicePtr(const void *self) noexcept { return cxxbridge1$slice$ptr(self); } |
| 288 | |
| 289 | std::size_t sliceLen(const void *self) noexcept { |
| 290 | return cxxbridge1$slice$len(self); |
| 291 | } |
| 292 | |
| David Tolnay | 03c43f5 | 2020-12-12 21:07:17 -0800 | [diff] [blame] | 293 | // Rust specifies that usize is ABI compatible with C's uintptr_t. |
| 294 | // https://rust-lang.github.io/unsafe-code-guidelines/layout/scalars.html#isize-and-usize |
| 295 | // However there is no direct Rust equivalent for size_t. C does not guarantee |
| 296 | // that size_t and uintptr_t are compatible. In practice though, on all |
| 297 | // platforms supported by Rust, they are identical for ABI purposes. See the |
| 298 | // libc crate which unconditionally defines libc::size_t = usize. We expect the |
| 299 | // same here and these assertions are just here to explicitly document that. |
| 300 | // *Note that no assumption is made about C++ name mangling of signatures |
| 301 | // containing these types, not here nor anywhere in CXX.* |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 302 | static_assert(sizeof(std::size_t) == sizeof(std::uintptr_t), |
| 303 | "unsupported size_t size"); |
| 304 | static_assert(alignof(std::size_t) == alignof(std::uintptr_t), |
| David Tolnay | 03c43f5 | 2020-12-12 21:07:17 -0800 | [diff] [blame] | 305 | "unsupported size_t alignment"); |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 306 | static_assert(sizeof(rust::isize) == sizeof(std::intptr_t), |
| David Tolnay | 03c43f5 | 2020-12-12 21:07:17 -0800 | [diff] [blame] | 307 | "unsupported ssize_t size"); |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 308 | static_assert(alignof(rust::isize) == alignof(std::intptr_t), |
| David Tolnay | 03c43f5 | 2020-12-12 21:07:17 -0800 | [diff] [blame] | 309 | "unsupported ssize_t alignment"); |
| 310 | |
| David Tolnay | 9ed15c6 | 2020-10-31 18:02:03 -0700 | [diff] [blame] | 311 | static_assert(std::is_trivially_copy_constructible<Str>::value, |
| 312 | "trivial Str(const Str &)"); |
| 313 | static_assert(std::is_trivially_copy_assignable<Str>::value, |
| 314 | "trivial operator=(const Str &)"); |
| 315 | static_assert(std::is_trivially_destructible<Str>::value, "trivial ~Str()"); |
| 316 | |
| David Tolnay | 9578cf1 | 2020-11-25 14:36:46 -0800 | [diff] [blame] | 317 | static_assert( |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 318 | std::is_trivially_copy_constructible<Slice<const std::uint8_t>>::value, |
| 319 | "trivial Slice(const Slice &)"); |
| 320 | static_assert( |
| 321 | std::is_trivially_move_constructible<Slice<const std::uint8_t>>::value, |
| 322 | "trivial Slice(Slice &&)"); |
| 323 | static_assert( |
| 324 | std::is_trivially_copy_assignable<Slice<const std::uint8_t>>::value, |
| 325 | "trivial Slice::operator=(const Slice &) for const slices"); |
| 326 | static_assert( |
| 327 | std::is_trivially_move_assignable<Slice<const std::uint8_t>>::value, |
| 328 | "trivial Slice::operator=(Slice &&)"); |
| 329 | static_assert(std::is_trivially_destructible<Slice<const std::uint8_t>>::value, |
| 330 | "trivial ~Slice()"); |
| 331 | |
| 332 | static_assert(std::is_trivially_copy_constructible<Slice<std::uint8_t>>::value, |
| 333 | "trivial Slice(const Slice &)"); |
| 334 | static_assert(std::is_trivially_move_constructible<Slice<std::uint8_t>>::value, |
| 335 | "trivial Slice(Slice &&)"); |
| 336 | static_assert(!std::is_copy_assignable<Slice<std::uint8_t>>::value, |
| 337 | "delete Slice::operator=(const Slice &) for mut slices"); |
| 338 | static_assert(std::is_trivially_move_assignable<Slice<std::uint8_t>>::value, |
| 339 | "trivial Slice::operator=(Slice &&)"); |
| 340 | static_assert(std::is_trivially_destructible<Slice<std::uint8_t>>::value, |
| 341 | "trivial ~Slice()"); |
| 342 | |
| 343 | static_assert(std::is_same<Vec<std::uint8_t>::const_iterator, |
| 344 | Vec<const std::uint8_t>::iterator>::value, |
| 345 | "Vec<T>::const_iterator == Vec<const T>::iterator"); |
| 346 | static_assert(std::is_same<Vec<const std::uint8_t>::const_iterator, |
| 347 | Vec<const std::uint8_t>::iterator>::value, |
| 348 | "Vec<const T>::const_iterator == Vec<const T>::iterator"); |
| 349 | static_assert(!std::is_same<Vec<std::uint8_t>::const_iterator, |
| 350 | Vec<std::uint8_t>::iterator>::value, |
| 351 | "Vec<T>::const_iterator != Vec<T>::iterator"); |
| David Tolnay | 9578cf1 | 2020-11-25 14:36:46 -0800 | [diff] [blame] | 352 | |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 353 | extern "C" { |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 354 | const char *cxxbridge1$error(const char *ptr, std::size_t len) { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 355 | char *copy = new char[len]; |
| David Tolnay | 504cf3c | 2020-10-31 16:08:04 -0700 | [diff] [blame] | 356 | std::strncpy(copy, ptr, len); |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 357 | return copy; |
| 358 | } |
| 359 | } // extern "C" |
| 360 | |
| David Tolnay | d5712ee | 2020-10-31 17:10:00 -0700 | [diff] [blame] | 361 | Error::Error(const Error &other) |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 362 | : std::exception(other), msg(cxxbridge1$error(other.msg, other.len)), |
| David Tolnay | 23c2319 | 2020-10-31 17:11:48 -0700 | [diff] [blame] | 363 | len(other.len) {} |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 364 | |
| David Tolnay | 23c2319 | 2020-10-31 17:11:48 -0700 | [diff] [blame] | 365 | Error::Error(Error &&other) noexcept |
| 366 | : std::exception(std::move(other)), msg(other.msg), len(other.len) { |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 367 | other.msg = nullptr; |
| 368 | other.len = 0; |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 371 | Error::~Error() noexcept { delete[] this->msg; } |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 372 | |
| David Tolnay | 7c6ac71 | 2020-10-31 17:22:28 -0700 | [diff] [blame] | 373 | Error &Error::operator=(const Error &other) { |
| 374 | if (this != &other) { |
| 375 | std::exception::operator=(other); |
| 376 | delete[] this->msg; |
| 377 | this->msg = nullptr; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 378 | this->msg = cxxbridge1$error(other.msg, other.len); |
| David Tolnay | 7c6ac71 | 2020-10-31 17:22:28 -0700 | [diff] [blame] | 379 | this->len = other.len; |
| 380 | } |
| 381 | return *this; |
| 382 | } |
| 383 | |
| David Tolnay | 1549106 | 2020-10-31 17:25:13 -0700 | [diff] [blame] | 384 | Error &Error::operator=(Error &&other) noexcept { |
| 385 | if (this != &other) { |
| 386 | std::exception::operator=(std::move(other)); |
| 387 | this->msg = other.msg; |
| 388 | this->len = other.len; |
| 389 | other.msg = nullptr; |
| 390 | other.len = 0; |
| 391 | } |
| 392 | return *this; |
| 393 | } |
| 394 | |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 395 | const char *Error::what() const noexcept { return this->msg; } |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 396 | |
| David Tolnay | 5b16340 | 2020-12-10 19:26:02 -0800 | [diff] [blame] | 397 | namespace { |
| 398 | template <typename T> |
| 399 | union MaybeUninit { |
| 400 | T value; |
| 401 | MaybeUninit() {} |
| 402 | ~MaybeUninit() {} |
| 403 | }; |
| 404 | } // namespace |
| 405 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 406 | } // namespace cxxbridge1 |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 407 | } // namespace rust |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 408 | |
| 409 | extern "C" { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 410 | void cxxbridge1$unique_ptr$std$string$null( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 411 | std::unique_ptr<std::string> *ptr) noexcept { |
| 412 | new (ptr) std::unique_ptr<std::string>(); |
| 413 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 414 | void cxxbridge1$unique_ptr$std$string$raw(std::unique_ptr<std::string> *ptr, |
| 415 | std::string *raw) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 416 | new (ptr) std::unique_ptr<std::string>(raw); |
| 417 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 418 | const std::string *cxxbridge1$unique_ptr$std$string$get( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 419 | const std::unique_ptr<std::string> &ptr) noexcept { |
| 420 | return ptr.get(); |
| 421 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 422 | std::string *cxxbridge1$unique_ptr$std$string$release( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 423 | std::unique_ptr<std::string> &ptr) noexcept { |
| 424 | return ptr.release(); |
| 425 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 426 | void cxxbridge1$unique_ptr$std$string$drop( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 427 | std::unique_ptr<std::string> *ptr) noexcept { |
| 428 | ptr->~unique_ptr(); |
| 429 | } |
| 430 | } // extern "C" |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 431 | |
| David Tolnay | 06677b3 | 2020-11-23 18:05:45 -0800 | [diff] [blame] | 432 | namespace { |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 433 | const std::size_t kMaxExpectedWordsInString = 8; |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame] | 434 | static_assert(alignof(std::string) <= alignof(void *), |
| 435 | "unexpectedly large std::string alignment"); |
| David Tolnay | 06677b3 | 2020-11-23 18:05:45 -0800 | [diff] [blame] | 436 | static_assert(sizeof(std::string) <= kMaxExpectedWordsInString * sizeof(void *), |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame] | 437 | "unexpectedly large std::string size"); |
| David Tolnay | c26de54 | 2020-11-23 18:18:19 -0800 | [diff] [blame] | 438 | } // namespace |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame] | 439 | |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 440 | #define STD_VECTOR_OPS(RUST_TYPE, CXX_TYPE) \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 441 | std::size_t cxxbridge1$std$vector$##RUST_TYPE##$size( \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 442 | const std::vector<CXX_TYPE> &s) noexcept { \ |
| 443 | return s.size(); \ |
| 444 | } \ |
| David Tolnay | 767e00d | 2020-12-21 17:12:27 -0800 | [diff] [blame] | 445 | CXX_TYPE *cxxbridge1$std$vector$##RUST_TYPE##$get_unchecked( \ |
| 446 | std::vector<CXX_TYPE> *s, std::size_t pos) noexcept { \ |
| 447 | return &(*s)[pos]; \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 448 | } \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 449 | void cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$null( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 450 | std::unique_ptr<std::vector<CXX_TYPE>> *ptr) noexcept { \ |
| 451 | new (ptr) std::unique_ptr<std::vector<CXX_TYPE>>(); \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 452 | } \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 453 | void cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$raw( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 454 | std::unique_ptr<std::vector<CXX_TYPE>> *ptr, \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 455 | std::vector<CXX_TYPE> *raw) noexcept { \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 456 | new (ptr) std::unique_ptr<std::vector<CXX_TYPE>>(raw); \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 457 | } \ |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 458 | const std::vector<CXX_TYPE> \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 459 | *cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$get( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 460 | const std::unique_ptr<std::vector<CXX_TYPE>> &ptr) noexcept { \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 461 | return ptr.get(); \ |
| 462 | } \ |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 463 | std::vector<CXX_TYPE> \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 464 | *cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$release( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 465 | std::unique_ptr<std::vector<CXX_TYPE>> &ptr) noexcept { \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 466 | return ptr.release(); \ |
| 467 | } \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 468 | void cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$drop( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 469 | std::unique_ptr<std::vector<CXX_TYPE>> *ptr) noexcept { \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 470 | ptr->~unique_ptr(); \ |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 471 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 472 | |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 473 | #define RUST_VEC_EXTERNS(RUST_TYPE, CXX_TYPE) \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 474 | void cxxbridge1$rust_vec$##RUST_TYPE##$new( \ |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 475 | rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 476 | void cxxbridge1$rust_vec$##RUST_TYPE##$drop( \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 477 | rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 478 | std::size_t cxxbridge1$rust_vec$##RUST_TYPE##$len( \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 479 | const rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 480 | std::size_t cxxbridge1$rust_vec$##RUST_TYPE##$capacity( \ |
| David Tolnay | dc62d71 | 2020-12-11 13:51:53 -0800 | [diff] [blame] | 481 | const rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 482 | const CXX_TYPE *cxxbridge1$rust_vec$##RUST_TYPE##$data( \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 483 | const rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 484 | void cxxbridge1$rust_vec$##RUST_TYPE##$reserve_total( \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 485 | rust::Vec<CXX_TYPE> *ptr, std::size_t cap) noexcept; \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 486 | 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] | 487 | std::size_t len) noexcept; |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 488 | |
| 489 | #define RUST_VEC_OPS(RUST_TYPE, CXX_TYPE) \ |
| 490 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 491 | Vec<CXX_TYPE>::Vec() noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 492 | cxxbridge1$rust_vec$##RUST_TYPE##$new(this); \ |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 493 | } \ |
| 494 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 495 | void Vec<CXX_TYPE>::drop() noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 496 | return cxxbridge1$rust_vec$##RUST_TYPE##$drop(this); \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 497 | } \ |
| 498 | template <> \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 499 | std::size_t Vec<CXX_TYPE>::size() const noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 500 | return cxxbridge1$rust_vec$##RUST_TYPE##$len(this); \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 501 | } \ |
| 502 | template <> \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 503 | std::size_t Vec<CXX_TYPE>::capacity() const noexcept { \ |
| David Tolnay | dc62d71 | 2020-12-11 13:51:53 -0800 | [diff] [blame] | 504 | return cxxbridge1$rust_vec$##RUST_TYPE##$capacity(this); \ |
| 505 | } \ |
| 506 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 507 | const CXX_TYPE *Vec<CXX_TYPE>::data() const noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 508 | return cxxbridge1$rust_vec$##RUST_TYPE##$data(this); \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 509 | } \ |
| 510 | template <> \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 511 | void Vec<CXX_TYPE>::reserve_total(std::size_t cap) noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 512 | cxxbridge1$rust_vec$##RUST_TYPE##$reserve_total(this, cap); \ |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 513 | } \ |
| 514 | template <> \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 515 | void Vec<CXX_TYPE>::set_len(std::size_t len) noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 516 | cxxbridge1$rust_vec$##RUST_TYPE##$set_len(this, len); \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| David Tolnay | 5b16340 | 2020-12-10 19:26:02 -0800 | [diff] [blame] | 519 | #define SHARED_PTR_OPS(RUST_TYPE, CXX_TYPE) \ |
| 520 | static_assert(sizeof(std::shared_ptr<CXX_TYPE>) == 2 * sizeof(void *), ""); \ |
| 521 | static_assert(alignof(std::shared_ptr<CXX_TYPE>) == alignof(void *), ""); \ |
| 522 | void cxxbridge1$std$shared_ptr$##RUST_TYPE##$null( \ |
| 523 | std::shared_ptr<CXX_TYPE> *ptr) noexcept { \ |
| 524 | new (ptr) std::shared_ptr<CXX_TYPE>(); \ |
| 525 | } \ |
| 526 | CXX_TYPE *cxxbridge1$std$shared_ptr$##RUST_TYPE##$uninit( \ |
| 527 | std::shared_ptr<CXX_TYPE> *ptr) noexcept { \ |
| 528 | CXX_TYPE *uninit = \ |
| 529 | reinterpret_cast<CXX_TYPE *>(new rust::MaybeUninit<CXX_TYPE>); \ |
| 530 | new (ptr) std::shared_ptr<CXX_TYPE>(uninit); \ |
| 531 | return uninit; \ |
| 532 | } \ |
| 533 | void cxxbridge1$std$shared_ptr$##RUST_TYPE##$clone( \ |
| 534 | const std::shared_ptr<CXX_TYPE> &self, \ |
| 535 | std::shared_ptr<CXX_TYPE> *ptr) noexcept { \ |
| 536 | new (ptr) std::shared_ptr<CXX_TYPE>(self); \ |
| 537 | } \ |
| 538 | const CXX_TYPE *cxxbridge1$std$shared_ptr$##RUST_TYPE##$get( \ |
| 539 | const std::shared_ptr<CXX_TYPE> &self) noexcept { \ |
| 540 | return self.get(); \ |
| 541 | } \ |
| 542 | void cxxbridge1$std$shared_ptr$##RUST_TYPE##$drop( \ |
| 543 | const std::shared_ptr<CXX_TYPE> *self) noexcept { \ |
| 544 | self->~shared_ptr(); \ |
| David Tolnay | 215e77f | 2020-12-28 17:09:48 -0800 | [diff] [blame] | 545 | } \ |
| 546 | static_assert(sizeof(std::weak_ptr<CXX_TYPE>) == 2 * sizeof(void *), ""); \ |
| 547 | static_assert(alignof(std::weak_ptr<CXX_TYPE>) == alignof(void *), ""); \ |
| 548 | void cxxbridge1$std$weak_ptr$##RUST_TYPE##$null( \ |
| 549 | std::weak_ptr<CXX_TYPE> *ptr) noexcept { \ |
| 550 | new (ptr) std::weak_ptr<CXX_TYPE>(); \ |
| 551 | } \ |
| 552 | void cxxbridge1$std$weak_ptr$##RUST_TYPE##$clone( \ |
| 553 | const std::weak_ptr<CXX_TYPE> &self, \ |
| 554 | std::weak_ptr<CXX_TYPE> *ptr) noexcept { \ |
| 555 | new (ptr) std::weak_ptr<CXX_TYPE>(self); \ |
| 556 | } \ |
| David Tolnay | 85b6bc4 | 2020-12-28 17:47:51 -0800 | [diff] [blame] | 557 | void cxxbridge1$std$weak_ptr$##RUST_TYPE##$downgrade( \ |
| 558 | const std::shared_ptr<CXX_TYPE> &shared, \ |
| 559 | std::weak_ptr<CXX_TYPE> *weak) noexcept { \ |
| 560 | new (weak) std::weak_ptr<CXX_TYPE>(shared); \ |
| 561 | } \ |
| David Tolnay | 7a48785 | 2020-12-28 18:02:25 -0800 | [diff] [blame] | 562 | void cxxbridge1$std$weak_ptr$##RUST_TYPE##$upgrade( \ |
| 563 | const std::weak_ptr<CXX_TYPE> &weak, \ |
| 564 | std::shared_ptr<CXX_TYPE> *shared) noexcept { \ |
| 565 | new (shared) std::shared_ptr<CXX_TYPE>(weak.lock()); \ |
| 566 | } \ |
| David Tolnay | 215e77f | 2020-12-28 17:09:48 -0800 | [diff] [blame] | 567 | void cxxbridge1$std$weak_ptr$##RUST_TYPE##$drop( \ |
| 568 | const std::weak_ptr<CXX_TYPE> *self) noexcept { \ |
| 569 | self->~weak_ptr(); \ |
| David Tolnay | 5b16340 | 2020-12-10 19:26:02 -0800 | [diff] [blame] | 570 | } |
| 571 | |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 572 | // Usize and isize are the same type as one of the below. |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 573 | #define FOR_EACH_NUMERIC(MACRO) \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 574 | MACRO(u8, std::uint8_t) \ |
| 575 | MACRO(u16, std::uint16_t) \ |
| 576 | MACRO(u32, std::uint32_t) \ |
| 577 | MACRO(u64, std::uint64_t) \ |
| 578 | MACRO(i8, std::int8_t) \ |
| 579 | MACRO(i16, std::int16_t) \ |
| 580 | MACRO(i32, std::int32_t) \ |
| 581 | MACRO(i64, std::int64_t) \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 582 | MACRO(f32, float) \ |
| 583 | MACRO(f64, double) |
| 584 | |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 585 | #define FOR_EACH_STD_VECTOR(MACRO) \ |
| 586 | FOR_EACH_NUMERIC(MACRO) \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 587 | MACRO(usize, std::size_t) \ |
| David Tolnay | 47e239d | 2020-08-28 00:32:04 -0700 | [diff] [blame] | 588 | MACRO(isize, rust::isize) \ |
| 589 | MACRO(string, std::string) |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 590 | |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 591 | #define FOR_EACH_RUST_VEC(MACRO) \ |
| 592 | FOR_EACH_NUMERIC(MACRO) \ |
| David Tolnay | 33f56ad | 2020-08-27 17:06:35 -0700 | [diff] [blame] | 593 | MACRO(bool, bool) \ |
| David Tolnay | 93e71d0 | 2020-11-25 20:16:52 -0800 | [diff] [blame] | 594 | MACRO(char, char) \ |
| David Tolnay | 33f56ad | 2020-08-27 17:06:35 -0700 | [diff] [blame] | 595 | MACRO(string, rust::String) |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 596 | |
| David Tolnay | 5b16340 | 2020-12-10 19:26:02 -0800 | [diff] [blame] | 597 | #define FOR_EACH_SHARED_PTR(MACRO) \ |
| 598 | FOR_EACH_NUMERIC(MACRO) \ |
| David Tolnay | cd1430c | 2020-12-28 17:17:14 -0800 | [diff] [blame] | 599 | MACRO(bool, bool) \ |
| David Tolnay | be3cbf7 | 2020-12-12 22:12:07 -0800 | [diff] [blame] | 600 | MACRO(usize, std::size_t) \ |
| David Tolnay | 5b16340 | 2020-12-10 19:26:02 -0800 | [diff] [blame] | 601 | MACRO(isize, rust::isize) \ |
| 602 | MACRO(string, std::string) |
| 603 | |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 604 | extern "C" { |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 605 | FOR_EACH_STD_VECTOR(STD_VECTOR_OPS) |
| 606 | FOR_EACH_RUST_VEC(RUST_VEC_EXTERNS) |
| David Tolnay | 5b16340 | 2020-12-10 19:26:02 -0800 | [diff] [blame] | 607 | FOR_EACH_SHARED_PTR(SHARED_PTR_OPS) |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 608 | } // extern "C" |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 609 | |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 610 | namespace rust { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 611 | inline namespace cxxbridge1 { |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 612 | FOR_EACH_RUST_VEC(RUST_VEC_OPS) |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 613 | } // namespace cxxbridge1 |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 614 | } // namespace rust |