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;
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>
at(std::size_t n) const563 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>(this->stride);
663 }
664
665 template <typename T>
666 bool Slice<T>::iterator::operator==(const iterator &other) const noexcept {
667 return this->pos == other.pos;
668 }
669
670 template <typename T>
671 bool Slice<T>::iterator::operator!=(const iterator &other) const noexcept {
672 return this->pos != other.pos;
673 }
674
675 template <typename T>
676 bool Slice<T>::iterator::operator<(const iterator &other) const noexcept {
677 return this->pos < other.pos;
678 }
679
680 template <typename T>
681 bool Slice<T>::iterator::operator<=(const iterator &other) const noexcept {
682 return this->pos <= other.pos;
683 }
684
685 template <typename T>
686 bool Slice<T>::iterator::operator>(const iterator &other) const noexcept {
687 return this->pos > other.pos;
688 }
689
690 template <typename T>
691 bool Slice<T>::iterator::operator>=(const iterator &other) const noexcept {
692 return this->pos >= other.pos;
693 }
694
695 template <typename T>
696 typename 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
703 template <typename T>
704 typename 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
710 template <typename T>
711 void 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
718 template <typename T>
719 class Box<T>::uninit {};
720
721 template <typename T>
722 class Box<T>::allocation {
723 static T *alloc() noexcept;
724 static void dealloc(T *) noexcept;
725
726 public:
alloc()727 allocation() noexcept : ptr(alloc()) {}
728 ~allocation() noexcept {
729 if (this->ptr) {
730 dealloc(this->ptr);
731 }
732 }
733 T *ptr;
734 };
735
736 template <typename T>
ptr(other.ptr)737 Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
738 other.ptr = nullptr;
739 }
740
741 template <typename T>
Box(const T &val)742 Box<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
749 template <typename T>
Box(T &&val)750 Box<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
757 template <typename T>
758 Box<T>::~Box() noexcept {
759 if (this->ptr) {
760 this->drop();
761 }
762 }
763
764 template <typename T>
765 Box<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
774 template <typename T>
775 const T *Box<T>::operator->() const noexcept {
776 return this->ptr;
777 }
778
779 template <typename T>
780 const T &Box<T>::operator*() const noexcept {
781 return *this->ptr;
782 }
783
784 template <typename T>
785 T *Box<T>::operator->() noexcept {
786 return this->ptr;
787 }
788
789 template <typename T>
790 T &Box<T>::operator*() noexcept {
791 return *this->ptr;
792 }
793
794 template <typename T>
795 template <typename... Fields>
in_place(Fields &&....fields)796 Box<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
804 template <typename T>
805 void Box<T>::swap(Box &rhs) noexcept {
806 using std::swap;
807 swap(this->ptr, rhs.ptr);
808 }
809
810 template <typename T>
811 Box<T> Box<T>::from_raw(T *raw) noexcept {
812 Box box = uninit{};
813 box.ptr = raw;
814 return box;
815 }
816
817 template <typename T>
818 T *Box<T>::into_raw() noexcept {
819 T *raw = this->ptr;
820 this->ptr = nullptr;
821 return raw;
822 }
823
824 template <typename T>
825 Box<T>::Box(uninit) noexcept {}
826 #endif // CXXBRIDGE1_RUST_BOX
827
828 #ifndef CXXBRIDGE1_RUST_VEC
829 #define CXXBRIDGE1_RUST_VEC
830 template <typename T>
Vec(std::initializer_list<T> init)831 Vec<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
836 template <typename T>
Vec(const Vec &other)837 Vec<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
842 template <typename T>
repr(other.repr)843 Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {
844 new (&other) Vec();
845 }
846
847 template <typename T>
848 Vec<T>::~Vec() noexcept {
849 this->drop();
850 }
851
852 template <typename T>
853 Vec<T> &Vec<T>::operator=(Vec &&other) &noexcept {
854 this->drop();
855 this->repr = other.repr;
856 new (&other) Vec();
857 return *this;
858 }
859
860 template <typename T>
operator =(const Vec &other)861 Vec<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
869 template <typename T>
870 bool Vec<T>::empty() const noexcept {
871 return this->size() == 0;
872 }
873
874 template <typename T>
875 T *Vec<T>::data() noexcept {
876 return const_cast<T *>(const_cast<const Vec<T> *>(this)->data());
877 }
878
879 template <typename T>
880 const 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
886 template <typename T>
at(std::size_t n) const887 const 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
894 template <typename T>
895 const T &Vec<T>::front() const noexcept {
896 assert(!this->empty());
897 return (*this)[0];
898 }
899
900 template <typename T>
901 const T &Vec<T>::back() const noexcept {
902 assert(!this->empty());
903 return (*this)[this->size() - 1];
904 }
905
906 template <typename T>
907 T &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
913 template <typename T>
at(std::size_t n)914 T &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
921 template <typename T>
922 T &Vec<T>::front() noexcept {
923 assert(!this->empty());
924 return (*this)[0];
925 }
926
927 template <typename T>
928 T &Vec<T>::back() noexcept {
929 assert(!this->empty());
930 return (*this)[this->size() - 1];
931 }
932
933 template <typename T>
reserve(std::size_t new_cap)934 void Vec<T>::reserve(std::size_t new_cap) {
935 this->reserve_total(new_cap);
936 }
937
938 template <typename T>
push_back(const T &value)939 void Vec<T>::push_back(const T &value) {
940 this->emplace_back(value);
941 }
942
943 template <typename T>
push_back(T &&value)944 void Vec<T>::push_back(T &&value) {
945 this->emplace_back(std::move(value));
946 }
947
948 template <typename T>
949 template <typename... Args>
emplace_back(Args &&....args)950 void 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
959 template <typename T>
clear()960 void Vec<T>::clear() {
961 this->truncate(0);
962 }
963
964 template <typename T>
965 typename Vec<T>::iterator Vec<T>::begin() noexcept {
966 return Slice<T>(this->data(), this->size()).begin();
967 }
968
969 template <typename T>
970 typename Vec<T>::iterator Vec<T>::end() noexcept {
971 return Slice<T>(this->data(), this->size()).end();
972 }
973
974 template <typename T>
975 typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
976 return this->cbegin();
977 }
978
979 template <typename T>
980 typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
981 return this->cend();
982 }
983
984 template <typename T>
985 typename Vec<T>::const_iterator Vec<T>::cbegin() const noexcept {
986 return Slice<const T>(this->data(), this->size()).begin();
987 }
988
989 template <typename T>
990 typename Vec<T>::const_iterator Vec<T>::cend() const noexcept {
991 return Slice<const T>(this->data(), this->size()).end();
992 }
993
994 template <typename T>
995 void 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.
1001 template <typename T>
repr(bits.repr)1002 Vec<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
1007 namespace detail {
1008 namespace {
1009 template <typename T, typename = std::size_t>
1010 struct is_complete : std::false_type {};
1011 template <typename T>
1012 struct 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
1019 class 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
do_size_of()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
do_size_of()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
size_of()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
do_align_of()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
do_align_of()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
align_of()1057 align_of() {
1058 return do_align_of<T>();
1059 }
1060 };
1061
1062 template <typename T>
size_of()1063 std::size_t size_of() {
1064 return layout::size_of<T>();
1065 }
1066
1067 template <typename T>
align_of()1068 std::size_t align_of() {
1069 return layout::align_of<T>();
1070 }
1071 #endif // CXXBRIDGE1_LAYOUT
1072
1073 #ifndef CXXBRIDGE1_RELOCATABLE
1074 #define CXXBRIDGE1_RELOCATABLE
1075 namespace detail {
1076 template <typename... Ts>
1077 struct make_void {
1078 using type = void;
1079 };
1080
1081 template <typename... Ts>
1082 using void_t = typename make_void<Ts...>::type;
1083
1084 template <typename Void, template <typename...> class, typename...>
1085 struct detect : std::false_type {};
1086 template <template <typename...> class T, typename... A>
1087 struct detect<void_t<T<A...>>, T, A...> : std::true_type {};
1088
1089 template <template <typename...> class T, typename... A>
1090 using is_detected = detect<void, T, A...>;
1091
1092 template <typename T>
1093 using detect_IsRelocatable = typename T::IsRelocatable;
1094
1095 template <typename T>
1096 struct get_IsRelocatable
1097 : std::is_same<typename T::IsRelocatable, std::true_type> {};
1098 } // namespace detail
1099
1100 template <typename T>
1101 struct 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