1#include "engine/shared/snapshot.h"
2#include <array>
3#include <cassert>
4#include <cstddef>
5#include <cstdint>
6#include <iterator>
7#include <new>
8#include <stdexcept>
9#include <type_traits>
10#include <utility>
11#if __cplusplus >= 202002L
12#include <ranges>
13#endif
14
15#ifdef __GNUC__
16#pragma GCC diagnostic ignored "-Wmissing-declarations"
17#ifdef __clang__
18#pragma clang diagnostic ignored "-Wdollar-in-identifier-extension"
19#endif // __clang__
20#endif // __GNUC__
21
22namespace rust {
23inline namespace cxxbridge1 {
24// #include "rust/cxx.h"
25
26#ifndef CXXBRIDGE1_PANIC
27#define CXXBRIDGE1_PANIC
28template <typename Exception>
29void panic [[noreturn]] (const char *msg);
30#endif // CXXBRIDGE1_PANIC
31
32namespace {
33template <typename T>
34class impl;
35} // namespace
36
37template <typename T>
38::std::size_t size_of();
39template <typename T>
40::std::size_t align_of();
41
42#ifndef CXXBRIDGE1_RUST_SLICE
43#define CXXBRIDGE1_RUST_SLICE
44namespace detail {
45template <bool>
46struct copy_assignable_if {};
47
48template <>
49struct copy_assignable_if<false> {
50 copy_assignable_if() noexcept = default;
51 copy_assignable_if(const copy_assignable_if &) noexcept = default;
52 copy_assignable_if &operator=(const copy_assignable_if &) & noexcept = delete;
53 copy_assignable_if &operator=(copy_assignable_if &&) & noexcept = default;
54};
55} // namespace detail
56
57template <typename T>
58class Slice final
59 : private detail::copy_assignable_if<std::is_const<T>::value> {
60public:
61 using value_type = T;
62
63 Slice() noexcept;
64 Slice(T *, std::size_t count) noexcept;
65
66 template <typename C>
67 explicit Slice(C &c) : Slice(c.data(), c.size()) {}
68
69 Slice &operator=(const Slice<T> &) & noexcept = default;
70 Slice &operator=(Slice<T> &&) & noexcept = default;
71
72 T *data() const noexcept;
73 std::size_t size() const noexcept;
74 std::size_t length() const noexcept;
75 bool empty() const noexcept;
76
77 T &operator[](std::size_t n) const noexcept;
78 T &at(std::size_t n) const;
79 T &front() const noexcept;
80 T &back() const noexcept;
81
82 Slice(const Slice<T> &) noexcept = default;
83 ~Slice() noexcept = default;
84
85 class iterator;
86 iterator begin() const noexcept;
87 iterator end() const noexcept;
88
89 void swap(Slice &) noexcept;
90
91private:
92 class uninit;
93 Slice(uninit) noexcept;
94 friend impl<Slice>;
95 friend void sliceInit(void *, const void *, std::size_t) noexcept;
96 friend void *slicePtr(const void *) noexcept;
97 friend std::size_t sliceLen(const void *) noexcept;
98
99 std::array<std::uintptr_t, 2> repr;
100};
101
102#ifdef __cpp_deduction_guides
103template <typename C>
104explicit Slice(C &c)
105 -> Slice<std::remove_reference_t<decltype(*std::declval<C>().data())>>;
106#endif // __cpp_deduction_guides
107
108template <typename T>
109class Slice<T>::iterator final {
110public:
111#if __cplusplus >= 202002L
112 using iterator_category = std::contiguous_iterator_tag;
113#else
114 using iterator_category = std::random_access_iterator_tag;
115#endif
116 using value_type = T;
117 using difference_type = std::ptrdiff_t;
118 using pointer = typename std::add_pointer<T>::type;
119 using reference = typename std::add_lvalue_reference<T>::type;
120
121 reference operator*() const noexcept;
122 pointer operator->() const noexcept;
123 reference operator[](difference_type) const noexcept;
124
125 iterator &operator++() noexcept;
126 iterator operator++(int) noexcept;
127 iterator &operator--() noexcept;
128 iterator operator--(int) noexcept;
129
130 iterator &operator+=(difference_type) noexcept;
131 iterator &operator-=(difference_type) noexcept;
132 iterator operator+(difference_type) const noexcept;
133 friend inline iterator operator+(difference_type lhs, iterator rhs) noexcept {
134 return rhs + lhs;
135 }
136 iterator operator-(difference_type) const noexcept;
137 difference_type operator-(const iterator &) const noexcept;
138
139 bool operator==(const iterator &) const noexcept;
140 bool operator!=(const iterator &) const noexcept;
141 bool operator<(const iterator &) const noexcept;
142 bool operator<=(const iterator &) const noexcept;
143 bool operator>(const iterator &) const noexcept;
144 bool operator>=(const iterator &) const noexcept;
145
146private:
147 friend class Slice;
148 void *pos;
149 std::size_t stride;
150};
151
152#if __cplusplus >= 202002L
153static_assert(std::ranges::contiguous_range<rust::Slice<const uint8_t>>);
154static_assert(std::contiguous_iterator<rust::Slice<const uint8_t>::iterator>);
155#endif
156
157template <typename T>
158Slice<T>::Slice() noexcept {
159 sliceInit(this, reinterpret_cast<void *>(align_of<T>()), 0);
160}
161
162template <typename T>
163Slice<T>::Slice(T *s, std::size_t count) noexcept {
164 assert(s != nullptr || count == 0);
165 sliceInit(this,
166 s == nullptr && count == 0
167 ? reinterpret_cast<void *>(align_of<T>())
168 : const_cast<typename std::remove_const<T>::type *>(s),
169 count);
170}
171
172template <typename T>
173T *Slice<T>::data() const noexcept {
174 return reinterpret_cast<T *>(slicePtr(this));
175}
176
177template <typename T>
178std::size_t Slice<T>::size() const noexcept {
179 return sliceLen(this);
180}
181
182template <typename T>
183std::size_t Slice<T>::length() const noexcept {
184 return this->size();
185}
186
187template <typename T>
188bool Slice<T>::empty() const noexcept {
189 return this->size() == 0;
190}
191
192template <typename T>
193T &Slice<T>::operator[](std::size_t n) const noexcept {
194 assert(n < this->size());
195 auto ptr = static_cast<char *>(slicePtr(this)) + size_of<T>() * n;
196 return *reinterpret_cast<T *>(ptr);
197}
198
199template <typename T>
200T &Slice<T>::at(std::size_t n) const {
201 if (n >= this->size()) {
202 panic<std::out_of_range>("rust::Slice index out of range");
203 }
204 return (*this)[n];
205}
206
207template <typename T>
208T &Slice<T>::front() const noexcept {
209 assert(!this->empty());
210 return (*this)[0];
211}
212
213template <typename T>
214T &Slice<T>::back() const noexcept {
215 assert(!this->empty());
216 return (*this)[this->size() - 1];
217}
218
219template <typename T>
220typename Slice<T>::iterator::reference
221Slice<T>::iterator::operator*() const noexcept {
222 return *static_cast<T *>(this->pos);
223}
224
225template <typename T>
226typename Slice<T>::iterator::pointer
227Slice<T>::iterator::operator->() const noexcept {
228 return static_cast<T *>(this->pos);
229}
230
231template <typename T>
232typename Slice<T>::iterator::reference Slice<T>::iterator::operator[](
233 typename Slice<T>::iterator::difference_type n) const noexcept {
234 auto ptr = static_cast<char *>(this->pos) + this->stride * n;
235 return *reinterpret_cast<T *>(ptr);
236}
237
238template <typename T>
239typename Slice<T>::iterator &Slice<T>::iterator::operator++() noexcept {
240 this->pos = static_cast<char *>(this->pos) + this->stride;
241 return *this;
242}
243
244template <typename T>
245typename Slice<T>::iterator Slice<T>::iterator::operator++(int) noexcept {
246 auto ret = iterator(*this);
247 this->pos = static_cast<char *>(this->pos) + this->stride;
248 return ret;
249}
250
251template <typename T>
252typename Slice<T>::iterator &Slice<T>::iterator::operator--() noexcept {
253 this->pos = static_cast<char *>(this->pos) - this->stride;
254 return *this;
255}
256
257template <typename T>
258typename Slice<T>::iterator Slice<T>::iterator::operator--(int) noexcept {
259 auto ret = iterator(*this);
260 this->pos = static_cast<char *>(this->pos) - this->stride;
261 return ret;
262}
263
264template <typename T>
265typename Slice<T>::iterator &Slice<T>::iterator::operator+=(
266 typename Slice<T>::iterator::difference_type n) noexcept {
267 this->pos = static_cast<char *>(this->pos) + this->stride * n;
268 return *this;
269}
270
271template <typename T>
272typename Slice<T>::iterator &Slice<T>::iterator::operator-=(
273 typename Slice<T>::iterator::difference_type n) noexcept {
274 this->pos = static_cast<char *>(this->pos) - this->stride * n;
275 return *this;
276}
277
278template <typename T>
279typename Slice<T>::iterator Slice<T>::iterator::operator+(
280 typename Slice<T>::iterator::difference_type n) const noexcept {
281 auto ret = iterator(*this);
282 ret.pos = static_cast<char *>(this->pos) + this->stride * n;
283 return ret;
284}
285
286template <typename T>
287typename Slice<T>::iterator Slice<T>::iterator::operator-(
288 typename Slice<T>::iterator::difference_type n) const noexcept {
289 auto ret = iterator(*this);
290 ret.pos = static_cast<char *>(this->pos) - this->stride * n;
291 return ret;
292}
293
294template <typename T>
295typename Slice<T>::iterator::difference_type
296Slice<T>::iterator::operator-(const iterator &other) const noexcept {
297 auto diff = std::distance(static_cast<char *>(other.pos),
298 static_cast<char *>(this->pos));
299 return diff / static_cast<typename Slice<T>::iterator::difference_type>(
300 this->stride);
301}
302
303template <typename T>
304bool Slice<T>::iterator::operator==(const iterator &other) const noexcept {
305 return this->pos == other.pos;
306}
307
308template <typename T>
309bool Slice<T>::iterator::operator!=(const iterator &other) const noexcept {
310 return this->pos != other.pos;
311}
312
313template <typename T>
314bool Slice<T>::iterator::operator<(const iterator &other) const noexcept {
315 return this->pos < other.pos;
316}
317
318template <typename T>
319bool Slice<T>::iterator::operator<=(const iterator &other) const noexcept {
320 return this->pos <= other.pos;
321}
322
323template <typename T>
324bool Slice<T>::iterator::operator>(const iterator &other) const noexcept {
325 return this->pos > other.pos;
326}
327
328template <typename T>
329bool Slice<T>::iterator::operator>=(const iterator &other) const noexcept {
330 return this->pos >= other.pos;
331}
332
333template <typename T>
334typename Slice<T>::iterator Slice<T>::begin() const noexcept {
335 iterator it;
336 it.pos = slicePtr(this);
337 it.stride = size_of<T>();
338 return it;
339}
340
341template <typename T>
342typename Slice<T>::iterator Slice<T>::end() const noexcept {
343 iterator it = this->begin();
344 it.pos = static_cast<char *>(it.pos) + it.stride * this->size();
345 return it;
346}
347
348template <typename T>
349void Slice<T>::swap(Slice &rhs) noexcept {
350 std::swap(*this, rhs);
351}
352#endif // CXXBRIDGE1_RUST_SLICE
353
354#ifndef CXXBRIDGE1_RUST_BOX
355#define CXXBRIDGE1_RUST_BOX
356template <typename T>
357class Box final {
358public:
359 using element_type = T;
360 using const_pointer =
361 typename std::add_pointer<typename std::add_const<T>::type>::type;
362 using pointer = typename std::add_pointer<T>::type;
363
364 Box() = delete;
365 Box(Box &&) noexcept;
366 ~Box() noexcept;
367
368 explicit Box(const T &);
369 explicit Box(T &&);
370
371 Box &operator=(Box &&) & noexcept;
372
373 const T *operator->() const noexcept;
374 const T &operator*() const noexcept;
375 T *operator->() noexcept;
376 T &operator*() noexcept;
377
378 template <typename... Fields>
379 static Box in_place(Fields &&...);
380
381 void swap(Box &) noexcept;
382
383 static Box from_raw(T *) noexcept;
384
385 T *into_raw() noexcept;
386
387 /* Deprecated */ using value_type = element_type;
388
389private:
390 class uninit;
391 class allocation;
392 Box(uninit) noexcept;
393 void drop() noexcept;
394
395 friend void swap(Box &lhs, Box &rhs) noexcept { lhs.swap(rhs); }
396
397 T *ptr;
398};
399
400template <typename T>
401class Box<T>::uninit {};
402
403template <typename T>
404class Box<T>::allocation {
405 static T *alloc() noexcept;
406 static void dealloc(T *) noexcept;
407
408public:
409 allocation() noexcept : ptr(alloc()) {}
410 ~allocation() noexcept {
411 if (this->ptr) {
412 dealloc(this->ptr);
413 }
414 }
415 T *ptr;
416};
417
418template <typename T>
419Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
420 other.ptr = nullptr;
421}
422
423template <typename T>
424Box<T>::Box(const T &val) {
425 allocation alloc;
426 ::new (alloc.ptr) T(val);
427 this->ptr = alloc.ptr;
428 alloc.ptr = nullptr;
429}
430
431template <typename T>
432Box<T>::Box(T &&val) {
433 allocation alloc;
434 ::new (alloc.ptr) T(std::move(val));
435 this->ptr = alloc.ptr;
436 alloc.ptr = nullptr;
437}
438
439template <typename T>
440Box<T>::~Box() noexcept {
441 if (this->ptr) {
442 this->drop();
443 }
444}
445
446template <typename T>
447Box<T> &Box<T>::operator=(Box &&other) & noexcept {
448 if (this->ptr) {
449 this->drop();
450 }
451 this->ptr = other.ptr;
452 other.ptr = nullptr;
453 return *this;
454}
455
456template <typename T>
457const T *Box<T>::operator->() const noexcept {
458 return this->ptr;
459}
460
461template <typename T>
462const T &Box<T>::operator*() const noexcept {
463 return *this->ptr;
464}
465
466template <typename T>
467T *Box<T>::operator->() noexcept {
468 return this->ptr;
469}
470
471template <typename T>
472T &Box<T>::operator*() noexcept {
473 return *this->ptr;
474}
475
476template <typename T>
477template <typename... Fields>
478Box<T> Box<T>::in_place(Fields &&...fields) {
479 allocation alloc;
480 auto ptr = alloc.ptr;
481 ::new (ptr) T{std::forward<Fields>(fields)...};
482 alloc.ptr = nullptr;
483 return from_raw(ptr);
484}
485
486template <typename T>
487void Box<T>::swap(Box &rhs) noexcept {
488 using std::swap;
489 swap(this->ptr, rhs.ptr);
490}
491
492template <typename T>
493Box<T> Box<T>::from_raw(T *raw) noexcept {
494 Box box = uninit{};
495 box.ptr = raw;
496 return box;
497}
498
499template <typename T>
500T *Box<T>::into_raw() noexcept {
501 T *raw = this->ptr;
502 this->ptr = nullptr;
503 return raw;
504}
505
506template <typename T>
507Box<T>::Box(uninit) noexcept {}
508#endif // CXXBRIDGE1_RUST_BOX
509
510#ifndef CXXBRIDGE1_RUST_OPAQUE
511#define CXXBRIDGE1_RUST_OPAQUE
512class Opaque {
513public:
514 Opaque() = delete;
515 Opaque(const Opaque &) = delete;
516 ~Opaque() = delete;
517};
518#endif // CXXBRIDGE1_RUST_OPAQUE
519
520#ifndef CXXBRIDGE1_IS_COMPLETE
521#define CXXBRIDGE1_IS_COMPLETE
522namespace detail {
523namespace {
524template <typename T, typename = std::size_t>
525struct is_complete : std::false_type {};
526template <typename T>
527struct is_complete<T, decltype(sizeof(T))> : std::true_type {};
528} // namespace
529} // namespace detail
530#endif // CXXBRIDGE1_IS_COMPLETE
531
532#ifndef CXXBRIDGE1_LAYOUT
533#define CXXBRIDGE1_LAYOUT
534class layout {
535 template <typename T>
536 friend std::size_t size_of();
537 template <typename T>
538 friend std::size_t align_of();
539 template <typename T>
540 static typename std::enable_if<std::is_base_of<Opaque, T>::value,
541 std::size_t>::type
542 do_size_of() {
543 return T::layout::size();
544 }
545 template <typename T>
546 static typename std::enable_if<!std::is_base_of<Opaque, T>::value,
547 std::size_t>::type
548 do_size_of() {
549 return sizeof(T);
550 }
551 template <typename T>
552 static
553 typename std::enable_if<detail::is_complete<T>::value, std::size_t>::type
554 size_of() {
555 return do_size_of<T>();
556 }
557 template <typename T>
558 static typename std::enable_if<std::is_base_of<Opaque, T>::value,
559 std::size_t>::type
560 do_align_of() {
561 return T::layout::align();
562 }
563 template <typename T>
564 static typename std::enable_if<!std::is_base_of<Opaque, T>::value,
565 std::size_t>::type
566 do_align_of() {
567 return alignof(T);
568 }
569 template <typename T>
570 static
571 typename std::enable_if<detail::is_complete<T>::value, std::size_t>::type
572 align_of() {
573 return do_align_of<T>();
574 }
575};
576
577template <typename T>
578std::size_t size_of() {
579 return layout::size_of<T>();
580}
581
582template <typename T>
583std::size_t align_of() {
584 return layout::align_of<T>();
585}
586#endif // CXXBRIDGE1_LAYOUT
587} // namespace cxxbridge1
588} // namespace rust
589
590struct CSnapshotBuilder;
591
592#ifndef CXXBRIDGE1_STRUCT_CSnapshotBuilder
593#define CXXBRIDGE1_STRUCT_CSnapshotBuilder
594struct CSnapshotBuilder final : public ::rust::Opaque {
595 static ::rust::Box<::CSnapshotBuilder> New() noexcept;
596 void Init(bool sixup) noexcept;
597 bool NewItem(::std::int32_t type_, ::std::int32_t id, ::rust::Slice<::std::int32_t const> data) noexcept;
598 ::std::int32_t FinishIfNoDroppedItems(::CSnapshotBuffer &buffer) noexcept;
599 ::std::int32_t Finish(::CSnapshotBuffer &buffer) noexcept;
600 ~CSnapshotBuilder() = delete;
601
602private:
603 friend ::rust::layout;
604 struct layout {
605 static ::std::size_t size() noexcept;
606 static ::std::size_t align() noexcept;
607 };
608};
609#endif // CXXBRIDGE1_STRUCT_CSnapshotBuilder
610
611extern "C" {
612::std::size_t cxxbridge1$194$CSnapshotBuilder$operator$sizeof() noexcept;
613::std::size_t cxxbridge1$194$CSnapshotBuilder$operator$alignof() noexcept;
614
615::CSnapshotBuilder *cxxbridge1$194$CSnapshotBuilder$New() noexcept;
616
617void cxxbridge1$194$CSnapshotBuilder$Init(::CSnapshotBuilder &self, bool sixup) noexcept;
618
619bool cxxbridge1$194$CSnapshotBuilder$NewItem(::CSnapshotBuilder &self, ::std::int32_t type_, ::std::int32_t id, ::rust::Slice<::std::int32_t const> data) noexcept;
620
621::std::int32_t cxxbridge1$194$CSnapshotBuilder$FinishIfNoDroppedItems(::CSnapshotBuilder &self, ::CSnapshotBuffer &buffer) noexcept;
622
623::std::int32_t cxxbridge1$194$CSnapshotBuilder$Finish(::CSnapshotBuilder &self, ::CSnapshotBuffer &buffer) noexcept;
624} // extern "C"
625
626::std::size_t CSnapshotBuilder::layout::size() noexcept {
627 return cxxbridge1$194$CSnapshotBuilder$operator$sizeof();
628}
629
630::std::size_t CSnapshotBuilder::layout::align() noexcept {
631 return cxxbridge1$194$CSnapshotBuilder$operator$alignof();
632}
633
634::rust::Box<::CSnapshotBuilder> CSnapshotBuilder::New() noexcept {
635 return ::rust::Box<::CSnapshotBuilder>::from_raw(raw: cxxbridge1$194$CSnapshotBuilder$New());
636}
637
638void CSnapshotBuilder::Init(bool sixup) noexcept {
639 cxxbridge1$194$CSnapshotBuilder$Init(self&: *this, sixup);
640}
641
642bool CSnapshotBuilder::NewItem(::std::int32_t type_, ::std::int32_t id, ::rust::Slice<::std::int32_t const> data) noexcept {
643 return cxxbridge1$194$CSnapshotBuilder$NewItem(self&: *this, type_, id, data);
644}
645
646::std::int32_t CSnapshotBuilder::FinishIfNoDroppedItems(::CSnapshotBuffer &buffer) noexcept {
647 return cxxbridge1$194$CSnapshotBuilder$FinishIfNoDroppedItems(self&: *this, buffer);
648}
649
650::std::int32_t CSnapshotBuilder::Finish(::CSnapshotBuffer &buffer) noexcept {
651 return cxxbridge1$194$CSnapshotBuilder$Finish(self&: *this, buffer);
652}
653
654extern "C" {
655::CSnapshotBuilder *cxxbridge1$box$CSnapshotBuilder$alloc() noexcept;
656void cxxbridge1$box$CSnapshotBuilder$dealloc(::CSnapshotBuilder *) noexcept;
657void cxxbridge1$box$CSnapshotBuilder$drop(::rust::Box<::CSnapshotBuilder> *ptr) noexcept;
658} // extern "C"
659
660namespace rust {
661inline namespace cxxbridge1 {
662template <>
663::CSnapshotBuilder *Box<::CSnapshotBuilder>::allocation::alloc() noexcept {
664 return cxxbridge1$box$CSnapshotBuilder$alloc();
665}
666template <>
667void Box<::CSnapshotBuilder>::allocation::dealloc(::CSnapshotBuilder *ptr) noexcept {
668 cxxbridge1$box$CSnapshotBuilder$dealloc(ptr);
669}
670template <>
671void Box<::CSnapshotBuilder>::drop() noexcept {
672 cxxbridge1$box$CSnapshotBuilder$drop(ptr: this);
673}
674} // namespace cxxbridge1
675} // namespace rust
676