@ohah/hwpjs 0.1.0-rc.1

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