blob: b15dcaa37ec74a8b9c720b6a65ffa93130288af8 [file] [log] [blame]
David Tolnay736cbca2020-03-11 16:49:18 -07001#include "../include/cxx.h"
David Tolnay7db73692019-10-20 14:51:12 -04002#include <cstring>
David Tolnay001102a2020-03-01 20:05:04 -08003#include <iostream>
David Tolnay7db73692019-10-20 14:51:12 -04004#include <memory>
David Tolnay7db73692019-10-20 14:51:12 -04005
David Tolnay7db73692019-10-20 14:51:12 -04006extern "C" {
David Tolnaybe3cbf72020-12-12 22:12:07 -08007void cxxbridge1$cxx_string$init(std::string *s, const std::uint8_t *ptr,
8 std::size_t len) noexcept {
David Tolnaybb3ff5d2020-11-15 19:45:11 -08009 new (s) std::string(reinterpret_cast<const char *>(ptr), len);
10}
11
David Tolnay0f0162f2020-11-16 23:43:37 -080012void cxxbridge1$cxx_string$destroy(std::string *s) noexcept {
David Tolnaybb3ff5d2020-11-15 19:45:11 -080013 using std::string;
14 s->~string();
15}
16
David Tolnay0f0162f2020-11-16 23:43:37 -080017const char *cxxbridge1$cxx_string$data(const std::string &s) noexcept {
David Tolnay7db73692019-10-20 14:51:12 -040018 return s.data();
19}
20
David Tolnaybe3cbf72020-12-12 22:12:07 -080021std::size_t cxxbridge1$cxx_string$length(const std::string &s) noexcept {
David Tolnay7db73692019-10-20 14:51:12 -040022 return s.length();
23}
24
David Tolnaybe3cbf72020-12-12 22:12:07 -080025void cxxbridge1$cxx_string$push(std::string &s, const std::uint8_t *ptr,
26 std::size_t len) noexcept {
David Tolnay90691f42020-11-14 20:01:46 -080027 s.append(reinterpret_cast<const char *>(ptr), len);
28}
29
David Tolnay750755e2020-03-01 13:04:08 -080030// rust::String
David Tolnay0f0162f2020-11-16 23:43:37 -080031void cxxbridge1$string$new(rust::String *self) noexcept;
32void cxxbridge1$string$clone(rust::String *self,
33 const rust::String &other) noexcept;
34bool cxxbridge1$string$from(rust::String *self, const char *ptr,
David Tolnaybe3cbf72020-12-12 22:12:07 -080035 std::size_t len) noexcept;
David Tolnay0f0162f2020-11-16 23:43:37 -080036void cxxbridge1$string$drop(rust::String *self) noexcept;
37const char *cxxbridge1$string$ptr(const rust::String *self) noexcept;
David Tolnaybe3cbf72020-12-12 22:12:07 -080038std::size_t cxxbridge1$string$len(const rust::String *self) noexcept;
David Tolnaycca2e612020-12-18 12:48:22 -080039void cxxbridge1$string$reserve_total(rust::String *self, size_t cap) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -040040
David Tolnay750755e2020-03-01 13:04:08 -080041// rust::Str
David Tolnaybe3cbf72020-12-12 22:12:07 -080042bool cxxbridge1$str$valid(const char *ptr, std::size_t len) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -040043} // extern "C"
44
David Tolnay750755e2020-03-01 13:04:08 -080045namespace rust {
David Tolnay0f0162f2020-11-16 23:43:37 -080046inline namespace cxxbridge1 {
David Tolnay7db73692019-10-20 14:51:12 -040047
David Tolnay521d99d2020-08-26 20:45:40 -070048template <typename Exception>
49void panic [[noreturn]] (const char *msg) {
50#if defined(RUST_CXX_NO_EXCEPTIONS)
51 std::cerr << "Error: " << msg << ". Aborting." << std::endl;
52 std::terminate();
53#else
54 throw Exception(msg);
55#endif
56}
57
David Tolnayb10c4bc2020-08-26 21:55:29 -070058template void panic<std::out_of_range>[[noreturn]] (const char *msg);
David Tolnay521d99d2020-08-26 20:45:40 -070059
David Tolnay0f0162f2020-11-16 23:43:37 -080060String::String() noexcept { cxxbridge1$string$new(this); }
David Tolnay7db73692019-10-20 14:51:12 -040061
David Tolnay56082162020-03-01 12:57:33 -080062String::String(const String &other) noexcept {
David Tolnay0f0162f2020-11-16 23:43:37 -080063 cxxbridge1$string$clone(this, other);
David Tolnay7db73692019-10-20 14:51:12 -040064}
65
David Tolnay15671862020-11-23 18:13:56 -080066String::String(String &&other) noexcept : repr(other.repr) {
David Tolnay0f0162f2020-11-16 23:43:37 -080067 cxxbridge1$string$new(&other);
David Tolnay7db73692019-10-20 14:51:12 -040068}
69
David Tolnay0f0162f2020-11-16 23:43:37 -080070String::~String() noexcept { cxxbridge1$string$drop(this); }
David Tolnay7db73692019-10-20 14:51:12 -040071
David Tolnaybe3cbf72020-12-12 22:12:07 -080072static void initString(String *self, const char *s, std::size_t len) {
David Tolnay0f0162f2020-11-16 23:43:37 -080073 if (!cxxbridge1$string$from(self, s, len)) {
David Tolnay8d323662020-10-30 19:32:26 -070074 panic<std::invalid_argument>("data for rust::String is not utf-8");
75 }
76}
David Tolnay7db73692019-10-20 14:51:12 -040077
David Tolnay032d8532020-10-30 20:47:31 -070078String::String(const std::string &s) { initString(this, s.data(), s.length()); }
79
David Tolnay54b13222020-10-30 20:58:32 -070080String::String(const char *s) {
81 assert(s != nullptr);
82 initString(this, s, std::strlen(s));
83}
David Tolnayc2bbd952020-07-29 18:15:26 -070084
David Tolnaybe3cbf72020-12-12 22:12:07 -080085String::String(const char *s, std::size_t len) {
David Tolnay54b13222020-10-30 20:58:32 -070086 assert(s != nullptr || len == 0);
David Tolnay032d8532020-10-30 20:47:31 -070087 initString(this,
88 s == nullptr && len == 0 ? reinterpret_cast<const char *>(1) : s,
89 len);
David Tolnay7db73692019-10-20 14:51:12 -040090}
91
David Tolnay56082162020-03-01 12:57:33 -080092String &String::operator=(const String &other) noexcept {
David Tolnay7db73692019-10-20 14:51:12 -040093 if (this != &other) {
David Tolnay0f0162f2020-11-16 23:43:37 -080094 cxxbridge1$string$drop(this);
95 cxxbridge1$string$clone(this, other);
David Tolnay7db73692019-10-20 14:51:12 -040096 }
97 return *this;
98}
99
David Tolnay56082162020-03-01 12:57:33 -0800100String &String::operator=(String &&other) noexcept {
David Tolnay7db73692019-10-20 14:51:12 -0400101 if (this != &other) {
David Tolnay0f0162f2020-11-16 23:43:37 -0800102 cxxbridge1$string$drop(this);
David Tolnay7db73692019-10-20 14:51:12 -0400103 this->repr = other.repr;
David Tolnay0f0162f2020-11-16 23:43:37 -0800104 cxxbridge1$string$new(&other);
David Tolnay7db73692019-10-20 14:51:12 -0400105 }
106 return *this;
107}
108
David Tolnayd9c4ac92020-03-01 20:33:58 -0800109String::operator std::string() const {
110 return std::string(this->data(), this->size());
111}
112
David Tolnay56082162020-03-01 12:57:33 -0800113const char *String::data() const noexcept {
David Tolnay0f0162f2020-11-16 23:43:37 -0800114 return cxxbridge1$string$ptr(this);
David Tolnay7db73692019-10-20 14:51:12 -0400115}
116
David Tolnaybe3cbf72020-12-12 22:12:07 -0800117std::size_t String::size() const noexcept {
118 return cxxbridge1$string$len(this);
119}
David Tolnay7db73692019-10-20 14:51:12 -0400120
David Tolnaybe3cbf72020-12-12 22:12:07 -0800121std::size_t String::length() const noexcept {
122 return cxxbridge1$string$len(this);
123}
David Tolnay7db73692019-10-20 14:51:12 -0400124
David Tolnaycca2e612020-12-18 12:48:22 -0800125const char *String::c_str() noexcept {
126 auto len = this->length();
127 cxxbridge1$string$reserve_total(this, len + 1);
128 auto ptr = this->data();
129 const_cast<char *>(ptr)[len] = '\0';
130 return ptr;
131}
132
David Tolnayff7f5fb2020-11-25 20:50:32 -0800133String::iterator String::begin() noexcept {
134 return const_cast<char *>(this->data());
135}
136
137String::iterator String::end() noexcept {
138 return const_cast<char *>(this->data()) + this->size();
139}
140
141String::const_iterator String::begin() const noexcept { return this->cbegin(); }
142
143String::const_iterator String::end() const noexcept { return this->cend(); }
144
145String::const_iterator String::cbegin() const noexcept { return this->data(); }
146
147String::const_iterator String::cend() const noexcept {
148 return this->data() + this->size();
149}
150
David Tolnayff86dce2020-11-29 19:45:13 -0800151bool String::operator==(const String &rhs) const noexcept {
152 return rust::Str(*this) == rust::Str(rhs);
153}
154
155bool String::operator!=(const String &rhs) const noexcept {
156 return rust::Str(*this) != rust::Str(rhs);
157}
158
159bool String::operator<(const String &rhs) const noexcept {
160 return rust::Str(*this) < rust::Str(rhs);
161}
162
163bool String::operator<=(const String &rhs) const noexcept {
164 return rust::Str(*this) <= rust::Str(rhs);
165}
166
167bool String::operator>(const String &rhs) const noexcept {
168 return rust::Str(*this) > rust::Str(rhs);
169}
170
171bool String::operator>=(const String &rhs) const noexcept {
172 return rust::Str(*this) >= rust::Str(rhs);
173}
174
David Tolnayd1e2efc2020-03-03 22:25:43 -0800175String::String(unsafe_bitcopy_t, const String &bits) noexcept
176 : repr(bits.repr) {}
177
David Tolnay56082162020-03-01 12:57:33 -0800178std::ostream &operator<<(std::ostream &os, const String &s) {
David Tolnay7db73692019-10-20 14:51:12 -0400179 os.write(s.data(), s.size());
180 return os;
181}
182
David Tolnay5df1f062020-10-31 12:31:10 -0700183Str::Str() noexcept : ptr(reinterpret_cast<const char *>(1)), len(0) {}
David Tolnay7db73692019-10-20 14:51:12 -0400184
David Tolnay828e5132020-11-29 20:40:40 -0800185Str::Str(const String &s) noexcept : ptr(s.data()), len(s.length()) {}
186
David Tolnaybe3cbf72020-12-12 22:12:07 -0800187static void initStr(const char *ptr, std::size_t len) {
David Tolnay0f0162f2020-11-16 23:43:37 -0800188 if (!cxxbridge1$str$valid(ptr, len)) {
David Tolnay8d323662020-10-30 19:32:26 -0700189 panic<std::invalid_argument>("data for rust::Str is not utf-8");
190 }
191}
David Tolnay7db73692019-10-20 14:51:12 -0400192
David Tolnay5df1f062020-10-31 12:31:10 -0700193Str::Str(const std::string &s) : ptr(s.data()), len(s.length()) {
194 initStr(this->ptr, this->len);
David Tolnay8d323662020-10-30 19:32:26 -0700195}
David Tolnay894c5e42020-07-29 18:20:00 -0700196
David Tolnay5df1f062020-10-31 12:31:10 -0700197Str::Str(const char *s) : ptr(s), len(std::strlen(s)) {
David Tolnay54b13222020-10-30 20:58:32 -0700198 assert(s != nullptr);
David Tolnay5df1f062020-10-31 12:31:10 -0700199 initStr(this->ptr, this->len);
David Tolnay54b13222020-10-30 20:58:32 -0700200}
David Tolnay032d8532020-10-30 20:47:31 -0700201
David Tolnaybe3cbf72020-12-12 22:12:07 -0800202Str::Str(const char *s, std::size_t len)
David Tolnay5df1f062020-10-31 12:31:10 -0700203 : ptr(s == nullptr && len == 0 ? reinterpret_cast<const char *>(1) : s),
204 len(len) {
David Tolnay54b13222020-10-30 20:58:32 -0700205 assert(s != nullptr || len == 0);
David Tolnay5df1f062020-10-31 12:31:10 -0700206 initStr(this->ptr, this->len);
David Tolnayd9c4ac92020-03-01 20:33:58 -0800207}
David Tolnay7db73692019-10-20 14:51:12 -0400208
David Tolnay09dbe752020-03-01 13:00:40 -0800209Str::operator std::string() const {
David Tolnay7db73692019-10-20 14:51:12 -0400210 return std::string(this->data(), this->size());
211}
212
David Tolnayff7f5fb2020-11-25 20:50:32 -0800213Str::const_iterator Str::begin() const noexcept { return this->cbegin(); }
214
215Str::const_iterator Str::end() const noexcept { return this->cend(); }
216
217Str::const_iterator Str::cbegin() const noexcept { return this->ptr; }
218
219Str::const_iterator Str::cend() const noexcept { return this->ptr + this->len; }
220
David Tolnayff86dce2020-11-29 19:45:13 -0800221bool Str::operator==(const Str &rhs) const noexcept {
222 return this->len == rhs.len &&
223 std::equal(this->begin(), this->end(), rhs.begin());
224}
225
226bool Str::operator!=(const Str &rhs) const noexcept { return !(*this == rhs); }
227
228bool Str::operator<(const Str &rhs) const noexcept {
229 return std::lexicographical_compare(this->begin(), this->end(), rhs.begin(),
230 rhs.end());
231}
232
233bool Str::operator<=(const Str &rhs) const noexcept {
234 // std::mismatch(this->begin(), this->end(), rhs.begin(), rhs.end()), except
235 // without Undefined Behavior on C++11 if rhs is shorter than *this.
236 const_iterator liter = this->begin(), lend = this->end(), riter = rhs.begin(),
237 rend = rhs.end();
238 while (liter != lend && riter != rend && *liter == *riter) {
239 ++liter, ++riter;
240 }
241 if (liter == lend) {
242 return true; // equal or *this is a prefix of rhs
243 } else if (riter == rend) {
244 return false; // rhs is a prefix of *this
245 } else {
246 return *liter <= *riter;
247 }
248}
249
250bool Str::operator>(const Str &rhs) const noexcept { return rhs < *this; }
251
252bool Str::operator>=(const Str &rhs) const noexcept { return rhs <= *this; }
253
David Tolnay09dbe752020-03-01 13:00:40 -0800254std::ostream &operator<<(std::ostream &os, const Str &s) {
David Tolnay7db73692019-10-20 14:51:12 -0400255 os.write(s.data(), s.size());
256 return os;
257}
258
David Tolnay03c43f52020-12-12 21:07:17 -0800259// Rust specifies that usize is ABI compatible with C's uintptr_t.
260// https://rust-lang.github.io/unsafe-code-guidelines/layout/scalars.html#isize-and-usize
261// However there is no direct Rust equivalent for size_t. C does not guarantee
262// that size_t and uintptr_t are compatible. In practice though, on all
263// platforms supported by Rust, they are identical for ABI purposes. See the
264// libc crate which unconditionally defines libc::size_t = usize. We expect the
265// same here and these assertions are just here to explicitly document that.
266// *Note that no assumption is made about C++ name mangling of signatures
267// containing these types, not here nor anywhere in CXX.*
David Tolnaybe3cbf72020-12-12 22:12:07 -0800268static_assert(sizeof(std::size_t) == sizeof(std::uintptr_t),
269 "unsupported size_t size");
270static_assert(alignof(std::size_t) == alignof(std::uintptr_t),
David Tolnay03c43f52020-12-12 21:07:17 -0800271 "unsupported size_t alignment");
David Tolnaybe3cbf72020-12-12 22:12:07 -0800272static_assert(sizeof(rust::isize) == sizeof(std::intptr_t),
David Tolnay03c43f52020-12-12 21:07:17 -0800273 "unsupported ssize_t size");
David Tolnaybe3cbf72020-12-12 22:12:07 -0800274static_assert(alignof(rust::isize) == alignof(std::intptr_t),
David Tolnay03c43f52020-12-12 21:07:17 -0800275 "unsupported ssize_t alignment");
276
David Tolnay9ed15c62020-10-31 18:02:03 -0700277static_assert(std::is_trivially_copy_constructible<Str>::value,
278 "trivial Str(const Str &)");
279static_assert(std::is_trivially_copy_assignable<Str>::value,
280 "trivial operator=(const Str &)");
281static_assert(std::is_trivially_destructible<Str>::value, "trivial ~Str()");
282
David Tolnay9578cf12020-11-25 14:36:46 -0800283static_assert(
David Tolnaybe3cbf72020-12-12 22:12:07 -0800284 std::is_trivially_copy_constructible<Slice<const std::uint8_t>>::value,
285 "trivial Slice(const Slice &)");
286static_assert(
287 std::is_trivially_move_constructible<Slice<const std::uint8_t>>::value,
288 "trivial Slice(Slice &&)");
289static_assert(
290 std::is_trivially_copy_assignable<Slice<const std::uint8_t>>::value,
291 "trivial Slice::operator=(const Slice &) for const slices");
292static_assert(
293 std::is_trivially_move_assignable<Slice<const std::uint8_t>>::value,
294 "trivial Slice::operator=(Slice &&)");
295static_assert(std::is_trivially_destructible<Slice<const std::uint8_t>>::value,
296 "trivial ~Slice()");
297
298static_assert(std::is_trivially_copy_constructible<Slice<std::uint8_t>>::value,
299 "trivial Slice(const Slice &)");
300static_assert(std::is_trivially_move_constructible<Slice<std::uint8_t>>::value,
301 "trivial Slice(Slice &&)");
302static_assert(!std::is_copy_assignable<Slice<std::uint8_t>>::value,
303 "delete Slice::operator=(const Slice &) for mut slices");
304static_assert(std::is_trivially_move_assignable<Slice<std::uint8_t>>::value,
305 "trivial Slice::operator=(Slice &&)");
306static_assert(std::is_trivially_destructible<Slice<std::uint8_t>>::value,
307 "trivial ~Slice()");
308
309static_assert(std::is_same<Vec<std::uint8_t>::const_iterator,
310 Vec<const std::uint8_t>::iterator>::value,
311 "Vec<T>::const_iterator == Vec<const T>::iterator");
312static_assert(std::is_same<Vec<const std::uint8_t>::const_iterator,
313 Vec<const std::uint8_t>::iterator>::value,
314 "Vec<const T>::const_iterator == Vec<const T>::iterator");
315static_assert(!std::is_same<Vec<std::uint8_t>::const_iterator,
316 Vec<std::uint8_t>::iterator>::value,
317 "Vec<T>::const_iterator != Vec<T>::iterator");
David Tolnay9578cf12020-11-25 14:36:46 -0800318
David Tolnay1e548172020-03-16 13:37:09 -0700319extern "C" {
David Tolnaybe3cbf72020-12-12 22:12:07 -0800320const char *cxxbridge1$error(const char *ptr, std::size_t len) {
David Tolnay1e548172020-03-16 13:37:09 -0700321 char *copy = new char[len];
David Tolnay504cf3c2020-10-31 16:08:04 -0700322 std::strncpy(copy, ptr, len);
David Tolnay1e548172020-03-16 13:37:09 -0700323 return copy;
324}
325} // extern "C"
326
David Tolnayd5712ee2020-10-31 17:10:00 -0700327Error::Error(const Error &other)
David Tolnay0f0162f2020-11-16 23:43:37 -0800328 : std::exception(other), msg(cxxbridge1$error(other.msg, other.len)),
David Tolnay23c23192020-10-31 17:11:48 -0700329 len(other.len) {}
David Tolnay1e548172020-03-16 13:37:09 -0700330
David Tolnay23c23192020-10-31 17:11:48 -0700331Error::Error(Error &&other) noexcept
332 : std::exception(std::move(other)), msg(other.msg), len(other.len) {
David Tolnaya0c9bc72020-10-31 14:37:14 -0700333 other.msg = nullptr;
334 other.len = 0;
David Tolnay1e548172020-03-16 13:37:09 -0700335}
336
David Tolnaya0c9bc72020-10-31 14:37:14 -0700337Error::~Error() noexcept { delete[] this->msg; }
David Tolnay1e548172020-03-16 13:37:09 -0700338
David Tolnay7c6ac712020-10-31 17:22:28 -0700339Error &Error::operator=(const Error &other) {
340 if (this != &other) {
341 std::exception::operator=(other);
342 delete[] this->msg;
343 this->msg = nullptr;
David Tolnay0f0162f2020-11-16 23:43:37 -0800344 this->msg = cxxbridge1$error(other.msg, other.len);
David Tolnay7c6ac712020-10-31 17:22:28 -0700345 this->len = other.len;
346 }
347 return *this;
348}
349
David Tolnay15491062020-10-31 17:25:13 -0700350Error &Error::operator=(Error &&other) noexcept {
351 if (this != &other) {
352 std::exception::operator=(std::move(other));
353 this->msg = other.msg;
354 this->len = other.len;
355 other.msg = nullptr;
356 other.len = 0;
357 }
358 return *this;
359}
360
David Tolnaya0c9bc72020-10-31 14:37:14 -0700361const char *Error::what() const noexcept { return this->msg; }
David Tolnay1e548172020-03-16 13:37:09 -0700362
David Tolnay5b163402020-12-10 19:26:02 -0800363namespace {
364template <typename T>
365union MaybeUninit {
366 T value;
367 MaybeUninit() {}
368 ~MaybeUninit() {}
369};
370} // namespace
371
David Tolnay0f0162f2020-11-16 23:43:37 -0800372} // namespace cxxbridge1
David Tolnay750755e2020-03-01 13:04:08 -0800373} // namespace rust
David Tolnay7db73692019-10-20 14:51:12 -0400374
375extern "C" {
David Tolnay0f0162f2020-11-16 23:43:37 -0800376void cxxbridge1$unique_ptr$std$string$null(
David Tolnay7db73692019-10-20 14:51:12 -0400377 std::unique_ptr<std::string> *ptr) noexcept {
378 new (ptr) std::unique_ptr<std::string>();
379}
David Tolnay0f0162f2020-11-16 23:43:37 -0800380void cxxbridge1$unique_ptr$std$string$raw(std::unique_ptr<std::string> *ptr,
381 std::string *raw) noexcept {
David Tolnay7db73692019-10-20 14:51:12 -0400382 new (ptr) std::unique_ptr<std::string>(raw);
383}
David Tolnay0f0162f2020-11-16 23:43:37 -0800384const std::string *cxxbridge1$unique_ptr$std$string$get(
David Tolnay7db73692019-10-20 14:51:12 -0400385 const std::unique_ptr<std::string> &ptr) noexcept {
386 return ptr.get();
387}
David Tolnay0f0162f2020-11-16 23:43:37 -0800388std::string *cxxbridge1$unique_ptr$std$string$release(
David Tolnay7db73692019-10-20 14:51:12 -0400389 std::unique_ptr<std::string> &ptr) noexcept {
390 return ptr.release();
391}
David Tolnay0f0162f2020-11-16 23:43:37 -0800392void cxxbridge1$unique_ptr$std$string$drop(
David Tolnay7db73692019-10-20 14:51:12 -0400393 std::unique_ptr<std::string> *ptr) noexcept {
394 ptr->~unique_ptr();
395}
396} // extern "C"
Myron Ahneba35cf2020-02-05 19:41:51 +0700397
David Tolnay06677b32020-11-23 18:05:45 -0800398namespace {
David Tolnaybe3cbf72020-12-12 22:12:07 -0800399const std::size_t kMaxExpectedWordsInString = 8;
David Tolnaybb3ff5d2020-11-15 19:45:11 -0800400static_assert(alignof(std::string) <= alignof(void *),
401 "unexpectedly large std::string alignment");
David Tolnay06677b32020-11-23 18:05:45 -0800402static_assert(sizeof(std::string) <= kMaxExpectedWordsInString * sizeof(void *),
David Tolnaybb3ff5d2020-11-15 19:45:11 -0800403 "unexpectedly large std::string size");
David Tolnayc26de542020-11-23 18:18:19 -0800404} // namespace
David Tolnaybb3ff5d2020-11-15 19:45:11 -0800405
David Tolnay37dd7e12020-04-25 12:51:59 -0700406#define STD_VECTOR_OPS(RUST_TYPE, CXX_TYPE) \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800407 std::size_t cxxbridge1$std$vector$##RUST_TYPE##$size( \
David Tolnay37dd7e12020-04-25 12:51:59 -0700408 const std::vector<CXX_TYPE> &s) noexcept { \
409 return s.size(); \
410 } \
David Tolnay767e00d2020-12-21 17:12:27 -0800411 CXX_TYPE *cxxbridge1$std$vector$##RUST_TYPE##$get_unchecked( \
412 std::vector<CXX_TYPE> *s, std::size_t pos) noexcept { \
413 return &(*s)[pos]; \
David Tolnay37dd7e12020-04-25 12:51:59 -0700414 } \
David Tolnay0f0162f2020-11-16 23:43:37 -0800415 void cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$null( \
David Tolnay996db1e2020-04-24 14:46:31 -0700416 std::unique_ptr<std::vector<CXX_TYPE>> *ptr) noexcept { \
417 new (ptr) std::unique_ptr<std::vector<CXX_TYPE>>(); \
David Tolnay37dd7e12020-04-25 12:51:59 -0700418 } \
David Tolnay0f0162f2020-11-16 23:43:37 -0800419 void cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$raw( \
David Tolnay996db1e2020-04-24 14:46:31 -0700420 std::unique_ptr<std::vector<CXX_TYPE>> *ptr, \
David Tolnay37dd7e12020-04-25 12:51:59 -0700421 std::vector<CXX_TYPE> *raw) noexcept { \
David Tolnay996db1e2020-04-24 14:46:31 -0700422 new (ptr) std::unique_ptr<std::vector<CXX_TYPE>>(raw); \
David Tolnay37dd7e12020-04-25 12:51:59 -0700423 } \
David Tolnay4e7e7c42020-04-24 14:48:07 -0700424 const std::vector<CXX_TYPE> \
David Tolnay0f0162f2020-11-16 23:43:37 -0800425 *cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$get( \
David Tolnay996db1e2020-04-24 14:46:31 -0700426 const std::unique_ptr<std::vector<CXX_TYPE>> &ptr) noexcept { \
David Tolnay37dd7e12020-04-25 12:51:59 -0700427 return ptr.get(); \
428 } \
David Tolnay4e7e7c42020-04-24 14:48:07 -0700429 std::vector<CXX_TYPE> \
David Tolnay0f0162f2020-11-16 23:43:37 -0800430 *cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$release( \
David Tolnay996db1e2020-04-24 14:46:31 -0700431 std::unique_ptr<std::vector<CXX_TYPE>> &ptr) noexcept { \
David Tolnay37dd7e12020-04-25 12:51:59 -0700432 return ptr.release(); \
433 } \
David Tolnay0f0162f2020-11-16 23:43:37 -0800434 void cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$drop( \
David Tolnay996db1e2020-04-24 14:46:31 -0700435 std::unique_ptr<std::vector<CXX_TYPE>> *ptr) noexcept { \
David Tolnay37dd7e12020-04-25 12:51:59 -0700436 ptr->~unique_ptr(); \
David Tolnay4e7e7c42020-04-24 14:48:07 -0700437 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700438
David Tolnay6787be62020-04-25 11:01:02 -0700439#define RUST_VEC_EXTERNS(RUST_TYPE, CXX_TYPE) \
David Tolnay0f0162f2020-11-16 23:43:37 -0800440 void cxxbridge1$rust_vec$##RUST_TYPE##$new( \
David Tolnayf97c2d52020-04-25 16:37:48 -0700441 rust::Vec<CXX_TYPE> *ptr) noexcept; \
David Tolnay0f0162f2020-11-16 23:43:37 -0800442 void cxxbridge1$rust_vec$##RUST_TYPE##$drop( \
David Tolnay6787be62020-04-25 11:01:02 -0700443 rust::Vec<CXX_TYPE> *ptr) noexcept; \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800444 std::size_t cxxbridge1$rust_vec$##RUST_TYPE##$len( \
David Tolnay6787be62020-04-25 11:01:02 -0700445 const rust::Vec<CXX_TYPE> *ptr) noexcept; \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800446 std::size_t cxxbridge1$rust_vec$##RUST_TYPE##$capacity( \
David Tolnaydc62d712020-12-11 13:51:53 -0800447 const rust::Vec<CXX_TYPE> *ptr) noexcept; \
David Tolnay0f0162f2020-11-16 23:43:37 -0800448 const CXX_TYPE *cxxbridge1$rust_vec$##RUST_TYPE##$data( \
David Tolnay6787be62020-04-25 11:01:02 -0700449 const rust::Vec<CXX_TYPE> *ptr) noexcept; \
David Tolnay0f0162f2020-11-16 23:43:37 -0800450 void cxxbridge1$rust_vec$##RUST_TYPE##$reserve_total( \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800451 rust::Vec<CXX_TYPE> *ptr, std::size_t cap) noexcept; \
David Tolnay0f0162f2020-11-16 23:43:37 -0800452 void cxxbridge1$rust_vec$##RUST_TYPE##$set_len(rust::Vec<CXX_TYPE> *ptr, \
David Tolnaye1df7dd2020-12-27 02:51:51 -0800453 std::size_t len) noexcept;
David Tolnay6787be62020-04-25 11:01:02 -0700454
455#define RUST_VEC_OPS(RUST_TYPE, CXX_TYPE) \
456 template <> \
David Tolnay1768d8f2020-04-25 18:15:11 -0700457 Vec<CXX_TYPE>::Vec() noexcept { \
David Tolnay0f0162f2020-11-16 23:43:37 -0800458 cxxbridge1$rust_vec$##RUST_TYPE##$new(this); \
David Tolnayf97c2d52020-04-25 16:37:48 -0700459 } \
460 template <> \
David Tolnay1768d8f2020-04-25 18:15:11 -0700461 void Vec<CXX_TYPE>::drop() noexcept { \
David Tolnay0f0162f2020-11-16 23:43:37 -0800462 return cxxbridge1$rust_vec$##RUST_TYPE##$drop(this); \
David Tolnay6787be62020-04-25 11:01:02 -0700463 } \
464 template <> \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800465 std::size_t Vec<CXX_TYPE>::size() const noexcept { \
David Tolnay0f0162f2020-11-16 23:43:37 -0800466 return cxxbridge1$rust_vec$##RUST_TYPE##$len(this); \
David Tolnay6787be62020-04-25 11:01:02 -0700467 } \
468 template <> \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800469 std::size_t Vec<CXX_TYPE>::capacity() const noexcept { \
David Tolnaydc62d712020-12-11 13:51:53 -0800470 return cxxbridge1$rust_vec$##RUST_TYPE##$capacity(this); \
471 } \
472 template <> \
David Tolnay1768d8f2020-04-25 18:15:11 -0700473 const CXX_TYPE *Vec<CXX_TYPE>::data() const noexcept { \
David Tolnay0f0162f2020-11-16 23:43:37 -0800474 return cxxbridge1$rust_vec$##RUST_TYPE##$data(this); \
David Tolnay6787be62020-04-25 11:01:02 -0700475 } \
476 template <> \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800477 void Vec<CXX_TYPE>::reserve_total(std::size_t cap) noexcept { \
David Tolnay0f0162f2020-11-16 23:43:37 -0800478 cxxbridge1$rust_vec$##RUST_TYPE##$reserve_total(this, cap); \
David Tolnayfb6b73c2020-11-10 14:32:16 -0800479 } \
480 template <> \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800481 void Vec<CXX_TYPE>::set_len(std::size_t len) noexcept { \
David Tolnay0f0162f2020-11-16 23:43:37 -0800482 cxxbridge1$rust_vec$##RUST_TYPE##$set_len(this, len); \
David Tolnay6787be62020-04-25 11:01:02 -0700483 }
484
David Tolnay5b163402020-12-10 19:26:02 -0800485#define SHARED_PTR_OPS(RUST_TYPE, CXX_TYPE) \
486 static_assert(sizeof(std::shared_ptr<CXX_TYPE>) == 2 * sizeof(void *), ""); \
487 static_assert(alignof(std::shared_ptr<CXX_TYPE>) == alignof(void *), ""); \
488 void cxxbridge1$std$shared_ptr$##RUST_TYPE##$null( \
489 std::shared_ptr<CXX_TYPE> *ptr) noexcept { \
490 new (ptr) std::shared_ptr<CXX_TYPE>(); \
491 } \
492 CXX_TYPE *cxxbridge1$std$shared_ptr$##RUST_TYPE##$uninit( \
493 std::shared_ptr<CXX_TYPE> *ptr) noexcept { \
494 CXX_TYPE *uninit = \
495 reinterpret_cast<CXX_TYPE *>(new rust::MaybeUninit<CXX_TYPE>); \
496 new (ptr) std::shared_ptr<CXX_TYPE>(uninit); \
497 return uninit; \
498 } \
499 void cxxbridge1$std$shared_ptr$##RUST_TYPE##$clone( \
500 const std::shared_ptr<CXX_TYPE> &self, \
501 std::shared_ptr<CXX_TYPE> *ptr) noexcept { \
502 new (ptr) std::shared_ptr<CXX_TYPE>(self); \
503 } \
504 const CXX_TYPE *cxxbridge1$std$shared_ptr$##RUST_TYPE##$get( \
505 const std::shared_ptr<CXX_TYPE> &self) noexcept { \
506 return self.get(); \
507 } \
508 void cxxbridge1$std$shared_ptr$##RUST_TYPE##$drop( \
509 const std::shared_ptr<CXX_TYPE> *self) noexcept { \
510 self->~shared_ptr(); \
511 }
512
David Tolnay6787be62020-04-25 11:01:02 -0700513// Usize and isize are the same type as one of the below.
David Tolnayf336b3b2020-04-30 08:45:54 -0700514#define FOR_EACH_NUMERIC(MACRO) \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800515 MACRO(u8, std::uint8_t) \
516 MACRO(u16, std::uint16_t) \
517 MACRO(u32, std::uint32_t) \
518 MACRO(u64, std::uint64_t) \
519 MACRO(i8, std::int8_t) \
520 MACRO(i16, std::int16_t) \
521 MACRO(i32, std::int32_t) \
522 MACRO(i64, std::int64_t) \
David Tolnay6787be62020-04-25 11:01:02 -0700523 MACRO(f32, float) \
524 MACRO(f64, double)
525
David Tolnayf336b3b2020-04-30 08:45:54 -0700526#define FOR_EACH_STD_VECTOR(MACRO) \
527 FOR_EACH_NUMERIC(MACRO) \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800528 MACRO(usize, std::size_t) \
David Tolnay47e239d2020-08-28 00:32:04 -0700529 MACRO(isize, rust::isize) \
530 MACRO(string, std::string)
David Tolnay6787be62020-04-25 11:01:02 -0700531
David Tolnayf336b3b2020-04-30 08:45:54 -0700532#define FOR_EACH_RUST_VEC(MACRO) \
533 FOR_EACH_NUMERIC(MACRO) \
David Tolnay33f56ad2020-08-27 17:06:35 -0700534 MACRO(bool, bool) \
David Tolnay93e71d02020-11-25 20:16:52 -0800535 MACRO(char, char) \
David Tolnay33f56ad2020-08-27 17:06:35 -0700536 MACRO(string, rust::String)
David Tolnayf336b3b2020-04-30 08:45:54 -0700537
David Tolnay5b163402020-12-10 19:26:02 -0800538#define FOR_EACH_SHARED_PTR(MACRO) \
539 FOR_EACH_NUMERIC(MACRO) \
David Tolnaycd1430c2020-12-28 17:17:14 -0800540 MACRO(bool, bool) \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800541 MACRO(usize, std::size_t) \
David Tolnay5b163402020-12-10 19:26:02 -0800542 MACRO(isize, rust::isize) \
543 MACRO(string, std::string)
544
David Tolnay4e7e7c42020-04-24 14:48:07 -0700545extern "C" {
David Tolnayf336b3b2020-04-30 08:45:54 -0700546FOR_EACH_STD_VECTOR(STD_VECTOR_OPS)
547FOR_EACH_RUST_VEC(RUST_VEC_EXTERNS)
David Tolnay5b163402020-12-10 19:26:02 -0800548FOR_EACH_SHARED_PTR(SHARED_PTR_OPS)
David Tolnay4e7e7c42020-04-24 14:48:07 -0700549} // extern "C"
David Tolnay6787be62020-04-25 11:01:02 -0700550
David Tolnay1768d8f2020-04-25 18:15:11 -0700551namespace rust {
David Tolnay0f0162f2020-11-16 23:43:37 -0800552inline namespace cxxbridge1 {
David Tolnayf336b3b2020-04-30 08:45:54 -0700553FOR_EACH_RUST_VEC(RUST_VEC_OPS)
David Tolnay0f0162f2020-11-16 23:43:37 -0800554} // namespace cxxbridge1
David Tolnay1768d8f2020-04-25 18:15:11 -0700555} // namespace rust