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