| 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 | 4092191 | 2020-11-23 20:44:48 -0800 | [diff] [blame^] | 172 | static_assert(std::is_trivially_copy_constructible<Slice<const uint8_t>>::value, |
| 173 | "trivial Slice(const Slice &)"); |
| 174 | static_assert(std::is_trivially_move_constructible<Slice<const uint8_t>>::value, |
| 175 | "trivial Slice(Slice &&)"); |
| 176 | static_assert(std::is_trivially_copy_assignable<Slice<const uint8_t>>::value, |
| 177 | "trivial Slice::operator=(const Slice &) for const slices"); |
| 178 | static_assert(std::is_trivially_move_assignable<Slice<const uint8_t>>::value, |
| 179 | "trivial Slice::operator=(Slice &&)"); |
| 180 | static_assert(std::is_trivially_destructible<Slice<const uint8_t>>::value, |
| 181 | "trivial ~Slice()"); |
| 182 | |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 183 | extern "C" { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 184 | const char *cxxbridge1$error(const char *ptr, size_t len) { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 185 | char *copy = new char[len]; |
| David Tolnay | 504cf3c | 2020-10-31 16:08:04 -0700 | [diff] [blame] | 186 | std::strncpy(copy, ptr, len); |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 187 | return copy; |
| 188 | } |
| 189 | } // extern "C" |
| 190 | |
| David Tolnay | d5712ee | 2020-10-31 17:10:00 -0700 | [diff] [blame] | 191 | Error::Error(const Error &other) |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 192 | : std::exception(other), msg(cxxbridge1$error(other.msg, other.len)), |
| David Tolnay | 23c2319 | 2020-10-31 17:11:48 -0700 | [diff] [blame] | 193 | len(other.len) {} |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 194 | |
| David Tolnay | 23c2319 | 2020-10-31 17:11:48 -0700 | [diff] [blame] | 195 | Error::Error(Error &&other) noexcept |
| 196 | : std::exception(std::move(other)), msg(other.msg), len(other.len) { |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 197 | other.msg = nullptr; |
| 198 | other.len = 0; |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 201 | Error::~Error() noexcept { delete[] this->msg; } |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 202 | |
| David Tolnay | 7c6ac71 | 2020-10-31 17:22:28 -0700 | [diff] [blame] | 203 | Error &Error::operator=(const Error &other) { |
| 204 | if (this != &other) { |
| 205 | std::exception::operator=(other); |
| 206 | delete[] this->msg; |
| 207 | this->msg = nullptr; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 208 | this->msg = cxxbridge1$error(other.msg, other.len); |
| David Tolnay | 7c6ac71 | 2020-10-31 17:22:28 -0700 | [diff] [blame] | 209 | this->len = other.len; |
| 210 | } |
| 211 | return *this; |
| 212 | } |
| 213 | |
| David Tolnay | 1549106 | 2020-10-31 17:25:13 -0700 | [diff] [blame] | 214 | Error &Error::operator=(Error &&other) noexcept { |
| 215 | if (this != &other) { |
| 216 | std::exception::operator=(std::move(other)); |
| 217 | this->msg = other.msg; |
| 218 | this->len = other.len; |
| 219 | other.msg = nullptr; |
| 220 | other.len = 0; |
| 221 | } |
| 222 | return *this; |
| 223 | } |
| 224 | |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 225 | const char *Error::what() const noexcept { return this->msg; } |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 226 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 227 | } // namespace cxxbridge1 |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 228 | } // namespace rust |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 229 | |
| 230 | extern "C" { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 231 | void cxxbridge1$unique_ptr$std$string$null( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 232 | std::unique_ptr<std::string> *ptr) noexcept { |
| 233 | new (ptr) std::unique_ptr<std::string>(); |
| 234 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 235 | void cxxbridge1$unique_ptr$std$string$raw(std::unique_ptr<std::string> *ptr, |
| 236 | std::string *raw) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 237 | new (ptr) std::unique_ptr<std::string>(raw); |
| 238 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 239 | const std::string *cxxbridge1$unique_ptr$std$string$get( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 240 | const std::unique_ptr<std::string> &ptr) noexcept { |
| 241 | return ptr.get(); |
| 242 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 243 | std::string *cxxbridge1$unique_ptr$std$string$release( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 244 | std::unique_ptr<std::string> &ptr) noexcept { |
| 245 | return ptr.release(); |
| 246 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 247 | void cxxbridge1$unique_ptr$std$string$drop( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 248 | std::unique_ptr<std::string> *ptr) noexcept { |
| 249 | ptr->~unique_ptr(); |
| 250 | } |
| 251 | } // extern "C" |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 252 | |
| David Tolnay | 06677b3 | 2020-11-23 18:05:45 -0800 | [diff] [blame] | 253 | namespace { |
| 254 | const size_t kMaxExpectedWordsInString = 8; |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame] | 255 | static_assert(alignof(std::string) <= alignof(void *), |
| 256 | "unexpectedly large std::string alignment"); |
| David Tolnay | 06677b3 | 2020-11-23 18:05:45 -0800 | [diff] [blame] | 257 | static_assert(sizeof(std::string) <= kMaxExpectedWordsInString * sizeof(void *), |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame] | 258 | "unexpectedly large std::string size"); |
| David Tolnay | c26de54 | 2020-11-23 18:18:19 -0800 | [diff] [blame] | 259 | } // namespace |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame] | 260 | |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 261 | #define STD_VECTOR_OPS(RUST_TYPE, CXX_TYPE) \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 262 | size_t cxxbridge1$std$vector$##RUST_TYPE##$size( \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 263 | const std::vector<CXX_TYPE> &s) noexcept { \ |
| 264 | return s.size(); \ |
| 265 | } \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 266 | const CXX_TYPE *cxxbridge1$std$vector$##RUST_TYPE##$get_unchecked( \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 267 | const std::vector<CXX_TYPE> &s, size_t pos) noexcept { \ |
| David Tolnay | 9626d08 | 2020-04-24 14:52:45 -0700 | [diff] [blame] | 268 | return &s[pos]; \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 269 | } \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 270 | void cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$null( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 271 | std::unique_ptr<std::vector<CXX_TYPE>> *ptr) noexcept { \ |
| 272 | new (ptr) std::unique_ptr<std::vector<CXX_TYPE>>(); \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 273 | } \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 274 | void cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$raw( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 275 | std::unique_ptr<std::vector<CXX_TYPE>> *ptr, \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 276 | std::vector<CXX_TYPE> *raw) noexcept { \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 277 | new (ptr) std::unique_ptr<std::vector<CXX_TYPE>>(raw); \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 278 | } \ |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 279 | const std::vector<CXX_TYPE> \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 280 | *cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$get( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 281 | const std::unique_ptr<std::vector<CXX_TYPE>> &ptr) noexcept { \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 282 | return ptr.get(); \ |
| 283 | } \ |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 284 | std::vector<CXX_TYPE> \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 285 | *cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$release( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 286 | std::unique_ptr<std::vector<CXX_TYPE>> &ptr) noexcept { \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 287 | return ptr.release(); \ |
| 288 | } \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 289 | void cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$drop( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 290 | std::unique_ptr<std::vector<CXX_TYPE>> *ptr) noexcept { \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 291 | ptr->~unique_ptr(); \ |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 292 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 293 | |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 294 | #define RUST_VEC_EXTERNS(RUST_TYPE, CXX_TYPE) \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 295 | void cxxbridge1$rust_vec$##RUST_TYPE##$new( \ |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 296 | rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 297 | void cxxbridge1$rust_vec$##RUST_TYPE##$drop( \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 298 | rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 299 | size_t cxxbridge1$rust_vec$##RUST_TYPE##$len( \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 300 | const rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 301 | const CXX_TYPE *cxxbridge1$rust_vec$##RUST_TYPE##$data( \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 302 | const rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 303 | void cxxbridge1$rust_vec$##RUST_TYPE##$reserve_total( \ |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 304 | rust::Vec<CXX_TYPE> *ptr, size_t cap) noexcept; \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 305 | void cxxbridge1$rust_vec$##RUST_TYPE##$set_len(rust::Vec<CXX_TYPE> *ptr, \ |
| 306 | size_t len) noexcept; \ |
| 307 | size_t cxxbridge1$rust_vec$##RUST_TYPE##$stride() noexcept; |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 308 | |
| 309 | #define RUST_VEC_OPS(RUST_TYPE, CXX_TYPE) \ |
| 310 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 311 | Vec<CXX_TYPE>::Vec() noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 312 | cxxbridge1$rust_vec$##RUST_TYPE##$new(this); \ |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 313 | } \ |
| 314 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 315 | void Vec<CXX_TYPE>::drop() noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 316 | return cxxbridge1$rust_vec$##RUST_TYPE##$drop(this); \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 317 | } \ |
| 318 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 319 | size_t Vec<CXX_TYPE>::size() const noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 320 | return cxxbridge1$rust_vec$##RUST_TYPE##$len(this); \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 321 | } \ |
| 322 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 323 | const CXX_TYPE *Vec<CXX_TYPE>::data() const noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 324 | return cxxbridge1$rust_vec$##RUST_TYPE##$data(this); \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 325 | } \ |
| 326 | template <> \ |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 327 | void Vec<CXX_TYPE>::reserve_total(size_t cap) noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 328 | cxxbridge1$rust_vec$##RUST_TYPE##$reserve_total(this, cap); \ |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 329 | } \ |
| 330 | template <> \ |
| 331 | void Vec<CXX_TYPE>::set_len(size_t len) noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 332 | cxxbridge1$rust_vec$##RUST_TYPE##$set_len(this, len); \ |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 333 | } \ |
| 334 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 335 | size_t Vec<CXX_TYPE>::stride() noexcept { \ |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 336 | return cxxbridge1$rust_vec$##RUST_TYPE##$stride(); \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | // Usize and isize are the same type as one of the below. |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 340 | #define FOR_EACH_NUMERIC(MACRO) \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 341 | MACRO(u8, uint8_t) \ |
| 342 | MACRO(u16, uint16_t) \ |
| 343 | MACRO(u32, uint32_t) \ |
| 344 | MACRO(u64, uint64_t) \ |
| 345 | MACRO(i8, int8_t) \ |
| 346 | MACRO(i16, int16_t) \ |
| 347 | MACRO(i32, int32_t) \ |
| 348 | MACRO(i64, int64_t) \ |
| 349 | MACRO(f32, float) \ |
| 350 | MACRO(f64, double) |
| 351 | |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 352 | #define FOR_EACH_STD_VECTOR(MACRO) \ |
| 353 | FOR_EACH_NUMERIC(MACRO) \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 354 | MACRO(usize, size_t) \ |
| David Tolnay | 47e239d | 2020-08-28 00:32:04 -0700 | [diff] [blame] | 355 | MACRO(isize, rust::isize) \ |
| 356 | MACRO(string, std::string) |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 357 | |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 358 | #define FOR_EACH_RUST_VEC(MACRO) \ |
| 359 | FOR_EACH_NUMERIC(MACRO) \ |
| David Tolnay | 33f56ad | 2020-08-27 17:06:35 -0700 | [diff] [blame] | 360 | MACRO(bool, bool) \ |
| 361 | MACRO(string, rust::String) |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 362 | |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 363 | extern "C" { |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 364 | FOR_EACH_STD_VECTOR(STD_VECTOR_OPS) |
| 365 | FOR_EACH_RUST_VEC(RUST_VEC_EXTERNS) |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 366 | } // extern "C" |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 367 | |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 368 | namespace rust { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 369 | inline namespace cxxbridge1 { |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 370 | FOR_EACH_RUST_VEC(RUST_VEC_OPS) |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 371 | } // namespace cxxbridge1 |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 372 | } // namespace rust |