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 
23 namespace rust {
24 inline namespace cxxbridge1 {
25 
26 struct unsafe_bitcopy_t;
27 
28 namespace {
29 template <typename T>
30 class impl;
31 }
32 
33 #ifndef CXXBRIDGE1_RUST_STRING
34 #define CXXBRIDGE1_RUST_STRING
35 // https://cxx.rs/binding/string.html
36 class String final {
37 public:
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 
94 private:
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;
swap(String & lhs,String & rhs)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
108 class Str final {
109 public:
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 
146 private:
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
156 namespace detail {
157 template <bool>
158 struct copy_assignable_if {};
159 
160 template <>
161 struct 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
170 template <typename T>
171 class Slice final
172     : private detail::copy_assignable_if<std::is_const<T>::value> {
173 public:
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 
202 private:
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 
213 template <typename T>
214 class Slice<T>::iterator final {
215 public:
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 
244 private:
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
253 template <typename T>
254 class Box final {
255 public:
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 
288 private:
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
302 template <typename T>
303 class Vec final {
304 public:
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 
355 private:
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
369 template <typename Signature>
370 class Fn;
371 
372 template <typename Ret, typename... Args>
373 class Fn<Ret(Args...)> final {
374 public:
375   Ret operator()(Args... args) const noexcept;
376   Fn operator*() const noexcept;
377 
378 private:
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
387 class Error final : public std::exception {
388 public:
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 
398 private:
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)
409 using isize = SSIZE_T;
410 #else
411 using isize = ssize_t;
412 #endif
413 #endif // CXXBRIDGE1_RUST_ISIZE
414 
415 std::ostream &operator<<(std::ostream &, const String &);
416 std::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.
421 class Opaque {
422 public:
423   Opaque() = delete;
424   Opaque(const Opaque &) = delete;
425   ~Opaque() = delete;
426 };
427 #endif // CXXBRIDGE1_RUST_OPAQUE
428 
429 template <typename T>
430 std::size_t size_of();
431 template <typename T>
432 std::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 {};
452 template <typename T>
453 struct IsRelocatable;
454 
455 using u8 = std::uint8_t;
456 using u16 = std::uint16_t;
457 using u32 = std::uint32_t;
458 using u64 = std::uint64_t;
459 using usize = std::size_t; // see static asserts in cxx.cc
460 using i8 = std::int8_t;
461 using i16 = std::int16_t;
462 using i32 = std::int32_t;
463 using i64 = std::int64_t;
464 using f32 = float;
465 using f64 = double;
466 
467 // Snake case aliases for use in code that uses this style for type names.
468 using string = String;
469 using str = Str;
470 template <typename T>
471 using slice = Slice<T>;
472 template <typename T>
473 using box = Box<T>;
474 template <typename T>
475 using vec = Vec<T>;
476 using error = Error;
477 template <typename Signature>
478 using fn = Fn<Signature>;
479 template <typename T>
480 using is_relocatable = IsRelocatable<T>;
481 
482 
483 
484 ////////////////////////////////////////////////////////////////////////////////
485 /// end public API, begin implementation details
486 
487 #ifndef CXXBRIDGE1_PANIC
488 #define CXXBRIDGE1_PANIC
489 template <typename Exception>
490 void panic [[noreturn]] (const char *msg);
491 #endif // CXXBRIDGE1_PANIC
492 
493 #ifndef CXXBRIDGE1_RUST_FN
494 #define CXXBRIDGE1_RUST_FN
495 template <typename Ret, typename... Args>
496 Ret Fn<Ret(Args...)>::operator()(Args... args) const noexcept {
497   return (*this->trampoline)(std::forward<Args>(args)..., this->fn);
498 }
499 
500 template <typename Ret, typename... Args>
501 Fn<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
508 struct 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
515 constexpr unsafe_bitcopy_t unsafe_bitcopy{};
516 #endif // CXXBRIDGE1_RUST_BITCOPY
517 
518 #ifndef CXXBRIDGE1_RUST_SLICE
519 #define CXXBRIDGE1_RUST_SLICE
520 template <typename T>
521 Slice<T>::Slice() noexcept {
522   sliceInit(this, reinterpret_cast<void *>(align_of<T>()), 0);
523 }
524 
525 template <typename T>
526 Slice<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 
535 template <typename T>
536 T *Slice<T>::data() const noexcept {
537   return reinterpret_cast<T *>(slicePtr(this));
538 }
539 
540 template <typename T>
541 std::size_t Slice<T>::size() const noexcept {
542   return sliceLen(this);
543 }
544 
545 template <typename T>
546 std::size_t Slice<T>::length() const noexcept {
547   return this->size();
548 }
549 
550 template <typename T>
551 bool Slice<T>::empty() const noexcept {
552   return this->size() == 0;
553 }
554 
555 template <typename T>
556 T &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 
562 template <typename T>
563 T &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 
570 template <typename T>
571 T &Slice<T>::front() const noexcept {
572   assert(!this->empty());
573   return (*this)[0];
574 }
575 
576 template <typename T>
577 T &Slice<T>::back() const noexcept {
578   assert(!this->empty());
579   return (*this)[this->size() - 1];
580 }
581 
582 template <typename T>
583 typename Slice<T>::iterator::reference
584 Slice<T>::iterator::operator*() const noexcept {
585   return *static_cast<T *>(this->pos);
586 }
587 
588 template <typename T>
589 typename Slice<T>::iterator::pointer
590 Slice<T>::iterator::operator->() const noexcept {
591   return static_cast<T *>(this->pos);
592 }
593 
594 template <typename T>
595 typename 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 
601 template <typename T>
602 typename Slice<T>::iterator &Slice<T>::iterator::operator++() noexcept {
603   this->pos = static_cast<char *>(this->pos) + this->stride;
604   return *this;
605 }
606 
607 template <typename T>
608 typename 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 
614 template <typename T>
615 typename Slice<T>::iterator &Slice<T>::iterator::operator--() noexcept {
616   this->pos = static_cast<char *>(this->pos) - this->stride;
617   return *this;
618 }
619 
620 template <typename T>
621 typename 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 
627 template <typename T>
628 typename 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 
634 template <typename T>
635 typename 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 
641 template <typename T>
642 typename 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 
649 template <typename T>
650 typename 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 
657 template <typename T>
658 typename Slice<T>::iterator::difference_type
659 Slice<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>(
663                     this->stride);
664 }
665 
666 template <typename T>
667 bool Slice<T>::iterator::operator==(const iterator &other) const noexcept {
668   return this->pos == other.pos;
669 }
670 
671 template <typename T>
672 bool Slice<T>::iterator::operator!=(const iterator &other) const noexcept {
673   return this->pos != other.pos;
674 }
675 
676 template <typename T>
677 bool Slice<T>::iterator::operator<(const iterator &other) const noexcept {
678   return this->pos < other.pos;
679 }
680 
681 template <typename T>
682 bool Slice<T>::iterator::operator<=(const iterator &other) const noexcept {
683   return this->pos <= other.pos;
684 }
685 
686 template <typename T>
687 bool Slice<T>::iterator::operator>(const iterator &other) const noexcept {
688   return this->pos > other.pos;
689 }
690 
691 template <typename T>
692 bool Slice<T>::iterator::operator>=(const iterator &other) const noexcept {
693   return this->pos >= other.pos;
694 }
695 
696 template <typename T>
697 typename Slice<T>::iterator Slice<T>::begin() const noexcept {
698   iterator it;
699   it.pos = slicePtr(this);
700   it.stride = size_of<T>();
701   return it;
702 }
703 
704 template <typename T>
705 typename Slice<T>::iterator Slice<T>::end() const noexcept {
706   iterator it = this->begin();
707   it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
708   return it;
709 }
710 
711 template <typename T>
712 void Slice<T>::swap(Slice &rhs) noexcept {
713   std::swap(*this, rhs);
714 }
715 #endif // CXXBRIDGE1_RUST_SLICE
716 
717 #ifndef CXXBRIDGE1_RUST_BOX
718 #define CXXBRIDGE1_RUST_BOX
719 template <typename T>
720 class Box<T>::uninit {};
721 
722 template <typename T>
723 class Box<T>::allocation {
724   static T *alloc() noexcept;
725   static void dealloc(T *) noexcept;
726 
727 public:
728   allocation() noexcept : ptr(alloc()) {}
729   ~allocation() noexcept {
730     if (this->ptr) {
731       dealloc(this->ptr);
732     }
733   }
734   T *ptr;
735 };
736 
737 template <typename T>
738 Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
739   other.ptr = nullptr;
740 }
741 
742 template <typename T>
743 Box<T>::Box(const T &val) {
744   allocation alloc;
745   ::new (alloc.ptr) T(val);
746   this->ptr = alloc.ptr;
747   alloc.ptr = nullptr;
748 }
749 
750 template <typename T>
751 Box<T>::Box(T &&val) {
752   allocation alloc;
753   ::new (alloc.ptr) T(std::move(val));
754   this->ptr = alloc.ptr;
755   alloc.ptr = nullptr;
756 }
757 
758 template <typename T>
759 Box<T>::~Box() noexcept {
760   if (this->ptr) {
761     this->drop();
762   }
763 }
764 
765 template <typename T>
766 Box<T> &Box<T>::operator=(Box &&other) &noexcept {
767   if (this->ptr) {
768     this->drop();
769   }
770   this->ptr = other.ptr;
771   other.ptr = nullptr;
772   return *this;
773 }
774 
775 template <typename T>
776 const T *Box<T>::operator->() const noexcept {
777   return this->ptr;
778 }
779 
780 template <typename T>
781 const T &Box<T>::operator*() const noexcept {
782   return *this->ptr;
783 }
784 
785 template <typename T>
786 T *Box<T>::operator->() noexcept {
787   return this->ptr;
788 }
789 
790 template <typename T>
791 T &Box<T>::operator*() noexcept {
792   return *this->ptr;
793 }
794 
795 template <typename T>
796 template <typename... Fields>
797 Box<T> Box<T>::in_place(Fields &&...fields) {
798   allocation alloc;
799   auto ptr = alloc.ptr;
800   ::new (ptr) T{std::forward<Fields>(fields)...};
801   alloc.ptr = nullptr;
802   return from_raw(ptr);
803 }
804 
805 template <typename T>
806 void Box<T>::swap(Box &rhs) noexcept {
807   using std::swap;
808   swap(this->ptr, rhs.ptr);
809 }
810 
811 template <typename T>
812 Box<T> Box<T>::from_raw(T *raw) noexcept {
813   Box box = uninit{};
814   box.ptr = raw;
815   return box;
816 }
817 
818 template <typename T>
819 T *Box<T>::into_raw() noexcept {
820   T *raw = this->ptr;
821   this->ptr = nullptr;
822   return raw;
823 }
824 
825 template <typename T>
826 Box<T>::Box(uninit) noexcept {}
827 #endif // CXXBRIDGE1_RUST_BOX
828 
829 #ifndef CXXBRIDGE1_RUST_VEC
830 #define CXXBRIDGE1_RUST_VEC
831 template <typename T>
832 Vec<T>::Vec(std::initializer_list<T> init) : Vec{} {
833   this->reserve_total(init.size());
834   std::move(init.begin(), init.end(), std::back_inserter(*this));
835 }
836 
837 template <typename T>
838 Vec<T>::Vec(const Vec &other) : Vec() {
839   this->reserve_total(other.size());
840   std::copy(other.begin(), other.end(), std::back_inserter(*this));
841 }
842 
843 template <typename T>
844 Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {
845   new (&other) Vec();
846 }
847 
848 template <typename T>
849 Vec<T>::~Vec() noexcept {
850   this->drop();
851 }
852 
853 template <typename T>
854 Vec<T> &Vec<T>::operator=(Vec &&other) &noexcept {
855   this->drop();
856   this->repr = other.repr;
857   new (&other) Vec();
858   return *this;
859 }
860 
861 template <typename T>
862 Vec<T> &Vec<T>::operator=(const Vec &other) & {
863   if (this != &other) {
864     this->drop();
865     new (this) Vec(other);
866   }
867   return *this;
868 }
869 
870 template <typename T>
871 bool Vec<T>::empty() const noexcept {
872   return this->size() == 0;
873 }
874 
875 template <typename T>
876 T *Vec<T>::data() noexcept {
877   return const_cast<T *>(const_cast<const Vec<T> *>(this)->data());
878 }
879 
880 template <typename T>
881 const T &Vec<T>::operator[](std::size_t n) const noexcept {
882   assert(n < this->size());
883   auto data = reinterpret_cast<const char *>(this->data());
884   return *reinterpret_cast<const T *>(data + n * size_of<T>());
885 }
886 
887 template <typename T>
888 const T &Vec<T>::at(std::size_t n) const {
889   if (n >= this->size()) {
890     panic<std::out_of_range>("rust::Vec index out of range");
891   }
892   return (*this)[n];
893 }
894 
895 template <typename T>
896 const T &Vec<T>::front() const noexcept {
897   assert(!this->empty());
898   return (*this)[0];
899 }
900 
901 template <typename T>
902 const T &Vec<T>::back() const noexcept {
903   assert(!this->empty());
904   return (*this)[this->size() - 1];
905 }
906 
907 template <typename T>
908 T &Vec<T>::operator[](std::size_t n) noexcept {
909   assert(n < this->size());
910   auto data = reinterpret_cast<char *>(this->data());
911   return *reinterpret_cast<T *>(data + n * size_of<T>());
912 }
913 
914 template <typename T>
915 T &Vec<T>::at(std::size_t n) {
916   if (n >= this->size()) {
917     panic<std::out_of_range>("rust::Vec index out of range");
918   }
919   return (*this)[n];
920 }
921 
922 template <typename T>
923 T &Vec<T>::front() noexcept {
924   assert(!this->empty());
925   return (*this)[0];
926 }
927 
928 template <typename T>
929 T &Vec<T>::back() noexcept {
930   assert(!this->empty());
931   return (*this)[this->size() - 1];
932 }
933 
934 template <typename T>
935 void Vec<T>::reserve(std::size_t new_cap) {
936   this->reserve_total(new_cap);
937 }
938 
939 template <typename T>
940 void Vec<T>::push_back(const T &value) {
941   this->emplace_back(value);
942 }
943 
944 template <typename T>
945 void Vec<T>::push_back(T &&value) {
946   this->emplace_back(std::move(value));
947 }
948 
949 template <typename T>
950 template <typename... Args>
951 void Vec<T>::emplace_back(Args &&...args) {
952   auto size = this->size();
953   this->reserve_total(size + 1);
954   ::new (reinterpret_cast<T *>(reinterpret_cast<char *>(this->data()) +
955                                size * size_of<T>()))
956       T(std::forward<Args>(args)...);
957   this->set_len(size + 1);
958 }
959 
960 template <typename T>
961 void Vec<T>::clear() {
962   this->truncate(0);
963 }
964 
965 template <typename T>
966 typename Vec<T>::iterator Vec<T>::begin() noexcept {
967   return Slice<T>(this->data(), this->size()).begin();
968 }
969 
970 template <typename T>
971 typename Vec<T>::iterator Vec<T>::end() noexcept {
972   return Slice<T>(this->data(), this->size()).end();
973 }
974 
975 template <typename T>
976 typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
977   return this->cbegin();
978 }
979 
980 template <typename T>
981 typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
982   return this->cend();
983 }
984 
985 template <typename T>
986 typename Vec<T>::const_iterator Vec<T>::cbegin() const noexcept {
987   return Slice<const T>(this->data(), this->size()).begin();
988 }
989 
990 template <typename T>
991 typename Vec<T>::const_iterator Vec<T>::cend() const noexcept {
992   return Slice<const T>(this->data(), this->size()).end();
993 }
994 
995 template <typename T>
996 void Vec<T>::swap(Vec &rhs) noexcept {
997   using std::swap;
998   swap(this->repr, rhs.repr);
999 }
1000 
1001 // Internal API only intended for the cxxbridge code generator.
1002 template <typename T>
1003 Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
1004 #endif // CXXBRIDGE1_RUST_VEC
1005 
1006 #ifndef CXXBRIDGE1_IS_COMPLETE
1007 #define CXXBRIDGE1_IS_COMPLETE
1008 namespace detail {
1009 namespace {
1010 template <typename T, typename = std::size_t>
1011 struct is_complete : std::false_type {};
1012 template <typename T>
1013 struct is_complete<T, decltype(sizeof(T))> : std::true_type {};
1014 } // namespace
1015 } // namespace detail
1016 #endif // CXXBRIDGE1_IS_COMPLETE
1017 
1018 #ifndef CXXBRIDGE1_LAYOUT
1019 #define CXXBRIDGE1_LAYOUT
1020 class layout {
1021   template <typename T>
1022   friend std::size_t size_of();
1023   template <typename T>
1024   friend std::size_t align_of();
1025   template <typename T>
1026   static typename std::enable_if<std::is_base_of<Opaque, T>::value,
1027                                  std::size_t>::type
1028   do_size_of() {
1029     return T::layout::size();
1030   }
1031   template <typename T>
1032   static typename std::enable_if<!std::is_base_of<Opaque, T>::value,
1033                                  std::size_t>::type
1034   do_size_of() {
1035     return sizeof(T);
1036   }
1037   template <typename T>
1038   static
1039       typename std::enable_if<detail::is_complete<T>::value, std::size_t>::type
1040       size_of() {
1041     return do_size_of<T>();
1042   }
1043   template <typename T>
1044   static typename std::enable_if<std::is_base_of<Opaque, T>::value,
1045                                  std::size_t>::type
1046   do_align_of() {
1047     return T::layout::align();
1048   }
1049   template <typename T>
1050   static typename std::enable_if<!std::is_base_of<Opaque, T>::value,
1051                                  std::size_t>::type
1052   do_align_of() {
1053     return alignof(T);
1054   }
1055   template <typename T>
1056   static
1057       typename std::enable_if<detail::is_complete<T>::value, std::size_t>::type
1058       align_of() {
1059     return do_align_of<T>();
1060   }
1061 };
1062 
1063 template <typename T>
1064 std::size_t size_of() {
1065   return layout::size_of<T>();
1066 }
1067 
1068 template <typename T>
1069 std::size_t align_of() {
1070   return layout::align_of<T>();
1071 }
1072 #endif // CXXBRIDGE1_LAYOUT
1073 
1074 #ifndef CXXBRIDGE1_RELOCATABLE
1075 #define CXXBRIDGE1_RELOCATABLE
1076 namespace detail {
1077 template <typename... Ts>
1078 struct make_void {
1079   using type = void;
1080 };
1081 
1082 template <typename... Ts>
1083 using void_t = typename make_void<Ts...>::type;
1084 
1085 template <typename Void, template <typename...> class, typename...>
1086 struct detect : std::false_type {};
1087 template <template <typename...> class T, typename... A>
1088 struct detect<void_t<T<A...>>, T, A...> : std::true_type {};
1089 
1090 template <template <typename...> class T, typename... A>
1091 using is_detected = detect<void, T, A...>;
1092 
1093 template <typename T>
1094 using detect_IsRelocatable = typename T::IsRelocatable;
1095 
1096 template <typename T>
1097 struct get_IsRelocatable
1098     : std::is_same<typename T::IsRelocatable, std::true_type> {};
1099 } // namespace detail
1100 
1101 template <typename T>
1102 struct IsRelocatable
1103     : std::conditional<
1104           detail::is_detected<detail::detect_IsRelocatable, T>::value,
1105           detail::get_IsRelocatable<T>,
1106           std::integral_constant<
1107               bool, std::is_trivially_move_constructible<T>::value &&
1108                         std::is_trivially_destructible<T>::value>>::type {};
1109 #endif // CXXBRIDGE1_RELOCATABLE
1110 
1111 } // namespace cxxbridge1
1112 } // namespace rust
1113