1#pragma once 2#include <algorithm> 3#include <array> 4#include <cassert> 5#include <cstddef> 6#include <cstdint> 7#include <exception> 8#include <initializer_list> 9#include <iosfwd> 10#include <iterator> 11#include <new> 12#include <stdexcept> 13#include <string> 14#include <type_traits> 15#include <utility> 16#include <vector> 17#if defined(_WIN32) 18#include <basetsd.h> 19#else 20#include <sys/types.h> 21#endif 22 23namespace rust { 24inline namespace cxxbridge1 { 25 26struct unsafe_bitcopy_t; 27 28namespace { 29template <typename T> 30class impl; 31} 32 33#ifndef CXXBRIDGE1_RUST_STRING 34#define CXXBRIDGE1_RUST_STRING 35// https://cxx.rs/binding/string.html 36class String final { 37public: 38 String() noexcept; 39 String(const String &) noexcept; 40 String(String &&) noexcept; 41 ~String() noexcept; 42 43 String(const std::string &); 44 String(const char *); 45 String(const char *, std::size_t); 46 String(const char16_t *); 47 String(const char16_t *, std::size_t); 48 49 // Replace invalid Unicode data with the replacement character (U+FFFD). 50 static String lossy(const std::string &) noexcept; 51 static String lossy(const char *) noexcept; 52 static String lossy(const char *, std::size_t) noexcept; 53 static String lossy(const char16_t *) noexcept; 54 static String lossy(const char16_t *, std::size_t) noexcept; 55 56 String &operator=(const String &) &noexcept; 57 String &operator=(String &&) &noexcept; 58 59 explicit operator std::string() const; 60 61 // Note: no null terminator. 62 const char *data() const noexcept; 63 std::size_t size() const noexcept; 64 std::size_t length() const noexcept; 65 bool empty() const noexcept; 66 67 const char *c_str() noexcept; 68 69 std::size_t capacity() const noexcept; 70 void reserve(size_t new_cap) noexcept; 71 72 using iterator = char *; 73 iterator begin() noexcept; 74 iterator end() noexcept; 75 76 using const_iterator = const char *; 77 const_iterator begin() const noexcept; 78 const_iterator end() const noexcept; 79 const_iterator cbegin() const noexcept; 80 const_iterator cend() const noexcept; 81 82 bool operator==(const String &) const noexcept; 83 bool operator!=(const String &) const noexcept; 84 bool operator<(const String &) const noexcept; 85 bool operator<=(const String &) const noexcept; 86 bool operator>(const String &) const noexcept; 87 bool operator>=(const String &) const noexcept; 88 89 void swap(String &) noexcept; 90 91 // Internal API only intended for the cxxbridge code generator. 92 String(unsafe_bitcopy_t, const String &) noexcept; 93 94private: 95 struct lossy_t; 96 String(lossy_t, const char *, std::size_t) noexcept; 97 String(lossy_t, const char16_t *, std::size_t) noexcept; 98 friend void swap(String &lhs, String &rhs) noexcept { lhs.swap(rhs); } 99 100 // Size and alignment statically verified by rust_string.rs. 101 std::array<std::uintptr_t, 3> repr; 102}; 103#endif // CXXBRIDGE1_RUST_STRING 104 105#ifndef CXXBRIDGE1_RUST_STR 106#define CXXBRIDGE1_RUST_STR 107// https://cxx.rs/binding/str.html 108class Str final { 109public: 110 Str() noexcept; 111 Str(const String &) noexcept; 112 Str(const std::string &); 113 Str(const char *); 114 Str(const char *, std::size_t); 115 116 Str &operator=(const Str &) &noexcept = default; 117 118 explicit operator std::string() const; 119 120 // Note: no null terminator. 121 const char *data() const noexcept; 122 std::size_t size() const noexcept; 123 std::size_t length() const noexcept; 124 bool empty() const noexcept; 125 126 // Important in order for System V ABI to pass in registers. 127 Str(const Str &) noexcept = default; 128 ~Str() noexcept = default; 129 130 using iterator = const char *; 131 using const_iterator = const char *; 132 const_iterator begin() const noexcept; 133 const_iterator end() const noexcept; 134 const_iterator cbegin() const noexcept; 135 const_iterator cend() const noexcept; 136 137 bool operator==(const Str &) const noexcept; 138 bool operator!=(const Str &) const noexcept; 139 bool operator<(const Str &) const noexcept; 140 bool operator<=(const Str &) const noexcept; 141 bool operator>(const Str &) const noexcept; 142 bool operator>=(const Str &) const noexcept; 143 144 void swap(Str &) noexcept; 145 146private: 147 class uninit; 148 Str(uninit) noexcept; 149 friend impl<Str>; 150 151 std::array<std::uintptr_t, 2> repr; 152}; 153#endif // CXXBRIDGE1_RUST_STR 154 155#ifndef CXXBRIDGE1_RUST_SLICE 156namespace detail { 157template <bool> 158struct copy_assignable_if {}; 159 160template <> 161struct copy_assignable_if<false> { 162 copy_assignable_if() noexcept = default; 163 copy_assignable_if(const copy_assignable_if &) noexcept = default; 164 copy_assignable_if &operator=(const copy_assignable_if &) &noexcept = delete; 165 copy_assignable_if &operator=(copy_assignable_if &&) &noexcept = default; 166}; 167} // namespace detail 168 169// https://cxx.rs/binding/slice.html 170template <typename T> 171class Slice final 172 : private detail::copy_assignable_if<std::is_const<T>::value> { 173public: 174 using value_type = T; 175 176 Slice() noexcept; 177 Slice(T *, std::size_t count) noexcept; 178 179 Slice &operator=(const Slice<T> &) &noexcept = default; 180 Slice &operator=(Slice<T> &&) &noexcept = default; 181 182 T *data() const noexcept; 183 std::size_t size() const noexcept; 184 std::size_t length() const noexcept; 185 bool empty() const noexcept; 186 187 T &operator[](std::size_t n) const noexcept; 188 T &at(std::size_t n) const; 189 T &front() const noexcept; 190 T &back() const noexcept; 191 192 // Important in order for System V ABI to pass in registers. 193 Slice(const Slice<T> &) noexcept = default; 194 ~Slice() noexcept = default; 195 196 class iterator; 197 iterator begin() const noexcept; 198 iterator end() const noexcept; 199 200 void swap(Slice &) noexcept; 201 202private: 203 class uninit; 204 Slice(uninit) noexcept; 205 friend impl<Slice>; 206 friend void sliceInit(void *, const void *, std::size_t) noexcept; 207 friend void *slicePtr(const void *) noexcept; 208 friend std::size_t sliceLen(const void *) noexcept; 209 210 std::array<std::uintptr_t, 2> repr; 211}; 212 213template <typename T> 214class Slice<T>::iterator final { 215public: 216 using iterator_category = std::random_access_iterator_tag; 217 using value_type = T; 218 using difference_type = std::ptrdiff_t; 219 using pointer = typename std::add_pointer<T>::type; 220 using reference = typename std::add_lvalue_reference<T>::type; 221 222 reference operator*() const noexcept; 223 pointer operator->() const noexcept; 224 reference operator[](difference_type) const noexcept; 225 226 iterator &operator++() noexcept; 227 iterator operator++(int) noexcept; 228 iterator &operator--() noexcept; 229 iterator operator--(int) noexcept; 230 231 iterator &operator+=(difference_type) noexcept; 232 iterator &operator-=(difference_type) noexcept; 233 iterator operator+(difference_type) const noexcept; 234 iterator operator-(difference_type) const noexcept; 235 difference_type operator-(const iterator &) const noexcept; 236 237 bool operator==(const iterator &) const noexcept; 238 bool operator!=(const iterator &) const noexcept; 239 bool operator<(const iterator &) const noexcept; 240 bool operator<=(const iterator &) const noexcept; 241 bool operator>(const iterator &) const noexcept; 242 bool operator>=(const iterator &) const noexcept; 243 244private: 245 friend class Slice; 246 void *pos; 247 std::size_t stride; 248}; 249#endif // CXXBRIDGE1_RUST_SLICE 250 251#ifndef CXXBRIDGE1_RUST_BOX 252// https://cxx.rs/binding/box.html 253template <typename T> 254class Box final { 255public: 256 using element_type = T; 257 using const_pointer = 258 typename std::add_pointer<typename std::add_const<T>::type>::type; 259 using pointer = typename std::add_pointer<T>::type; 260 261 Box() = delete; 262 Box(Box &&) noexcept; 263 ~Box() noexcept; 264 265 explicit Box(const T &); 266 explicit Box(T &&); 267 268 Box &operator=(Box &&) &noexcept; 269 270 const T *operator->() const noexcept; 271 const T &operator*() const noexcept; 272 T *operator->() noexcept; 273 T &operator*() noexcept; 274 275 template <typename... Fields> 276 static Box in_place(Fields &&...); 277 278 void swap(Box &) noexcept; 279 280 // Important: requires that `raw` came from an into_raw call. Do not pass a 281 // pointer from `new` or any other source. 282 static Box from_raw(T *) noexcept; 283 284 T *into_raw() noexcept; 285 286 /* Deprecated */ using value_type = element_type; 287 288private: 289 class uninit; 290 class allocation; 291 Box(uninit) noexcept; 292 void drop() noexcept; 293 294 friend void swap(Box &lhs, Box &rhs) noexcept { lhs.swap(rhs); } 295 296 T *ptr; 297}; 298#endif // CXXBRIDGE1_RUST_BOX 299 300#ifndef CXXBRIDGE1_RUST_VEC 301// https://cxx.rs/binding/vec.html 302template <typename T> 303class Vec final { 304public: 305 using value_type = T; 306 307 Vec() noexcept; 308 Vec(std::initializer_list<T>); 309 Vec(const Vec &); 310 Vec(Vec &&) noexcept; 311 ~Vec() noexcept; 312 313 Vec &operator=(Vec &&) &noexcept; 314 Vec &operator=(const Vec &) &; 315 316 std::size_t size() const noexcept; 317 bool empty() const noexcept; 318 const T *data() const noexcept; 319 T *data() noexcept; 320 std::size_t capacity() const noexcept; 321 322 const T &operator[](std::size_t n) const noexcept; 323 const T &at(std::size_t n) const; 324 const T &front() const noexcept; 325 const T &back() const noexcept; 326 327 T &operator[](std::size_t n) noexcept; 328 T &at(std::size_t n); 329 T &front() noexcept; 330 T &back() noexcept; 331 332 void reserve(std::size_t new_cap); 333 void push_back(const T &value); 334 void push_back(T &&value); 335 template <typename... Args> 336 void emplace_back(Args &&...args); 337 void truncate(std::size_t len); 338 void clear(); 339 340 using iterator = typename Slice<T>::iterator; 341 iterator begin() noexcept; 342 iterator end() noexcept; 343 344 using const_iterator = typename Slice<const T>::iterator; 345 const_iterator begin() const noexcept; 346 const_iterator end() const noexcept; 347 const_iterator cbegin() const noexcept; 348 const_iterator cend() const noexcept; 349 350 void swap(Vec &) noexcept; 351 352 // Internal API only intended for the cxxbridge code generator. 353 Vec(unsafe_bitcopy_t, const Vec &) noexcept; 354 355private: 356 void reserve_total(std::size_t new_cap) noexcept; 357 void set_len(std::size_t len) noexcept; 358 void drop() noexcept; 359 360 friend void swap(Vec &lhs, Vec &rhs) noexcept { lhs.swap(rhs); } 361 362 // Size and alignment statically verified by rust_vec.rs. 363 std::array<std::uintptr_t, 3> repr; 364}; 365#endif // CXXBRIDGE1_RUST_VEC 366 367#ifndef CXXBRIDGE1_RUST_FN 368// https://cxx.rs/binding/fn.html 369template <typename Signature> 370class Fn; 371 372template <typename Ret, typename... Args> 373class Fn<Ret(Args...)> final { 374public: 375 Ret operator()(Args... args) const noexcept; 376 Fn operator*() const noexcept; 377 378private: 379 Ret (*trampoline)(Args..., void *fn) noexcept; 380 void *fn; 381}; 382#endif // CXXBRIDGE1_RUST_FN 383 384#ifndef CXXBRIDGE1_RUST_ERROR 385#define CXXBRIDGE1_RUST_ERROR 386// https://cxx.rs/binding/result.html 387class Error final : public std::exception { 388public: 389 Error(const Error &); 390 Error(Error &&) noexcept; 391 ~Error() noexcept override; 392 393 Error &operator=(const Error &) &; 394 Error &operator=(Error &&) &noexcept; 395 396 const char *what() const noexcept override; 397 398private: 399 Error() noexcept = default; 400 friend impl<Error>; 401 const char *msg; 402 std::size_t len; 403}; 404#endif // CXXBRIDGE1_RUST_ERROR 405 406#ifndef CXXBRIDGE1_RUST_ISIZE 407#define CXXBRIDGE1_RUST_ISIZE 408#if defined(_WIN32) 409using isize = SSIZE_T; 410#else 411using isize = ssize_t; 412#endif 413#endif // CXXBRIDGE1_RUST_ISIZE 414 415std::ostream &operator<<(std::ostream &, const String &); 416std::ostream &operator<<(std::ostream &, const Str &); 417 418#ifndef CXXBRIDGE1_RUST_OPAQUE 419#define CXXBRIDGE1_RUST_OPAQUE 420// Base class of generated opaque Rust types. 421class Opaque { 422public: 423 Opaque() = delete; 424 Opaque(const Opaque &) = delete; 425 ~Opaque() = delete; 426}; 427#endif // CXXBRIDGE1_RUST_OPAQUE 428 429template <typename T> 430std::size_t size_of(); 431template <typename T> 432std::size_t align_of(); 433 434// IsRelocatable<T> is used in assertions that a C++ type passed by value 435// between Rust and C++ is soundly relocatable by Rust. 436// 437// There may be legitimate reasons to opt out of the check for support of types 438// that the programmer knows are soundly Rust-movable despite not being 439// recognized as such by the C++ type system due to a move constructor or 440// destructor. To opt out of the relocatability check, do either of the 441// following things in any header used by `include!` in the bridge. 442// 443// --- if you define the type: 444// struct MyType { 445// ... 446// + using IsRelocatable = std::true_type; 447// }; 448// 449// --- otherwise: 450// + template <> 451// + struct rust::IsRelocatable<MyType> : std::true_type {}; 452template <typename T> 453struct IsRelocatable; 454 455using u8 = std::uint8_t; 456using u16 = std::uint16_t; 457using u32 = std::uint32_t; 458using u64 = std::uint64_t; 459using usize = std::size_t; // see static asserts in cxx.cc 460using i8 = std::int8_t; 461using i16 = std::int16_t; 462using i32 = std::int32_t; 463using i64 = std::int64_t; 464using f32 = float; 465using f64 = double; 466 467// Snake case aliases for use in code that uses this style for type names. 468using string = String; 469using str = Str; 470template <typename T> 471using slice = Slice<T>; 472template <typename T> 473using box = Box<T>; 474template <typename T> 475using vec = Vec<T>; 476using error = Error; 477template <typename Signature> 478using fn = Fn<Signature>; 479template <typename T> 480using is_relocatable = IsRelocatable<T>; 481 482 483 484//////////////////////////////////////////////////////////////////////////////// 485/// end public API, begin implementation details 486 487#ifndef CXXBRIDGE1_PANIC 488#define CXXBRIDGE1_PANIC 489template <typename Exception> 490void panic [[noreturn]] (const char *msg); 491#endif // CXXBRIDGE1_PANIC 492 493#ifndef CXXBRIDGE1_RUST_FN 494#define CXXBRIDGE1_RUST_FN 495template <typename Ret, typename... Args> 496Ret Fn<Ret(Args...)>::operator()(Args... args) const noexcept { 497 return (*this->trampoline)(std::forward<Args>(args)..., this->fn); 498} 499 500template <typename Ret, typename... Args> 501Fn<Ret(Args...)> Fn<Ret(Args...)>::operator*() const noexcept { 502 return *this; 503} 504#endif // CXXBRIDGE1_RUST_FN 505 506#ifndef CXXBRIDGE1_RUST_BITCOPY_T 507#define CXXBRIDGE1_RUST_BITCOPY_T 508struct unsafe_bitcopy_t final { 509 explicit unsafe_bitcopy_t() = default; 510}; 511#endif // CXXBRIDGE1_RUST_BITCOPY_T 512 513#ifndef CXXBRIDGE1_RUST_BITCOPY 514#define CXXBRIDGE1_RUST_BITCOPY 515constexpr unsafe_bitcopy_t unsafe_bitcopy{}; 516#endif // CXXBRIDGE1_RUST_BITCOPY 517 518#ifndef CXXBRIDGE1_RUST_SLICE 519#define CXXBRIDGE1_RUST_SLICE 520template <typename T> 521Slice<T>::Slice() noexcept { 522 sliceInit(this, reinterpret_cast<void *>(align_of<T>()), 0); 523} 524 525template <typename T> 526Slice<T>::Slice(T *s, std::size_t count) noexcept { 527 assert(s != nullptr || count == 0); 528 sliceInit(this, 529 s == nullptr && count == 0 530 ? reinterpret_cast<void *>(align_of<T>()) 531 : const_cast<typename std::remove_const<T>::type *>(s), 532 count); 533} 534 535template <typename T> 536T *Slice<T>::data() const noexcept { 537 return reinterpret_cast<T *>(slicePtr(this)); 538} 539 540template <typename T> 541std::size_t Slice<T>::size() const noexcept { 542 return sliceLen(this); 543} 544 545template <typename T> 546std::size_t Slice<T>::length() const noexcept { 547 return this->size(); 548} 549 550template <typename T> 551bool Slice<T>::empty() const noexcept { 552 return this->size() == 0; 553} 554 555template <typename T> 556T &Slice<T>::operator[](std::size_t n) const noexcept { 557 assert(n < this->size()); 558 auto ptr = static_cast<char *>(slicePtr(this)) + size_of<T>() * n; 559 return *reinterpret_cast<T *>(ptr); 560} 561 562template <typename T> 563T &Slice<T>::at(std::size_t n) const { 564 if (n >= this->size()) { 565 panic<std::out_of_range>("rust::Slice index out of range"); 566 } 567 return (*this)[n]; 568} 569 570template <typename T> 571T &Slice<T>::front() const noexcept { 572 assert(!this->empty()); 573 return (*this)[0]; 574} 575 576template <typename T> 577T &Slice<T>::back() const noexcept { 578 assert(!this->empty()); 579 return (*this)[this->size() - 1]; 580} 581 582template <typename T> 583typename Slice<T>::iterator::reference 584Slice<T>::iterator::operator*() const noexcept { 585 return *static_cast<T *>(this->pos); 586} 587 588template <typename T> 589typename Slice<T>::iterator::pointer 590Slice<T>::iterator::operator->() const noexcept { 591 return static_cast<T *>(this->pos); 592} 593 594template <typename T> 595typename Slice<T>::iterator::reference Slice<T>::iterator::operator[]( 596 typename Slice<T>::iterator::difference_type n) const noexcept { 597 auto ptr = static_cast<char *>(this->pos) + this->stride * n; 598 return *reinterpret_cast<T *>(ptr); 599} 600 601template <typename T> 602typename Slice<T>::iterator &Slice<T>::iterator::operator++() noexcept { 603 this->pos = static_cast<char *>(this->pos) + this->stride; 604 return *this; 605} 606 607template <typename T> 608typename Slice<T>::iterator Slice<T>::iterator::operator++(int) noexcept { 609 auto ret = iterator(*this); 610 this->pos = static_cast<char *>(this->pos) + this->stride; 611 return ret; 612} 613 614template <typename T> 615typename Slice<T>::iterator &Slice<T>::iterator::operator--() noexcept { 616 this->pos = static_cast<char *>(this->pos) - this->stride; 617 return *this; 618} 619 620template <typename T> 621typename Slice<T>::iterator Slice<T>::iterator::operator--(int) noexcept { 622 auto ret = iterator(*this); 623 this->pos = static_cast<char *>(this->pos) - this->stride; 624 return ret; 625} 626 627template <typename T> 628typename Slice<T>::iterator &Slice<T>::iterator::operator+=( 629 typename Slice<T>::iterator::difference_type n) noexcept { 630 this->pos = static_cast<char *>(this->pos) + this->stride * n; 631 return *this; 632} 633 634template <typename T> 635typename Slice<T>::iterator &Slice<T>::iterator::operator-=( 636 typename Slice<T>::iterator::difference_type n) noexcept { 637 this->pos = static_cast<char *>(this->pos) - this->stride * n; 638 return *this; 639} 640 641template <typename T> 642typename Slice<T>::iterator Slice<T>::iterator::operator+( 643 typename Slice<T>::iterator::difference_type n) const noexcept { 644 auto ret = iterator(*this); 645 ret.pos = static_cast<char *>(this->pos) + this->stride * n; 646 return ret; 647} 648 649template <typename T> 650typename Slice<T>::iterator Slice<T>::iterator::operator-( 651 typename Slice<T>::iterator::difference_type n) const noexcept { 652 auto ret = iterator(*this); 653 ret.pos = static_cast<char *>(this->pos) - this->stride * n; 654 return ret; 655} 656 657template <typename T> 658typename Slice<T>::iterator::difference_type 659Slice<T>::iterator::operator-(const iterator &other) const noexcept { 660 auto diff = std::distance(static_cast<char *>(other.pos), 661 static_cast<char *>(this->pos)); 662 return diff / static_cast<typename Slice<T>::iterator::difference_type>(this->stride); 663} 664 665template <typename T> 666bool Slice<T>::iterator::operator==(const iterator &other) const noexcept { 667 return this->pos == other.pos; 668} 669 670template <typename T> 671bool Slice<T>::iterator::operator!=(const iterator &other) const noexcept { 672 return this->pos != other.pos; 673} 674 675template <typename T> 676bool Slice<T>::iterator::operator<(const iterator &other) const noexcept { 677 return this->pos < other.pos; 678} 679 680template <typename T> 681bool Slice<T>::iterator::operator<=(const iterator &other) const noexcept { 682 return this->pos <= other.pos; 683} 684 685template <typename T> 686bool Slice<T>::iterator::operator>(const iterator &other) const noexcept { 687 return this->pos > other.pos; 688} 689 690template <typename T> 691bool Slice<T>::iterator::operator>=(const iterator &other) const noexcept { 692 return this->pos >= other.pos; 693} 694 695template <typename T> 696typename Slice<T>::iterator Slice<T>::begin() const noexcept { 697 iterator it; 698 it.pos = slicePtr(this); 699 it.stride = size_of<T>(); 700 return it; 701} 702 703template <typename T> 704typename Slice<T>::iterator Slice<T>::end() const noexcept { 705 iterator it = this->begin(); 706 it.pos = static_cast<char *>(it.pos) + it.stride * this->size(); 707 return it; 708} 709 710template <typename T> 711void Slice<T>::swap(Slice &rhs) noexcept { 712 std::swap(*this, rhs); 713} 714#endif // CXXBRIDGE1_RUST_SLICE 715 716#ifndef CXXBRIDGE1_RUST_BOX 717#define CXXBRIDGE1_RUST_BOX 718template <typename T> 719class Box<T>::uninit {}; 720 721template <typename T> 722class Box<T>::allocation { 723 static T *alloc() noexcept; 724 static void dealloc(T *) noexcept; 725 726public: 727 allocation() noexcept : ptr(alloc()) {} 728 ~allocation() noexcept { 729 if (this->ptr) { 730 dealloc(this->ptr); 731 } 732 } 733 T *ptr; 734}; 735 736template <typename T> 737Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) { 738 other.ptr = nullptr; 739} 740 741template <typename T> 742Box<T>::Box(const T &val) { 743 allocation alloc; 744 ::new (alloc.ptr) T(val); 745 this->ptr = alloc.ptr; 746 alloc.ptr = nullptr; 747} 748 749template <typename T> 750Box<T>::Box(T &&val) { 751 allocation alloc; 752 ::new (alloc.ptr) T(std::move(val)); 753 this->ptr = alloc.ptr; 754 alloc.ptr = nullptr; 755} 756 757template <typename T> 758Box<T>::~Box() noexcept { 759 if (this->ptr) { 760 this->drop(); 761 } 762} 763 764template <typename T> 765Box<T> &Box<T>::operator=(Box &&other) &noexcept { 766 if (this->ptr) { 767 this->drop(); 768 } 769 this->ptr = other.ptr; 770 other.ptr = nullptr; 771 return *this; 772} 773 774template <typename T> 775const T *Box<T>::operator->() const noexcept { 776 return this->ptr; 777} 778 779template <typename T> 780const T &Box<T>::operator*() const noexcept { 781 return *this->ptr; 782} 783 784template <typename T> 785T *Box<T>::operator->() noexcept { 786 return this->ptr; 787} 788 789template <typename T> 790T &Box<T>::operator*() noexcept { 791 return *this->ptr; 792} 793 794template <typename T> 795template <typename... Fields> 796Box<T> Box<T>::in_place(Fields &&...fields) { 797 allocation alloc; 798 auto ptr = alloc.ptr; 799 ::new (ptr) T{std::forward<Fields>(fields)...}; 800 alloc.ptr = nullptr; 801 return from_raw(ptr); 802} 803 804template <typename T> 805void Box<T>::swap(Box &rhs) noexcept { 806 using std::swap; 807 swap(this->ptr, rhs.ptr); 808} 809 810template <typename T> 811Box<T> Box<T>::from_raw(T *raw) noexcept { 812 Box box = uninit{}; 813 box.ptr = raw; 814 return box; 815} 816 817template <typename T> 818T *Box<T>::into_raw() noexcept { 819 T *raw = this->ptr; 820 this->ptr = nullptr; 821 return raw; 822} 823 824template <typename T> 825Box<T>::Box(uninit) noexcept {} 826#endif // CXXBRIDGE1_RUST_BOX 827 828#ifndef CXXBRIDGE1_RUST_VEC 829#define CXXBRIDGE1_RUST_VEC 830template <typename T> 831Vec<T>::Vec(std::initializer_list<T> init) : Vec{} { 832 this->reserve_total(init.size()); 833 std::move(init.begin(), init.end(), std::back_inserter(*this)); 834} 835 836template <typename T> 837Vec<T>::Vec(const Vec &other) : Vec() { 838 this->reserve_total(other.size()); 839 std::copy(other.begin(), other.end(), std::back_inserter(*this)); 840} 841 842template <typename T> 843Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) { 844 new (&other) Vec(); 845} 846 847template <typename T> 848Vec<T>::~Vec() noexcept { 849 this->drop(); 850} 851 852template <typename T> 853Vec<T> &Vec<T>::operator=(Vec &&other) &noexcept { 854 this->drop(); 855 this->repr = other.repr; 856 new (&other) Vec(); 857 return *this; 858} 859 860template <typename T> 861Vec<T> &Vec<T>::operator=(const Vec &other) & { 862 if (this != &other) { 863 this->drop(); 864 new (this) Vec(other); 865 } 866 return *this; 867} 868 869template <typename T> 870bool Vec<T>::empty() const noexcept { 871 return this->size() == 0; 872} 873 874template <typename T> 875T *Vec<T>::data() noexcept { 876 return const_cast<T *>(const_cast<const Vec<T> *>(this)->data()); 877} 878 879template <typename T> 880const T &Vec<T>::operator[](std::size_t n) const noexcept { 881 assert(n < this->size()); 882 auto data = reinterpret_cast<const char *>(this->data()); 883 return *reinterpret_cast<const T *>(data + n * size_of<T>()); 884} 885 886template <typename T> 887const T &Vec<T>::at(std::size_t n) const { 888 if (n >= this->size()) { 889 panic<std::out_of_range>("rust::Vec index out of range"); 890 } 891 return (*this)[n]; 892} 893 894template <typename T> 895const T &Vec<T>::front() const noexcept { 896 assert(!this->empty()); 897 return (*this)[0]; 898} 899 900template <typename T> 901const T &Vec<T>::back() const noexcept { 902 assert(!this->empty()); 903 return (*this)[this->size() - 1]; 904} 905 906template <typename T> 907T &Vec<T>::operator[](std::size_t n) noexcept { 908 assert(n < this->size()); 909 auto data = reinterpret_cast<char *>(this->data()); 910 return *reinterpret_cast<T *>(data + n * size_of<T>()); 911} 912 913template <typename T> 914T &Vec<T>::at(std::size_t n) { 915 if (n >= this->size()) { 916 panic<std::out_of_range>("rust::Vec index out of range"); 917 } 918 return (*this)[n]; 919} 920 921template <typename T> 922T &Vec<T>::front() noexcept { 923 assert(!this->empty()); 924 return (*this)[0]; 925} 926 927template <typename T> 928T &Vec<T>::back() noexcept { 929 assert(!this->empty()); 930 return (*this)[this->size() - 1]; 931} 932 933template <typename T> 934void Vec<T>::reserve(std::size_t new_cap) { 935 this->reserve_total(new_cap); 936} 937 938template <typename T> 939void Vec<T>::push_back(const T &value) { 940 this->emplace_back(value); 941} 942 943template <typename T> 944void Vec<T>::push_back(T &&value) { 945 this->emplace_back(std::move(value)); 946} 947 948template <typename T> 949template <typename... Args> 950void Vec<T>::emplace_back(Args &&...args) { 951 auto size = this->size(); 952 this->reserve_total(size + 1); 953 ::new (reinterpret_cast<T *>(reinterpret_cast<char *>(this->data()) + 954 size * size_of<T>())) 955 T(std::forward<Args>(args)...); 956 this->set_len(size + 1); 957} 958 959template <typename T> 960void Vec<T>::clear() { 961 this->truncate(0); 962} 963 964template <typename T> 965typename Vec<T>::iterator Vec<T>::begin() noexcept { 966 return Slice<T>(this->data(), this->size()).begin(); 967} 968 969template <typename T> 970typename Vec<T>::iterator Vec<T>::end() noexcept { 971 return Slice<T>(this->data(), this->size()).end(); 972} 973 974template <typename T> 975typename Vec<T>::const_iterator Vec<T>::begin() const noexcept { 976 return this->cbegin(); 977} 978 979template <typename T> 980typename Vec<T>::const_iterator Vec<T>::end() const noexcept { 981 return this->cend(); 982} 983 984template <typename T> 985typename Vec<T>::const_iterator Vec<T>::cbegin() const noexcept { 986 return Slice<const T>(this->data(), this->size()).begin(); 987} 988 989template <typename T> 990typename Vec<T>::const_iterator Vec<T>::cend() const noexcept { 991 return Slice<const T>(this->data(), this->size()).end(); 992} 993 994template <typename T> 995void Vec<T>::swap(Vec &rhs) noexcept { 996 using std::swap; 997 swap(this->repr, rhs.repr); 998} 999 1000// Internal API only intended for the cxxbridge code generator. 1001template <typename T> 1002Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {} 1003#endif // CXXBRIDGE1_RUST_VEC 1004 1005#ifndef CXXBRIDGE1_IS_COMPLETE 1006#define CXXBRIDGE1_IS_COMPLETE 1007namespace detail { 1008namespace { 1009template <typename T, typename = std::size_t> 1010struct is_complete : std::false_type {}; 1011template <typename T> 1012struct is_complete<T, decltype(sizeof(T))> : std::true_type {}; 1013} // namespace 1014} // namespace detail 1015#endif // CXXBRIDGE1_IS_COMPLETE 1016 1017#ifndef CXXBRIDGE1_LAYOUT 1018#define CXXBRIDGE1_LAYOUT 1019class layout { 1020 template <typename T> 1021 friend std::size_t size_of(); 1022 template <typename T> 1023 friend std::size_t align_of(); 1024 template <typename T> 1025 static typename std::enable_if<std::is_base_of<Opaque, T>::value, 1026 std::size_t>::type 1027 do_size_of() { 1028 return T::layout::size(); 1029 } 1030 template <typename T> 1031 static typename std::enable_if<!std::is_base_of<Opaque, T>::value, 1032 std::size_t>::type 1033 do_size_of() { 1034 return sizeof(T); 1035 } 1036 template <typename T> 1037 static 1038 typename std::enable_if<detail::is_complete<T>::value, std::size_t>::type 1039 size_of() { 1040 return do_size_of<T>(); 1041 } 1042 template <typename T> 1043 static typename std::enable_if<std::is_base_of<Opaque, T>::value, 1044 std::size_t>::type 1045 do_align_of() { 1046 return T::layout::align(); 1047 } 1048 template <typename T> 1049 static typename std::enable_if<!std::is_base_of<Opaque, T>::value, 1050 std::size_t>::type 1051 do_align_of() { 1052 return alignof(T); 1053 } 1054 template <typename T> 1055 static 1056 typename std::enable_if<detail::is_complete<T>::value, std::size_t>::type 1057 align_of() { 1058 return do_align_of<T>(); 1059 } 1060}; 1061 1062template <typename T> 1063std::size_t size_of() { 1064 return layout::size_of<T>(); 1065} 1066 1067template <typename T> 1068std::size_t align_of() { 1069 return layout::align_of<T>(); 1070} 1071#endif // CXXBRIDGE1_LAYOUT 1072 1073#ifndef CXXBRIDGE1_RELOCATABLE 1074#define CXXBRIDGE1_RELOCATABLE 1075namespace detail { 1076template <typename... Ts> 1077struct make_void { 1078 using type = void; 1079}; 1080 1081template <typename... Ts> 1082using void_t = typename make_void<Ts...>::type; 1083 1084template <typename Void, template <typename...> class, typename...> 1085struct detect : std::false_type {}; 1086template <template <typename...> class T, typename... A> 1087struct detect<void_t<T<A...>>, T, A...> : std::true_type {}; 1088 1089template <template <typename...> class T, typename... A> 1090using is_detected = detect<void, T, A...>; 1091 1092template <typename T> 1093using detect_IsRelocatable = typename T::IsRelocatable; 1094 1095template <typename T> 1096struct get_IsRelocatable 1097 : std::is_same<typename T::IsRelocatable, std::true_type> {}; 1098} // namespace detail 1099 1100template <typename T> 1101struct IsRelocatable 1102 : std::conditional< 1103 detail::is_detected<detail::detect_IsRelocatable, T>::value, 1104 detail::get_IsRelocatable<T>, 1105 std::integral_constant< 1106 bool, std::is_trivially_move_constructible<T>::value && 1107 std::is_trivially_destructible<T>::value>>::type {}; 1108#endif // CXXBRIDGE1_RELOCATABLE 1109 1110} // namespace cxxbridge1 1111} // namespace rust 1112