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