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