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