| David Tolnay | 736cbca | 2020-03-11 16:49:18 -0700 | [diff] [blame] | 1 | #include "../include/cxx.h" |
| David Tolnay | 54b1322 | 2020-10-30 20:58:32 -0700 | [diff] [blame] | 2 | #include <cassert> |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 3 | #include <cstring> |
| David Tolnay | 71918ec | 2020-04-11 21:52:09 -0700 | [diff] [blame] | 4 | #include <exception> |
| David Tolnay | 001102a | 2020-03-01 20:05:04 -0800 | [diff] [blame] | 5 | #include <iostream> |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 6 | #include <memory> |
| 7 | #include <stdexcept> |
| David Tolnay | 9ed15c6 | 2020-10-31 18:02:03 -0700 | [diff] [blame] | 8 | #include <type_traits> |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 9 | #include <vector> |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 10 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 11 | extern "C" { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 12 | void cxxbridge1$cxx_string$init(std::string *s, const uint8_t *ptr, |
| 13 | size_t len) noexcept { |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame] | 14 | new (s) std::string(reinterpret_cast<const char *>(ptr), len); |
| 15 | } |
| 16 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 17 | void cxxbridge1$cxx_string$destroy(std::string *s) noexcept { |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame] | 18 | using std::string; |
| 19 | s->~string(); |
| 20 | } |
| 21 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 22 | const char *cxxbridge1$cxx_string$data(const std::string &s) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 23 | return s.data(); |
| 24 | } |
| 25 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 26 | size_t cxxbridge1$cxx_string$length(const std::string &s) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 27 | return s.length(); |
| 28 | } |
| 29 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 30 | void cxxbridge1$cxx_string$push(std::string &s, const uint8_t *ptr, |
| 31 | size_t len) noexcept { |
| David Tolnay | 90691f4 | 2020-11-14 20:01:46 -0800 | [diff] [blame] | 32 | s.append(reinterpret_cast<const char *>(ptr), len); |
| 33 | } |
| 34 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 35 | // rust::String |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 36 | void cxxbridge1$string$new(rust::String *self) noexcept; |
| 37 | void cxxbridge1$string$clone(rust::String *self, |
| 38 | const rust::String &other) noexcept; |
| 39 | bool cxxbridge1$string$from(rust::String *self, const char *ptr, |
| 40 | size_t len) noexcept; |
| 41 | void cxxbridge1$string$drop(rust::String *self) noexcept; |
| 42 | const char *cxxbridge1$string$ptr(const rust::String *self) noexcept; |
| 43 | size_t cxxbridge1$string$len(const rust::String *self) noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 44 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 45 | // rust::Str |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 46 | bool cxxbridge1$str$valid(const char *ptr, size_t len) noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 47 | } // extern "C" |
| 48 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 49 | namespace rust { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 50 | inline namespace cxxbridge1 { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 51 | |
| David Tolnay | 521d99d | 2020-08-26 20:45:40 -0700 | [diff] [blame] | 52 | template <typename Exception> |
| 53 | void panic [[noreturn]] (const char *msg) { |
| 54 | #if defined(RUST_CXX_NO_EXCEPTIONS) |
| 55 | std::cerr << "Error: " << msg << ". Aborting." << std::endl; |
| 56 | std::terminate(); |
| 57 | #else |
| 58 | throw Exception(msg); |
| 59 | #endif |
| 60 | } |
| 61 | |
| David Tolnay | b10c4bc | 2020-08-26 21:55:29 -0700 | [diff] [blame] | 62 | template void panic<std::out_of_range>[[noreturn]] (const char *msg); |
| David Tolnay | 521d99d | 2020-08-26 20:45:40 -0700 | [diff] [blame] | 63 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 64 | String::String() noexcept { cxxbridge1$string$new(this); } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 65 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 66 | String::String(const String &other) noexcept { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 67 | cxxbridge1$string$clone(this, other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 68 | } |
| 69 | |
| David Tolnay | 1567186 | 2020-11-23 18:13:56 -0800 | [diff] [blame] | 70 | String::String(String &&other) noexcept : repr(other.repr) { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 71 | cxxbridge1$string$new(&other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 72 | } |
| 73 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 74 | String::~String() noexcept { cxxbridge1$string$drop(this); } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 75 | |
| David Tolnay | 032d853 | 2020-10-30 20:47:31 -0700 | [diff] [blame] | 76 | static void initString(String *self, const char *s, size_t len) { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 77 | if (!cxxbridge1$string$from(self, s, len)) { |
| David Tolnay | 8d32366 | 2020-10-30 19:32:26 -0700 | [diff] [blame] | 78 | panic<std::invalid_argument>("data for rust::String is not utf-8"); |
| 79 | } |
| 80 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 81 | |
| David Tolnay | 032d853 | 2020-10-30 20:47:31 -0700 | [diff] [blame] | 82 | String::String(const std::string &s) { initString(this, s.data(), s.length()); } |
| 83 | |
| David Tolnay | 54b1322 | 2020-10-30 20:58:32 -0700 | [diff] [blame] | 84 | String::String(const char *s) { |
| 85 | assert(s != nullptr); |
| 86 | initString(this, s, std::strlen(s)); |
| 87 | } |
| David Tolnay | c2bbd95 | 2020-07-29 18:15:26 -0700 | [diff] [blame] | 88 | |
| 89 | String::String(const char *s, size_t len) { |
| David Tolnay | 54b1322 | 2020-10-30 20:58:32 -0700 | [diff] [blame] | 90 | assert(s != nullptr || len == 0); |
| David Tolnay | 032d853 | 2020-10-30 20:47:31 -0700 | [diff] [blame] | 91 | initString(this, |
| 92 | s == nullptr && len == 0 ? reinterpret_cast<const char *>(1) : s, |
| 93 | len); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 96 | String &String::operator=(const String &other) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 97 | if (this != &other) { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 98 | cxxbridge1$string$drop(this); |
| 99 | cxxbridge1$string$clone(this, other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 100 | } |
| 101 | return *this; |
| 102 | } |
| 103 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 104 | String &String::operator=(String &&other) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 105 | if (this != &other) { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 106 | cxxbridge1$string$drop(this); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 107 | this->repr = other.repr; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 108 | cxxbridge1$string$new(&other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 109 | } |
| 110 | return *this; |
| 111 | } |
| 112 | |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 113 | String::operator std::string() const { |
| 114 | return std::string(this->data(), this->size()); |
| 115 | } |
| 116 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 117 | const char *String::data() const noexcept { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 118 | return cxxbridge1$string$ptr(this); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 119 | } |
| 120 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 121 | size_t String::size() const noexcept { return cxxbridge1$string$len(this); } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 122 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 123 | size_t String::length() const noexcept { return cxxbridge1$string$len(this); } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 124 | |
| David Tolnay | d1e2efc | 2020-03-03 22:25:43 -0800 | [diff] [blame] | 125 | String::String(unsafe_bitcopy_t, const String &bits) noexcept |
| 126 | : repr(bits.repr) {} |
| 127 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 128 | std::ostream &operator<<(std::ostream &os, const String &s) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 129 | os.write(s.data(), s.size()); |
| 130 | return os; |
| 131 | } |
| 132 | |
| David Tolnay | 5df1f06 | 2020-10-31 12:31:10 -0700 | [diff] [blame] | 133 | Str::Str() noexcept : ptr(reinterpret_cast<const char *>(1)), len(0) {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 134 | |
| David Tolnay | 5df1f06 | 2020-10-31 12:31:10 -0700 | [diff] [blame] | 135 | static void initStr(const char *ptr, size_t len) { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 136 | if (!cxxbridge1$str$valid(ptr, len)) { |
| David Tolnay | 8d32366 | 2020-10-30 19:32:26 -0700 | [diff] [blame] | 137 | panic<std::invalid_argument>("data for rust::Str is not utf-8"); |
| 138 | } |
| 139 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 140 | |
| David Tolnay | 5df1f06 | 2020-10-31 12:31:10 -0700 | [diff] [blame] | 141 | Str::Str(const std::string &s) : ptr(s.data()), len(s.length()) { |
| 142 | initStr(this->ptr, this->len); |
| David Tolnay | 8d32366 | 2020-10-30 19:32:26 -0700 | [diff] [blame] | 143 | } |
| David Tolnay | 894c5e4 | 2020-07-29 18:20:00 -0700 | [diff] [blame] | 144 | |
| David Tolnay | 5df1f06 | 2020-10-31 12:31:10 -0700 | [diff] [blame] | 145 | Str::Str(const char *s) : ptr(s), len(std::strlen(s)) { |
| David Tolnay | 54b1322 | 2020-10-30 20:58:32 -0700 | [diff] [blame] | 146 | assert(s != nullptr); |
| David Tolnay | 5df1f06 | 2020-10-31 12:31:10 -0700 | [diff] [blame] | 147 | initStr(this->ptr, this->len); |
| David Tolnay | 54b1322 | 2020-10-30 20:58:32 -0700 | [diff] [blame] | 148 | } |
| David Tolnay | 032d853 | 2020-10-30 20:47:31 -0700 | [diff] [blame] | 149 | |
| David Tolnay | 8d32366 | 2020-10-30 19:32:26 -0700 | [diff] [blame] | 150 | Str::Str(const char *s, size_t len) |
| David Tolnay | 5df1f06 | 2020-10-31 12:31:10 -0700 | [diff] [blame] | 151 | : ptr(s == nullptr && len == 0 ? reinterpret_cast<const char *>(1) : s), |
| 152 | len(len) { |
| David Tolnay | 54b1322 | 2020-10-30 20:58:32 -0700 | [diff] [blame] | 153 | assert(s != nullptr || len == 0); |
| David Tolnay | 5df1f06 | 2020-10-31 12:31:10 -0700 | [diff] [blame] | 154 | initStr(this->ptr, this->len); |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 155 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 156 | |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 157 | Str::operator std::string() const { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 158 | return std::string(this->data(), this->size()); |
| 159 | } |
| 160 | |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 161 | std::ostream &operator<<(std::ostream &os, const Str &s) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 162 | os.write(s.data(), s.size()); |
| 163 | return os; |
| 164 | } |
| 165 | |
| David Tolnay | 9ed15c6 | 2020-10-31 18:02:03 -0700 | [diff] [blame] | 166 | static_assert(std::is_trivially_copy_constructible<Str>::value, |
| 167 | "trivial Str(const Str &)"); |
| 168 | static_assert(std::is_trivially_copy_assignable<Str>::value, |
| 169 | "trivial operator=(const Str &)"); |
| 170 | static_assert(std::is_trivially_destructible<Str>::value, "trivial ~Str()"); |
| 171 | |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 172 | extern "C" { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 173 | const char *cxxbridge1$error(const char *ptr, size_t len) { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 174 | char *copy = new char[len]; |
| David Tolnay | 504cf3c | 2020-10-31 16:08:04 -0700 | [diff] [blame] | 175 | std::strncpy(copy, ptr, len); |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 176 | return copy; |
| 177 | } |
| 178 | } // extern "C" |
| 179 | |
| David Tolnay | d5712ee | 2020-10-31 17:10:00 -0700 | [diff] [blame] | 180 | Error::Error(const Error &other) |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 181 | : std::exception(other), msg(cxxbridge1$error(other.msg, other.len)), |
| David Tolnay | 23c2319 | 2020-10-31 17:11:48 -0700 | [diff] [blame] | 182 | len(other.len) {} |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 183 | |
| David Tolnay | 23c2319 | 2020-10-31 17:11:48 -0700 | [diff] [blame] | 184 | Error::Error(Error &&other) noexcept |
| 185 | : std::exception(std::move(other)), msg(other.msg), len(other.len) { |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 186 | other.msg = nullptr; |
| 187 | other.len = 0; |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 190 | Error::~Error() noexcept { delete[] this->msg; } |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 191 | |
| David Tolnay | 7c6ac71 | 2020-10-31 17:22:28 -0700 | [diff] [blame] | 192 | Error &Error::operator=(const Error &other) { |
| 193 | if (this != &other) { |
| 194 | std::exception::operator=(other); |
| 195 | delete[] this->msg; |
| 196 | this->msg = nullptr; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 197 | this->msg = cxxbridge1$error(other.msg, other.len); |
| David Tolnay | 7c6ac71 | 2020-10-31 17:22:28 -0700 | [diff] [blame] | 198 | this->len = other.len; |
| 199 | } |
| 200 | return *this; |
| 201 | } |
| 202 | |
| David Tolnay | 1549106 | 2020-10-31 17:25:13 -0700 | [diff] [blame] | 203 | Error &Error::operator=(Error &&other) noexcept { |
| 204 | if (this != &other) { |
| 205 | std::exception::operator=(std::move(other)); |
| 206 | this->msg = other.msg; |
| 207 | this->len = other.len; |
| 208 | other.msg = nullptr; |
| 209 | other.len = 0; |
| 210 | } |
| 211 | return *this; |
| 212 | } |
| 213 | |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 214 | const char *Error::what() const noexcept { return this->msg; } |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 215 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 216 | } // namespace cxxbridge1 |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 217 | } // namespace rust |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 218 | |
| 219 | extern "C" { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 220 | void cxxbridge1$unique_ptr$std$string$null( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 221 | std::unique_ptr<std::string> *ptr) noexcept { |
| 222 | new (ptr) std::unique_ptr<std::string>(); |
| 223 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 224 | void cxxbridge1$unique_ptr$std$string$raw(std::unique_ptr<std::string> *ptr, |
| 225 | std::string *raw) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 226 | new (ptr) std::unique_ptr<std::string>(raw); |
| 227 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 228 | const std::string *cxxbridge1$unique_ptr$std$string$get( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 229 | const std::unique_ptr<std::string> &ptr) noexcept { |
| 230 | return ptr.get(); |
| 231 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 232 | std::string *cxxbridge1$unique_ptr$std$string$release( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 233 | std::unique_ptr<std::string> &ptr) noexcept { |
| 234 | return ptr.release(); |
| 235 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 236 | void cxxbridge1$unique_ptr$std$string$drop( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 237 | std::unique_ptr<std::string> *ptr) noexcept { |
| 238 | ptr->~unique_ptr(); |
| 239 | } |
| 240 | } // extern "C" |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 241 | |
| David Tolnay | 06677b3 | 2020-11-23 18:05:45 -0800 | [diff] [blame] | 242 | namespace { |
| 243 | const size_t kMaxExpectedWordsInString = 8; |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame] | 244 | static_assert(alignof(std::string) <= alignof(void *), |
| 245 | "unexpectedly large std::string alignment"); |
| David Tolnay | 06677b3 | 2020-11-23 18:05:45 -0800 | [diff] [blame] | 246 | static_assert(sizeof(std::string) <= kMaxExpectedWordsInString * sizeof(void *), |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame] | 247 | "unexpectedly large std::string size"); |
| David Tolnay | 06677b3 | 2020-11-23 18:05:45 -0800 | [diff] [blame] | 248 | } |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame] | 249 | |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 250 | #define STD_VECTOR_OPS(RUST_TYPE, CXX_TYPE) \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 251 | size_t cxxbridge1$std$vector$##RUST_TYPE##$size( \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 252 | const std::vector<CXX_TYPE> &s) noexcept { \ |
| 253 | return s.size(); \ |
| 254 | } \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 255 | const CXX_TYPE *cxxbridge1$std$vector$##RUST_TYPE##$get_unchecked( \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 256 | const std::vector<CXX_TYPE> &s, size_t pos) noexcept { \ |
| David Tolnay | 9626d08 | 2020-04-24 14:52:45 -0700 | [diff] [blame] | 257 | return &s[pos]; \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 258 | } \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 259 | void cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$null( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 260 | std::unique_ptr<std::vector<CXX_TYPE>> *ptr) noexcept { \ |
| 261 | new (ptr) std::unique_ptr<std::vector<CXX_TYPE>>(); \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 262 | } \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 263 | void cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$raw( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 264 | std::unique_ptr<std::vector<CXX_TYPE>> *ptr, \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 265 | std::vector<CXX_TYPE> *raw) noexcept { \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 266 | new (ptr) std::unique_ptr<std::vector<CXX_TYPE>>(raw); \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 267 | } \ |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 268 | const std::vector<CXX_TYPE> \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 269 | *cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$get( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 270 | const std::unique_ptr<std::vector<CXX_TYPE>> &ptr) noexcept { \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 271 | return ptr.get(); \ |
| 272 | } \ |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 273 | std::vector<CXX_TYPE> \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 274 | *cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$release( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 275 | std::unique_ptr<std::vector<CXX_TYPE>> &ptr) noexcept { \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 276 | return ptr.release(); \ |
| 277 | } \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 278 | void cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$drop( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 279 | std::unique_ptr<std::vector<CXX_TYPE>> *ptr) noexcept { \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 280 | ptr->~unique_ptr(); \ |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 281 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 282 | |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 283 | #define RUST_VEC_EXTERNS(RUST_TYPE, CXX_TYPE) \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 284 | void cxxbridge1$rust_vec$##RUST_TYPE##$new( \ |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 285 | rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 286 | void cxxbridge1$rust_vec$##RUST_TYPE##$drop( \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 287 | rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 288 | size_t cxxbridge1$rust_vec$##RUST_TYPE##$len( \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 289 | const rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 290 | const CXX_TYPE *cxxbridge1$rust_vec$##RUST_TYPE##$data( \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 291 | const rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 292 | void cxxbridge1$rust_vec$##RUST_TYPE##$reserve_total( \ |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 293 | rust::Vec<CXX_TYPE> *ptr, size_t cap) noexcept; \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 294 | void cxxbridge1$rust_vec$##RUST_TYPE##$set_len(rust::Vec<CXX_TYPE> *ptr, \ |
| 295 | size_t len) noexcept; \ |
| 296 | size_t cxxbridge1$rust_vec$##RUST_TYPE##$stride() noexcept; |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 297 | |
| 298 | #define RUST_VEC_OPS(RUST_TYPE, CXX_TYPE) \ |
| 299 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 300 | Vec<CXX_TYPE>::Vec() noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 301 | cxxbridge1$rust_vec$##RUST_TYPE##$new(this); \ |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 302 | } \ |
| 303 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 304 | void Vec<CXX_TYPE>::drop() noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 305 | return cxxbridge1$rust_vec$##RUST_TYPE##$drop(this); \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 306 | } \ |
| 307 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 308 | size_t Vec<CXX_TYPE>::size() const noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 309 | return cxxbridge1$rust_vec$##RUST_TYPE##$len(this); \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 310 | } \ |
| 311 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 312 | const CXX_TYPE *Vec<CXX_TYPE>::data() const noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 313 | return cxxbridge1$rust_vec$##RUST_TYPE##$data(this); \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 314 | } \ |
| 315 | template <> \ |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 316 | void Vec<CXX_TYPE>::reserve_total(size_t cap) noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 317 | cxxbridge1$rust_vec$##RUST_TYPE##$reserve_total(this, cap); \ |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 318 | } \ |
| 319 | template <> \ |
| 320 | void Vec<CXX_TYPE>::set_len(size_t len) noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 321 | cxxbridge1$rust_vec$##RUST_TYPE##$set_len(this, len); \ |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 322 | } \ |
| 323 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 324 | size_t Vec<CXX_TYPE>::stride() noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 325 | return cxxbridge1$rust_vec$##RUST_TYPE##$stride(); \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | // Usize and isize are the same type as one of the below. |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 329 | #define FOR_EACH_NUMERIC(MACRO) \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 330 | MACRO(u8, uint8_t) \ |
| 331 | MACRO(u16, uint16_t) \ |
| 332 | MACRO(u32, uint32_t) \ |
| 333 | MACRO(u64, uint64_t) \ |
| 334 | MACRO(i8, int8_t) \ |
| 335 | MACRO(i16, int16_t) \ |
| 336 | MACRO(i32, int32_t) \ |
| 337 | MACRO(i64, int64_t) \ |
| 338 | MACRO(f32, float) \ |
| 339 | MACRO(f64, double) |
| 340 | |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 341 | #define FOR_EACH_STD_VECTOR(MACRO) \ |
| 342 | FOR_EACH_NUMERIC(MACRO) \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 343 | MACRO(usize, size_t) \ |
| David Tolnay | 47e239d | 2020-08-28 00:32:04 -0700 | [diff] [blame] | 344 | MACRO(isize, rust::isize) \ |
| 345 | MACRO(string, std::string) |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 346 | |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 347 | #define FOR_EACH_RUST_VEC(MACRO) \ |
| 348 | FOR_EACH_NUMERIC(MACRO) \ |
| David Tolnay | 33f56ad | 2020-08-27 17:06:35 -0700 | [diff] [blame] | 349 | MACRO(bool, bool) \ |
| 350 | MACRO(string, rust::String) |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 351 | |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 352 | extern "C" { |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 353 | FOR_EACH_STD_VECTOR(STD_VECTOR_OPS) |
| 354 | FOR_EACH_RUST_VEC(RUST_VEC_EXTERNS) |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 355 | } // extern "C" |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 356 | |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 357 | namespace rust { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 358 | inline namespace cxxbridge1 { |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 359 | FOR_EACH_RUST_VEC(RUST_VEC_OPS) |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 360 | } // namespace cxxbridge1 |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 361 | } // namespace rust |