blob: 9983fbaabe36113f09d1b5263a478c7f059c66aa [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 Tolnay1202de52021-01-02 01:26:33 -080042void cxxbridge1$str$new(rust::Str *self) noexcept;
43void cxxbridge1$str$ref(rust::Str *self, const rust::String *string) noexcept;
44bool cxxbridge1$str$from(rust::Str *self, const char *ptr,
45 std::size_t len) noexcept;
46const char *cxxbridge1$str$ptr(const rust::Str *self) noexcept;
47std::size_t cxxbridge1$str$len(const rust::Str *self) noexcept;
David Tolnay9bffb932021-01-02 02:15:21 -080048
49// rust::Slice
50void cxxbridge1$slice$new(void *self, const void *ptr,
51 std::size_t len) noexcept;
52void *cxxbridge1$slice$ptr(const void *self) noexcept;
53std::size_t cxxbridge1$slice$len(const void *self) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -040054} // extern "C"
55
David Tolnay750755e2020-03-01 13:04:08 -080056namespace rust {
David Tolnay0f0162f2020-11-16 23:43:37 -080057inline namespace cxxbridge1 {
David Tolnay7db73692019-10-20 14:51:12 -040058
David Tolnay521d99d2020-08-26 20:45:40 -070059template <typename Exception>
60void panic [[noreturn]] (const char *msg) {
61#if defined(RUST_CXX_NO_EXCEPTIONS)
62 std::cerr << "Error: " << msg << ". Aborting." << std::endl;
63 std::terminate();
64#else
65 throw Exception(msg);
66#endif
67}
68
David Tolnayb10c4bc2020-08-26 21:55:29 -070069template void panic<std::out_of_range>[[noreturn]] (const char *msg);
David Tolnay521d99d2020-08-26 20:45:40 -070070
David Tolnay0f0162f2020-11-16 23:43:37 -080071String::String() noexcept { cxxbridge1$string$new(this); }
David Tolnay7db73692019-10-20 14:51:12 -040072
David Tolnay56082162020-03-01 12:57:33 -080073String::String(const String &other) noexcept {
David Tolnay0f0162f2020-11-16 23:43:37 -080074 cxxbridge1$string$clone(this, other);
David Tolnay7db73692019-10-20 14:51:12 -040075}
76
David Tolnay15671862020-11-23 18:13:56 -080077String::String(String &&other) noexcept : repr(other.repr) {
David Tolnay0f0162f2020-11-16 23:43:37 -080078 cxxbridge1$string$new(&other);
David Tolnay7db73692019-10-20 14:51:12 -040079}
80
David Tolnay0f0162f2020-11-16 23:43:37 -080081String::~String() noexcept { cxxbridge1$string$drop(this); }
David Tolnay7db73692019-10-20 14:51:12 -040082
David Tolnaybe3cbf72020-12-12 22:12:07 -080083static void initString(String *self, const char *s, std::size_t len) {
David Tolnay0f0162f2020-11-16 23:43:37 -080084 if (!cxxbridge1$string$from(self, s, len)) {
David Tolnay8d323662020-10-30 19:32:26 -070085 panic<std::invalid_argument>("data for rust::String is not utf-8");
86 }
87}
David Tolnay7db73692019-10-20 14:51:12 -040088
David Tolnay032d8532020-10-30 20:47:31 -070089String::String(const std::string &s) { initString(this, s.data(), s.length()); }
90
David Tolnay54b13222020-10-30 20:58:32 -070091String::String(const char *s) {
92 assert(s != nullptr);
93 initString(this, s, std::strlen(s));
94}
David Tolnayc2bbd952020-07-29 18:15:26 -070095
David Tolnaybe3cbf72020-12-12 22:12:07 -080096String::String(const char *s, std::size_t len) {
David Tolnay54b13222020-10-30 20:58:32 -070097 assert(s != nullptr || len == 0);
David Tolnay032d8532020-10-30 20:47:31 -070098 initString(this,
99 s == nullptr && len == 0 ? reinterpret_cast<const char *>(1) : s,
100 len);
David Tolnay7db73692019-10-20 14:51:12 -0400101}
102
David Tolnay56082162020-03-01 12:57:33 -0800103String &String::operator=(const String &other) noexcept {
David Tolnay7db73692019-10-20 14:51:12 -0400104 if (this != &other) {
David Tolnay0f0162f2020-11-16 23:43:37 -0800105 cxxbridge1$string$drop(this);
106 cxxbridge1$string$clone(this, other);
David Tolnay7db73692019-10-20 14:51:12 -0400107 }
108 return *this;
109}
110
David Tolnay56082162020-03-01 12:57:33 -0800111String &String::operator=(String &&other) noexcept {
David Tolnay7db73692019-10-20 14:51:12 -0400112 if (this != &other) {
David Tolnay0f0162f2020-11-16 23:43:37 -0800113 cxxbridge1$string$drop(this);
David Tolnay7db73692019-10-20 14:51:12 -0400114 this->repr = other.repr;
David Tolnay0f0162f2020-11-16 23:43:37 -0800115 cxxbridge1$string$new(&other);
David Tolnay7db73692019-10-20 14:51:12 -0400116 }
117 return *this;
118}
119
David Tolnayd9c4ac92020-03-01 20:33:58 -0800120String::operator std::string() const {
121 return std::string(this->data(), this->size());
122}
123
David Tolnay56082162020-03-01 12:57:33 -0800124const char *String::data() const noexcept {
David Tolnay0f0162f2020-11-16 23:43:37 -0800125 return cxxbridge1$string$ptr(this);
David Tolnay7db73692019-10-20 14:51:12 -0400126}
127
David Tolnaybe3cbf72020-12-12 22:12:07 -0800128std::size_t String::size() const noexcept {
129 return cxxbridge1$string$len(this);
130}
David Tolnay7db73692019-10-20 14:51:12 -0400131
David Tolnaybe3cbf72020-12-12 22:12:07 -0800132std::size_t String::length() const noexcept {
133 return cxxbridge1$string$len(this);
134}
David Tolnay7db73692019-10-20 14:51:12 -0400135
David Tolnaycca2e612020-12-18 12:48:22 -0800136const char *String::c_str() noexcept {
137 auto len = this->length();
138 cxxbridge1$string$reserve_total(this, len + 1);
139 auto ptr = this->data();
140 const_cast<char *>(ptr)[len] = '\0';
141 return ptr;
142}
143
David Tolnayff7f5fb2020-11-25 20:50:32 -0800144String::iterator String::begin() noexcept {
145 return const_cast<char *>(this->data());
146}
147
148String::iterator String::end() noexcept {
149 return const_cast<char *>(this->data()) + this->size();
150}
151
152String::const_iterator String::begin() const noexcept { return this->cbegin(); }
153
154String::const_iterator String::end() const noexcept { return this->cend(); }
155
156String::const_iterator String::cbegin() const noexcept { return this->data(); }
157
158String::const_iterator String::cend() const noexcept {
159 return this->data() + this->size();
160}
161
David Tolnayff86dce2020-11-29 19:45:13 -0800162bool String::operator==(const String &rhs) const noexcept {
163 return rust::Str(*this) == rust::Str(rhs);
164}
165
166bool String::operator!=(const String &rhs) const noexcept {
167 return rust::Str(*this) != rust::Str(rhs);
168}
169
170bool String::operator<(const String &rhs) const noexcept {
171 return rust::Str(*this) < rust::Str(rhs);
172}
173
174bool String::operator<=(const String &rhs) const noexcept {
175 return rust::Str(*this) <= rust::Str(rhs);
176}
177
178bool String::operator>(const String &rhs) const noexcept {
179 return rust::Str(*this) > rust::Str(rhs);
180}
181
182bool String::operator>=(const String &rhs) const noexcept {
183 return rust::Str(*this) >= rust::Str(rhs);
184}
185
David Tolnayfec17152021-01-02 12:51:29 -0800186void String::swap(String &rhs) noexcept {
187 using std::swap;
188 swap(this->repr, rhs.repr);
189}
190
David Tolnayd1e2efc2020-03-03 22:25:43 -0800191String::String(unsafe_bitcopy_t, const String &bits) noexcept
192 : repr(bits.repr) {}
193
David Tolnay56082162020-03-01 12:57:33 -0800194std::ostream &operator<<(std::ostream &os, const String &s) {
David Tolnay7db73692019-10-20 14:51:12 -0400195 os.write(s.data(), s.size());
196 return os;
197}
198
David Tolnay1202de52021-01-02 01:26:33 -0800199Str::Str() noexcept { cxxbridge1$str$new(this); }
David Tolnay7db73692019-10-20 14:51:12 -0400200
David Tolnay1202de52021-01-02 01:26:33 -0800201Str::Str(const String &s) noexcept { cxxbridge1$str$ref(this, &s); }
David Tolnay828e5132020-11-29 20:40:40 -0800202
David Tolnay1202de52021-01-02 01:26:33 -0800203static void initStr(Str *self, const char *ptr, std::size_t len) {
204 if (!cxxbridge1$str$from(self, ptr, len)) {
David Tolnay8d323662020-10-30 19:32:26 -0700205 panic<std::invalid_argument>("data for rust::Str is not utf-8");
206 }
207}
David Tolnay7db73692019-10-20 14:51:12 -0400208
David Tolnay1202de52021-01-02 01:26:33 -0800209Str::Str(const std::string &s) { initStr(this, s.data(), s.length()); }
David Tolnay894c5e42020-07-29 18:20:00 -0700210
David Tolnay1202de52021-01-02 01:26:33 -0800211Str::Str(const char *s) {
David Tolnay54b13222020-10-30 20:58:32 -0700212 assert(s != nullptr);
David Tolnay1202de52021-01-02 01:26:33 -0800213 initStr(this, s, std::strlen(s));
David Tolnay54b13222020-10-30 20:58:32 -0700214}
David Tolnay032d8532020-10-30 20:47:31 -0700215
David Tolnay1202de52021-01-02 01:26:33 -0800216Str::Str(const char *s, std::size_t len) {
David Tolnay54b13222020-10-30 20:58:32 -0700217 assert(s != nullptr || len == 0);
David Tolnay1202de52021-01-02 01:26:33 -0800218 initStr(this,
219 s == nullptr && len == 0 ? reinterpret_cast<const char *>(1) : s,
220 len);
David Tolnayd9c4ac92020-03-01 20:33:58 -0800221}
David Tolnay7db73692019-10-20 14:51:12 -0400222
David Tolnay09dbe752020-03-01 13:00:40 -0800223Str::operator std::string() const {
David Tolnay7db73692019-10-20 14:51:12 -0400224 return std::string(this->data(), this->size());
225}
226
David Tolnay1202de52021-01-02 01:26:33 -0800227const char *Str::data() const noexcept { return cxxbridge1$str$ptr(this); }
228
229std::size_t Str::size() const noexcept { return cxxbridge1$str$len(this); }
230
231std::size_t Str::length() const noexcept { return this->size(); }
232
David Tolnayff7f5fb2020-11-25 20:50:32 -0800233Str::const_iterator Str::begin() const noexcept { return this->cbegin(); }
234
235Str::const_iterator Str::end() const noexcept { return this->cend(); }
236
David Tolnay1202de52021-01-02 01:26:33 -0800237Str::const_iterator Str::cbegin() const noexcept { return this->data(); }
David Tolnayff7f5fb2020-11-25 20:50:32 -0800238
David Tolnay1202de52021-01-02 01:26:33 -0800239Str::const_iterator Str::cend() const noexcept {
240 return this->data() + this->size();
241}
David Tolnayff7f5fb2020-11-25 20:50:32 -0800242
David Tolnayff86dce2020-11-29 19:45:13 -0800243bool Str::operator==(const Str &rhs) const noexcept {
David Tolnay1202de52021-01-02 01:26:33 -0800244 return this->size() == rhs.size() &&
David Tolnayff86dce2020-11-29 19:45:13 -0800245 std::equal(this->begin(), this->end(), rhs.begin());
246}
247
248bool Str::operator!=(const Str &rhs) const noexcept { return !(*this == rhs); }
249
250bool Str::operator<(const Str &rhs) const noexcept {
251 return std::lexicographical_compare(this->begin(), this->end(), rhs.begin(),
252 rhs.end());
253}
254
255bool Str::operator<=(const Str &rhs) const noexcept {
256 // std::mismatch(this->begin(), this->end(), rhs.begin(), rhs.end()), except
257 // without Undefined Behavior on C++11 if rhs is shorter than *this.
258 const_iterator liter = this->begin(), lend = this->end(), riter = rhs.begin(),
259 rend = rhs.end();
260 while (liter != lend && riter != rend && *liter == *riter) {
261 ++liter, ++riter;
262 }
263 if (liter == lend) {
264 return true; // equal or *this is a prefix of rhs
265 } else if (riter == rend) {
266 return false; // rhs is a prefix of *this
267 } else {
268 return *liter <= *riter;
269 }
270}
271
272bool Str::operator>(const Str &rhs) const noexcept { return rhs < *this; }
273
274bool Str::operator>=(const Str &rhs) const noexcept { return rhs <= *this; }
275
David Tolnayba750b62021-01-02 16:05:48 -0800276void Str::swap(Str &rhs) noexcept {
277 using std::swap;
278 swap(this->repr, rhs.repr);
279}
David Tolnay0413ee22021-01-02 13:55:16 -0800280
David Tolnay09dbe752020-03-01 13:00:40 -0800281std::ostream &operator<<(std::ostream &os, const Str &s) {
David Tolnay7db73692019-10-20 14:51:12 -0400282 os.write(s.data(), s.size());
283 return os;
284}
285
David Tolnay9bffb932021-01-02 02:15:21 -0800286void sliceInit(void *self, const void *ptr, std::size_t len) noexcept {
287 cxxbridge1$slice$new(self, ptr, len);
288}
289
290void *slicePtr(const void *self) noexcept { return cxxbridge1$slice$ptr(self); }
291
292std::size_t sliceLen(const void *self) noexcept {
293 return cxxbridge1$slice$len(self);
294}
295
David Tolnay03c43f52020-12-12 21:07:17 -0800296// Rust specifies that usize is ABI compatible with C's uintptr_t.
297// https://rust-lang.github.io/unsafe-code-guidelines/layout/scalars.html#isize-and-usize
298// However there is no direct Rust equivalent for size_t. C does not guarantee
299// that size_t and uintptr_t are compatible. In practice though, on all
300// platforms supported by Rust, they are identical for ABI purposes. See the
301// libc crate which unconditionally defines libc::size_t = usize. We expect the
302// same here and these assertions are just here to explicitly document that.
303// *Note that no assumption is made about C++ name mangling of signatures
304// containing these types, not here nor anywhere in CXX.*
David Tolnaybe3cbf72020-12-12 22:12:07 -0800305static_assert(sizeof(std::size_t) == sizeof(std::uintptr_t),
306 "unsupported size_t size");
307static_assert(alignof(std::size_t) == alignof(std::uintptr_t),
David Tolnay03c43f52020-12-12 21:07:17 -0800308 "unsupported size_t alignment");
David Tolnaybe3cbf72020-12-12 22:12:07 -0800309static_assert(sizeof(rust::isize) == sizeof(std::intptr_t),
David Tolnay03c43f52020-12-12 21:07:17 -0800310 "unsupported ssize_t size");
David Tolnaybe3cbf72020-12-12 22:12:07 -0800311static_assert(alignof(rust::isize) == alignof(std::intptr_t),
David Tolnay03c43f52020-12-12 21:07:17 -0800312 "unsupported ssize_t alignment");
313
David Tolnay9ed15c62020-10-31 18:02:03 -0700314static_assert(std::is_trivially_copy_constructible<Str>::value,
315 "trivial Str(const Str &)");
316static_assert(std::is_trivially_copy_assignable<Str>::value,
317 "trivial operator=(const Str &)");
318static_assert(std::is_trivially_destructible<Str>::value, "trivial ~Str()");
319
David Tolnay9578cf12020-11-25 14:36:46 -0800320static_assert(
David Tolnaybe3cbf72020-12-12 22:12:07 -0800321 std::is_trivially_copy_constructible<Slice<const std::uint8_t>>::value,
322 "trivial Slice(const Slice &)");
323static_assert(
324 std::is_trivially_move_constructible<Slice<const std::uint8_t>>::value,
325 "trivial Slice(Slice &&)");
326static_assert(
327 std::is_trivially_copy_assignable<Slice<const std::uint8_t>>::value,
328 "trivial Slice::operator=(const Slice &) for const slices");
329static_assert(
330 std::is_trivially_move_assignable<Slice<const std::uint8_t>>::value,
331 "trivial Slice::operator=(Slice &&)");
332static_assert(std::is_trivially_destructible<Slice<const std::uint8_t>>::value,
333 "trivial ~Slice()");
334
335static_assert(std::is_trivially_copy_constructible<Slice<std::uint8_t>>::value,
336 "trivial Slice(const Slice &)");
337static_assert(std::is_trivially_move_constructible<Slice<std::uint8_t>>::value,
338 "trivial Slice(Slice &&)");
339static_assert(!std::is_copy_assignable<Slice<std::uint8_t>>::value,
340 "delete Slice::operator=(const Slice &) for mut slices");
341static_assert(std::is_trivially_move_assignable<Slice<std::uint8_t>>::value,
342 "trivial Slice::operator=(Slice &&)");
343static_assert(std::is_trivially_destructible<Slice<std::uint8_t>>::value,
344 "trivial ~Slice()");
345
346static_assert(std::is_same<Vec<std::uint8_t>::const_iterator,
347 Vec<const std::uint8_t>::iterator>::value,
348 "Vec<T>::const_iterator == Vec<const T>::iterator");
349static_assert(std::is_same<Vec<const std::uint8_t>::const_iterator,
350 Vec<const std::uint8_t>::iterator>::value,
351 "Vec<const T>::const_iterator == Vec<const T>::iterator");
352static_assert(!std::is_same<Vec<std::uint8_t>::const_iterator,
353 Vec<std::uint8_t>::iterator>::value,
354 "Vec<T>::const_iterator != Vec<T>::iterator");
David Tolnay9578cf12020-11-25 14:36:46 -0800355
David Tolnay4ddae802021-01-02 16:47:33 -0800356static const char *errorCopy(const char *ptr, std::size_t len) {
357 char *copy = new char[len];
358 std::memcpy(copy, ptr, len);
359 return copy;
360}
361
David Tolnay1e548172020-03-16 13:37:09 -0700362extern "C" {
David Tolnaydc2d4c32021-01-02 16:13:27 -0800363const char *cxxbridge1$error(const char *ptr, std::size_t len) noexcept {
David Tolnay4ddae802021-01-02 16:47:33 -0800364 return errorCopy(ptr, len);
David Tolnay1e548172020-03-16 13:37:09 -0700365}
366} // extern "C"
367
David Tolnayd5712ee2020-10-31 17:10:00 -0700368Error::Error(const Error &other)
David Tolnay4ddae802021-01-02 16:47:33 -0800369 : std::exception(other),
370 msg(other.msg ? errorCopy(other.msg, other.len) : nullptr),
David Tolnay23c23192020-10-31 17:11:48 -0700371 len(other.len) {}
David Tolnay1e548172020-03-16 13:37:09 -0700372
David Tolnay23c23192020-10-31 17:11:48 -0700373Error::Error(Error &&other) noexcept
374 : std::exception(std::move(other)), msg(other.msg), len(other.len) {
David Tolnaya0c9bc72020-10-31 14:37:14 -0700375 other.msg = nullptr;
376 other.len = 0;
David Tolnay1e548172020-03-16 13:37:09 -0700377}
378
David Tolnaya0c9bc72020-10-31 14:37:14 -0700379Error::~Error() noexcept { delete[] this->msg; }
David Tolnay1e548172020-03-16 13:37:09 -0700380
David Tolnay7c6ac712020-10-31 17:22:28 -0700381Error &Error::operator=(const Error &other) {
382 if (this != &other) {
383 std::exception::operator=(other);
384 delete[] this->msg;
385 this->msg = nullptr;
David Tolnay4ddae802021-01-02 16:47:33 -0800386 if (other.msg) {
387 this->msg = errorCopy(other.msg, other.len);
388 this->len = other.len;
389 }
David Tolnay7c6ac712020-10-31 17:22:28 -0700390 }
391 return *this;
392}
393
David Tolnay15491062020-10-31 17:25:13 -0700394Error &Error::operator=(Error &&other) noexcept {
395 if (this != &other) {
396 std::exception::operator=(std::move(other));
397 this->msg = other.msg;
398 this->len = other.len;
399 other.msg = nullptr;
400 other.len = 0;
401 }
402 return *this;
403}
404
David Tolnaya0c9bc72020-10-31 14:37:14 -0700405const char *Error::what() const noexcept { return this->msg; }
David Tolnay1e548172020-03-16 13:37:09 -0700406
David Tolnay5b163402020-12-10 19:26:02 -0800407namespace {
408template <typename T>
409union MaybeUninit {
410 T value;
411 MaybeUninit() {}
412 ~MaybeUninit() {}
413};
414} // namespace
415
David Tolnay0f0162f2020-11-16 23:43:37 -0800416} // namespace cxxbridge1
David Tolnay750755e2020-03-01 13:04:08 -0800417} // namespace rust
David Tolnay7db73692019-10-20 14:51:12 -0400418
419extern "C" {
David Tolnay0f0162f2020-11-16 23:43:37 -0800420void cxxbridge1$unique_ptr$std$string$null(
David Tolnay7db73692019-10-20 14:51:12 -0400421 std::unique_ptr<std::string> *ptr) noexcept {
422 new (ptr) std::unique_ptr<std::string>();
423}
David Tolnay0f0162f2020-11-16 23:43:37 -0800424void cxxbridge1$unique_ptr$std$string$raw(std::unique_ptr<std::string> *ptr,
425 std::string *raw) noexcept {
David Tolnay7db73692019-10-20 14:51:12 -0400426 new (ptr) std::unique_ptr<std::string>(raw);
427}
David Tolnay0f0162f2020-11-16 23:43:37 -0800428const std::string *cxxbridge1$unique_ptr$std$string$get(
David Tolnay7db73692019-10-20 14:51:12 -0400429 const std::unique_ptr<std::string> &ptr) noexcept {
430 return ptr.get();
431}
David Tolnay0f0162f2020-11-16 23:43:37 -0800432std::string *cxxbridge1$unique_ptr$std$string$release(
David Tolnay7db73692019-10-20 14:51:12 -0400433 std::unique_ptr<std::string> &ptr) noexcept {
434 return ptr.release();
435}
David Tolnay0f0162f2020-11-16 23:43:37 -0800436void cxxbridge1$unique_ptr$std$string$drop(
David Tolnay7db73692019-10-20 14:51:12 -0400437 std::unique_ptr<std::string> *ptr) noexcept {
438 ptr->~unique_ptr();
439}
440} // extern "C"
Myron Ahneba35cf2020-02-05 19:41:51 +0700441
David Tolnay06677b32020-11-23 18:05:45 -0800442namespace {
David Tolnaybe3cbf72020-12-12 22:12:07 -0800443const std::size_t kMaxExpectedWordsInString = 8;
David Tolnaybb3ff5d2020-11-15 19:45:11 -0800444static_assert(alignof(std::string) <= alignof(void *),
445 "unexpectedly large std::string alignment");
David Tolnay06677b32020-11-23 18:05:45 -0800446static_assert(sizeof(std::string) <= kMaxExpectedWordsInString * sizeof(void *),
David Tolnaybb3ff5d2020-11-15 19:45:11 -0800447 "unexpectedly large std::string size");
David Tolnayc26de542020-11-23 18:18:19 -0800448} // namespace
David Tolnaybb3ff5d2020-11-15 19:45:11 -0800449
David Tolnay37dd7e12020-04-25 12:51:59 -0700450#define STD_VECTOR_OPS(RUST_TYPE, CXX_TYPE) \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800451 std::size_t cxxbridge1$std$vector$##RUST_TYPE##$size( \
David Tolnay37dd7e12020-04-25 12:51:59 -0700452 const std::vector<CXX_TYPE> &s) noexcept { \
453 return s.size(); \
454 } \
David Tolnay767e00d2020-12-21 17:12:27 -0800455 CXX_TYPE *cxxbridge1$std$vector$##RUST_TYPE##$get_unchecked( \
456 std::vector<CXX_TYPE> *s, std::size_t pos) noexcept { \
457 return &(*s)[pos]; \
David Tolnay37dd7e12020-04-25 12:51:59 -0700458 } \
David Tolnay0f0162f2020-11-16 23:43:37 -0800459 void cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$null( \
David Tolnay996db1e2020-04-24 14:46:31 -0700460 std::unique_ptr<std::vector<CXX_TYPE>> *ptr) noexcept { \
461 new (ptr) std::unique_ptr<std::vector<CXX_TYPE>>(); \
David Tolnay37dd7e12020-04-25 12:51:59 -0700462 } \
David Tolnay0f0162f2020-11-16 23:43:37 -0800463 void cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$raw( \
David Tolnay996db1e2020-04-24 14:46:31 -0700464 std::unique_ptr<std::vector<CXX_TYPE>> *ptr, \
David Tolnay37dd7e12020-04-25 12:51:59 -0700465 std::vector<CXX_TYPE> *raw) noexcept { \
David Tolnay996db1e2020-04-24 14:46:31 -0700466 new (ptr) std::unique_ptr<std::vector<CXX_TYPE>>(raw); \
David Tolnay37dd7e12020-04-25 12:51:59 -0700467 } \
David Tolnay4e7e7c42020-04-24 14:48:07 -0700468 const std::vector<CXX_TYPE> \
David Tolnay0f0162f2020-11-16 23:43:37 -0800469 *cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$get( \
David Tolnay996db1e2020-04-24 14:46:31 -0700470 const std::unique_ptr<std::vector<CXX_TYPE>> &ptr) noexcept { \
David Tolnay37dd7e12020-04-25 12:51:59 -0700471 return ptr.get(); \
472 } \
David Tolnay4e7e7c42020-04-24 14:48:07 -0700473 std::vector<CXX_TYPE> \
David Tolnay0f0162f2020-11-16 23:43:37 -0800474 *cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$release( \
David Tolnay996db1e2020-04-24 14:46:31 -0700475 std::unique_ptr<std::vector<CXX_TYPE>> &ptr) noexcept { \
David Tolnay37dd7e12020-04-25 12:51:59 -0700476 return ptr.release(); \
477 } \
David Tolnay0f0162f2020-11-16 23:43:37 -0800478 void cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$drop( \
David Tolnay996db1e2020-04-24 14:46:31 -0700479 std::unique_ptr<std::vector<CXX_TYPE>> *ptr) noexcept { \
David Tolnay37dd7e12020-04-25 12:51:59 -0700480 ptr->~unique_ptr(); \
David Tolnay4e7e7c42020-04-24 14:48:07 -0700481 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700482
David Tolnay6787be62020-04-25 11:01:02 -0700483#define RUST_VEC_EXTERNS(RUST_TYPE, CXX_TYPE) \
David Tolnay0f0162f2020-11-16 23:43:37 -0800484 void cxxbridge1$rust_vec$##RUST_TYPE##$new( \
David Tolnayf97c2d52020-04-25 16:37:48 -0700485 rust::Vec<CXX_TYPE> *ptr) noexcept; \
David Tolnay0f0162f2020-11-16 23:43:37 -0800486 void cxxbridge1$rust_vec$##RUST_TYPE##$drop( \
David Tolnay6787be62020-04-25 11:01:02 -0700487 rust::Vec<CXX_TYPE> *ptr) noexcept; \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800488 std::size_t cxxbridge1$rust_vec$##RUST_TYPE##$len( \
David Tolnay6787be62020-04-25 11:01:02 -0700489 const rust::Vec<CXX_TYPE> *ptr) noexcept; \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800490 std::size_t cxxbridge1$rust_vec$##RUST_TYPE##$capacity( \
David Tolnaydc62d712020-12-11 13:51:53 -0800491 const rust::Vec<CXX_TYPE> *ptr) noexcept; \
David Tolnay0f0162f2020-11-16 23:43:37 -0800492 const CXX_TYPE *cxxbridge1$rust_vec$##RUST_TYPE##$data( \
David Tolnay6787be62020-04-25 11:01:02 -0700493 const rust::Vec<CXX_TYPE> *ptr) noexcept; \
David Tolnay0f0162f2020-11-16 23:43:37 -0800494 void cxxbridge1$rust_vec$##RUST_TYPE##$reserve_total( \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800495 rust::Vec<CXX_TYPE> *ptr, std::size_t cap) noexcept; \
David Tolnay0f0162f2020-11-16 23:43:37 -0800496 void cxxbridge1$rust_vec$##RUST_TYPE##$set_len(rust::Vec<CXX_TYPE> *ptr, \
David Tolnaye1df7dd2020-12-27 02:51:51 -0800497 std::size_t len) noexcept;
David Tolnay6787be62020-04-25 11:01:02 -0700498
499#define RUST_VEC_OPS(RUST_TYPE, CXX_TYPE) \
500 template <> \
David Tolnay1768d8f2020-04-25 18:15:11 -0700501 Vec<CXX_TYPE>::Vec() noexcept { \
David Tolnay0f0162f2020-11-16 23:43:37 -0800502 cxxbridge1$rust_vec$##RUST_TYPE##$new(this); \
David Tolnayf97c2d52020-04-25 16:37:48 -0700503 } \
504 template <> \
David Tolnay1768d8f2020-04-25 18:15:11 -0700505 void Vec<CXX_TYPE>::drop() noexcept { \
David Tolnay0f0162f2020-11-16 23:43:37 -0800506 return cxxbridge1$rust_vec$##RUST_TYPE##$drop(this); \
David Tolnay6787be62020-04-25 11:01:02 -0700507 } \
508 template <> \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800509 std::size_t Vec<CXX_TYPE>::size() const noexcept { \
David Tolnay0f0162f2020-11-16 23:43:37 -0800510 return cxxbridge1$rust_vec$##RUST_TYPE##$len(this); \
David Tolnay6787be62020-04-25 11:01:02 -0700511 } \
512 template <> \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800513 std::size_t Vec<CXX_TYPE>::capacity() const noexcept { \
David Tolnaydc62d712020-12-11 13:51:53 -0800514 return cxxbridge1$rust_vec$##RUST_TYPE##$capacity(this); \
515 } \
516 template <> \
David Tolnay1768d8f2020-04-25 18:15:11 -0700517 const CXX_TYPE *Vec<CXX_TYPE>::data() const noexcept { \
David Tolnay0f0162f2020-11-16 23:43:37 -0800518 return cxxbridge1$rust_vec$##RUST_TYPE##$data(this); \
David Tolnay6787be62020-04-25 11:01:02 -0700519 } \
520 template <> \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800521 void Vec<CXX_TYPE>::reserve_total(std::size_t cap) noexcept { \
David Tolnay0f0162f2020-11-16 23:43:37 -0800522 cxxbridge1$rust_vec$##RUST_TYPE##$reserve_total(this, cap); \
David Tolnayfb6b73c2020-11-10 14:32:16 -0800523 } \
524 template <> \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800525 void Vec<CXX_TYPE>::set_len(std::size_t len) noexcept { \
David Tolnay0f0162f2020-11-16 23:43:37 -0800526 cxxbridge1$rust_vec$##RUST_TYPE##$set_len(this, len); \
David Tolnay6787be62020-04-25 11:01:02 -0700527 }
528
David Tolnay5b163402020-12-10 19:26:02 -0800529#define SHARED_PTR_OPS(RUST_TYPE, CXX_TYPE) \
530 static_assert(sizeof(std::shared_ptr<CXX_TYPE>) == 2 * sizeof(void *), ""); \
531 static_assert(alignof(std::shared_ptr<CXX_TYPE>) == alignof(void *), ""); \
532 void cxxbridge1$std$shared_ptr$##RUST_TYPE##$null( \
533 std::shared_ptr<CXX_TYPE> *ptr) noexcept { \
534 new (ptr) std::shared_ptr<CXX_TYPE>(); \
535 } \
536 CXX_TYPE *cxxbridge1$std$shared_ptr$##RUST_TYPE##$uninit( \
537 std::shared_ptr<CXX_TYPE> *ptr) noexcept { \
538 CXX_TYPE *uninit = \
539 reinterpret_cast<CXX_TYPE *>(new rust::MaybeUninit<CXX_TYPE>); \
540 new (ptr) std::shared_ptr<CXX_TYPE>(uninit); \
541 return uninit; \
542 } \
543 void cxxbridge1$std$shared_ptr$##RUST_TYPE##$clone( \
544 const std::shared_ptr<CXX_TYPE> &self, \
545 std::shared_ptr<CXX_TYPE> *ptr) noexcept { \
546 new (ptr) std::shared_ptr<CXX_TYPE>(self); \
547 } \
548 const CXX_TYPE *cxxbridge1$std$shared_ptr$##RUST_TYPE##$get( \
549 const std::shared_ptr<CXX_TYPE> &self) noexcept { \
550 return self.get(); \
551 } \
552 void cxxbridge1$std$shared_ptr$##RUST_TYPE##$drop( \
553 const std::shared_ptr<CXX_TYPE> *self) noexcept { \
554 self->~shared_ptr(); \
David Tolnay215e77f2020-12-28 17:09:48 -0800555 } \
556 static_assert(sizeof(std::weak_ptr<CXX_TYPE>) == 2 * sizeof(void *), ""); \
557 static_assert(alignof(std::weak_ptr<CXX_TYPE>) == alignof(void *), ""); \
558 void cxxbridge1$std$weak_ptr$##RUST_TYPE##$null( \
559 std::weak_ptr<CXX_TYPE> *ptr) noexcept { \
560 new (ptr) std::weak_ptr<CXX_TYPE>(); \
561 } \
562 void cxxbridge1$std$weak_ptr$##RUST_TYPE##$clone( \
563 const std::weak_ptr<CXX_TYPE> &self, \
564 std::weak_ptr<CXX_TYPE> *ptr) noexcept { \
565 new (ptr) std::weak_ptr<CXX_TYPE>(self); \
566 } \
David Tolnay85b6bc42020-12-28 17:47:51 -0800567 void cxxbridge1$std$weak_ptr$##RUST_TYPE##$downgrade( \
568 const std::shared_ptr<CXX_TYPE> &shared, \
569 std::weak_ptr<CXX_TYPE> *weak) noexcept { \
570 new (weak) std::weak_ptr<CXX_TYPE>(shared); \
571 } \
David Tolnay7a487852020-12-28 18:02:25 -0800572 void cxxbridge1$std$weak_ptr$##RUST_TYPE##$upgrade( \
573 const std::weak_ptr<CXX_TYPE> &weak, \
574 std::shared_ptr<CXX_TYPE> *shared) noexcept { \
575 new (shared) std::shared_ptr<CXX_TYPE>(weak.lock()); \
576 } \
David Tolnay215e77f2020-12-28 17:09:48 -0800577 void cxxbridge1$std$weak_ptr$##RUST_TYPE##$drop( \
578 const std::weak_ptr<CXX_TYPE> *self) noexcept { \
579 self->~weak_ptr(); \
David Tolnay5b163402020-12-10 19:26:02 -0800580 }
581
David Tolnay6787be62020-04-25 11:01:02 -0700582// Usize and isize are the same type as one of the below.
David Tolnayf336b3b2020-04-30 08:45:54 -0700583#define FOR_EACH_NUMERIC(MACRO) \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800584 MACRO(u8, std::uint8_t) \
585 MACRO(u16, std::uint16_t) \
586 MACRO(u32, std::uint32_t) \
587 MACRO(u64, std::uint64_t) \
588 MACRO(i8, std::int8_t) \
589 MACRO(i16, std::int16_t) \
590 MACRO(i32, std::int32_t) \
591 MACRO(i64, std::int64_t) \
David Tolnay6787be62020-04-25 11:01:02 -0700592 MACRO(f32, float) \
593 MACRO(f64, double)
594
David Tolnayf336b3b2020-04-30 08:45:54 -0700595#define FOR_EACH_STD_VECTOR(MACRO) \
596 FOR_EACH_NUMERIC(MACRO) \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800597 MACRO(usize, std::size_t) \
David Tolnay47e239d2020-08-28 00:32:04 -0700598 MACRO(isize, rust::isize) \
599 MACRO(string, std::string)
David Tolnay6787be62020-04-25 11:01:02 -0700600
David Tolnayf336b3b2020-04-30 08:45:54 -0700601#define FOR_EACH_RUST_VEC(MACRO) \
602 FOR_EACH_NUMERIC(MACRO) \
David Tolnay33f56ad2020-08-27 17:06:35 -0700603 MACRO(bool, bool) \
David Tolnay93e71d02020-11-25 20:16:52 -0800604 MACRO(char, char) \
David Tolnay33f56ad2020-08-27 17:06:35 -0700605 MACRO(string, rust::String)
David Tolnayf336b3b2020-04-30 08:45:54 -0700606
David Tolnay5b163402020-12-10 19:26:02 -0800607#define FOR_EACH_SHARED_PTR(MACRO) \
608 FOR_EACH_NUMERIC(MACRO) \
David Tolnaycd1430c2020-12-28 17:17:14 -0800609 MACRO(bool, bool) \
David Tolnaybe3cbf72020-12-12 22:12:07 -0800610 MACRO(usize, std::size_t) \
David Tolnay5b163402020-12-10 19:26:02 -0800611 MACRO(isize, rust::isize) \
612 MACRO(string, std::string)
613
David Tolnay4e7e7c42020-04-24 14:48:07 -0700614extern "C" {
David Tolnayf336b3b2020-04-30 08:45:54 -0700615FOR_EACH_STD_VECTOR(STD_VECTOR_OPS)
616FOR_EACH_RUST_VEC(RUST_VEC_EXTERNS)
David Tolnay5b163402020-12-10 19:26:02 -0800617FOR_EACH_SHARED_PTR(SHARED_PTR_OPS)
David Tolnay4e7e7c42020-04-24 14:48:07 -0700618} // extern "C"
David Tolnay6787be62020-04-25 11:01:02 -0700619
David Tolnay1768d8f2020-04-25 18:15:11 -0700620namespace rust {
David Tolnay0f0162f2020-11-16 23:43:37 -0800621inline namespace cxxbridge1 {
David Tolnayf336b3b2020-04-30 08:45:54 -0700622FOR_EACH_RUST_VEC(RUST_VEC_OPS)
David Tolnay0f0162f2020-11-16 23:43:37 -0800623} // namespace cxxbridge1
David Tolnay1768d8f2020-04-25 18:15:11 -0700624} // namespace rust