blob: 786dbb3b504faaaa8e1ae97024f9b82ca692d8ba [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001#pragma once
2#include <array>
David Tolnay30430f12020-03-19 20:49:00 -07003#include <cstddef>
David Tolnay7db73692019-10-20 14:51:12 -04004#include <cstdint>
David Tolnayb7a7cb62020-03-17 21:18:40 -07005#include <exception>
David Tolnay001102a2020-03-01 20:05:04 -08006#include <iosfwd>
David Tolnay0ecd05a2020-07-29 16:32:03 -07007#include <new>
Stephen Crane9e48d5b2020-08-21 12:17:02 -07008#include <stdexcept>
David Tolnay7db73692019-10-20 14:51:12 -04009#include <string>
David Tolnayf6292372020-03-01 21:09:11 -080010#include <type_traits>
David Tolnay4791f1c2020-03-17 21:53:16 -070011#include <utility>
David Tolnay37dd7e12020-04-25 12:51:59 -070012#include <vector>
David Tolnay59b5ba12020-04-10 11:32:19 -070013#if defined(_WIN32)
14#include <BaseTsd.h>
15#endif
David Tolnay7db73692019-10-20 14:51:12 -040016
Stephen Crane9e48d5b2020-08-21 12:17:02 -070017#ifndef __has_feature
18#define __has_feature(__x) 0
19#endif
20
21#if !__has_feature(cxx_exceptions)
22# define _CXXBRIDGE03_NO_EXCEPTIONS
23#endif
24#if !__EXCEPTIONS
25# define _CXXBRIDGE03_NO_EXCEPTIONS
26#endif
27
28[[noreturn]] inline static void throw_out_of_range(const char *msg) {
29#ifndef _CXXBRIDGE03_NO_EXCEPTIONS
30 throw std::out_of_range(msg);
31#else
32 ((void)msg);
33 std::abort();
34#endif
35}
36
David Tolnay750755e2020-03-01 13:04:08 -080037namespace rust {
David Tolnay69601622020-04-29 18:48:36 -070038inline namespace cxxbridge03 {
David Tolnay7db73692019-10-20 14:51:12 -040039
David Tolnay2a2b9ad2020-05-12 20:07:26 -070040struct unsafe_bitcopy_t;
David Tolnayd1e2efc2020-03-03 22:25:43 -080041
David Tolnay69601622020-04-29 18:48:36 -070042#ifndef CXXBRIDGE03_RUST_STRING
43#define CXXBRIDGE03_RUST_STRING
David Tolnay56082162020-03-01 12:57:33 -080044class String final {
David Tolnay7db73692019-10-20 14:51:12 -040045public:
David Tolnay56082162020-03-01 12:57:33 -080046 String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080047 String(const String &) noexcept;
48 String(String &&) noexcept;
David Tolnay56082162020-03-01 12:57:33 -080049 ~String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080050
51 String(const std::string &);
52 String(const char *);
David Tolnayc2bbd952020-07-29 18:15:26 -070053 String(const char *, size_t);
David Tolnayd9c4ac92020-03-01 20:33:58 -080054
55 String &operator=(const String &) noexcept;
56 String &operator=(String &&) noexcept;
57
David Tolnay404d6892020-03-01 20:19:41 -080058 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040059
60 // Note: no null terminator.
61 const char *data() const noexcept;
62 size_t size() const noexcept;
63 size_t length() const noexcept;
64
David Tolnayd1e2efc2020-03-03 22:25:43 -080065 // Internal API only intended for the cxxbridge code generator.
66 String(unsafe_bitcopy_t, const String &) noexcept;
67
David Tolnay7db73692019-10-20 14:51:12 -040068private:
69 // Size and alignment statically verified by rust_string.rs.
70 std::array<uintptr_t, 3> repr;
71};
David Tolnay69601622020-04-29 18:48:36 -070072#endif // CXXBRIDGE03_RUST_STRING
David Tolnay7db73692019-10-20 14:51:12 -040073
David Tolnay69601622020-04-29 18:48:36 -070074#ifndef CXXBRIDGE03_RUST_STR
75#define CXXBRIDGE03_RUST_STR
David Tolnay09dbe752020-03-01 13:00:40 -080076class Str final {
David Tolnay7db73692019-10-20 14:51:12 -040077public:
David Tolnay09dbe752020-03-01 13:00:40 -080078 Str() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080079 Str(const Str &) noexcept;
80
David Tolnay851677c2020-03-01 23:49:46 -080081 Str(const std::string &);
82 Str(const char *);
David Tolnay894c5e42020-07-29 18:20:00 -070083 Str(const char *, size_t);
David Tolnay851677c2020-03-01 23:49:46 -080084 Str(std::string &&) = delete;
David Tolnayd9c4ac92020-03-01 20:33:58 -080085
86 Str &operator=(Str) noexcept;
87
David Tolnay404d6892020-03-01 20:19:41 -080088 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040089
90 // Note: no null terminator.
91 const char *data() const noexcept;
92 size_t size() const noexcept;
93 size_t length() const noexcept;
94
95 // Repr is PRIVATE; must not be used other than by our generated code.
96 //
97 // Not necessarily ABI compatible with &str. Codegen will translate to
98 // cxx::rust_str::RustStr which matches this layout.
99 struct Repr {
100 const char *ptr;
101 size_t len;
102 };
David Tolnayd9c4ac92020-03-01 20:33:58 -0800103 Str(Repr) noexcept;
David Tolnaybaae4432020-03-01 20:20:10 -0800104 explicit operator Repr() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400105
106private:
107 Repr repr;
108};
David Tolnay69601622020-04-29 18:48:36 -0700109#endif // CXXBRIDGE03_RUST_STR
David Tolnay7db73692019-10-20 14:51:12 -0400110
David Tolnay69601622020-04-29 18:48:36 -0700111#ifndef CXXBRIDGE03_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -0700112template <typename T>
113class Slice final {
114public:
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700115 Slice() noexcept;
116 Slice(const Slice<T> &) noexcept;
117 Slice(const T *, size_t count) noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700118
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700119 Slice &operator=(Slice<T>) noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700120
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700121 const T *data() const noexcept;
122 size_t size() const noexcept;
123 size_t length() const noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700124
125 // Repr is PRIVATE; must not be used other than by our generated code.
126 //
127 // At present this class is only used for &[u8] slices.
128 // Not necessarily ABI compatible with &[u8]. Codegen will translate to
David Tolnaye710af12020-04-14 16:31:54 -0700129 // cxx::rust_sliceu8::RustSliceU8 which matches this layout.
David Tolnayefe81052020-04-14 16:28:24 -0700130 struct Repr {
131 const T *ptr;
132 size_t len;
133 };
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700134 Slice(Repr) noexcept;
135 explicit operator Repr() noexcept;
David Tolnayefe81052020-04-14 16:28:24 -0700136
137private:
138 Repr repr;
139};
David Tolnay69601622020-04-29 18:48:36 -0700140#endif // CXXBRIDGE03_RUST_SLICE
David Tolnayefe81052020-04-14 16:28:24 -0700141
David Tolnay69601622020-04-29 18:48:36 -0700142#ifndef CXXBRIDGE03_RUST_BOX
David Tolnayf262d382020-04-11 22:12:40 -0700143template <typename T>
144class Box final {
David Tolnay7db73692019-10-20 14:51:12 -0400145public:
David Tolnayf6292372020-03-01 21:09:11 -0800146 using value_type = T;
David Tolnay9706a512020-04-24 17:09:01 -0700147 using const_pointer =
148 typename std::add_pointer<typename std::add_const<T>::type>::type;
149 using pointer = typename std::add_pointer<T>::type;
David Tolnayf6292372020-03-01 21:09:11 -0800150
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700151 Box(const Box &);
152 Box(Box &&) noexcept;
153 ~Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400154
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700155 explicit Box(const T &);
156 explicit Box(T &&);
157
158 Box &operator=(const Box &);
159 Box &operator=(Box &&) noexcept;
160
161 const T *operator->() const noexcept;
162 const T &operator*() const noexcept;
163 T *operator->() noexcept;
164 T &operator*() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400165
David Tolnayf262d382020-04-11 22:12:40 -0700166 template <typename... Fields>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700167 static Box in_place(Fields &&...);
David Tolnay7ce59fc2020-04-11 11:46:33 -0700168
David Tolnay7db73692019-10-20 14:51:12 -0400169 // Important: requires that `raw` came from an into_raw call. Do not pass a
170 // pointer from `new` or any other source.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700171 static Box from_raw(T *) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400172
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700173 T *into_raw() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400174
175private:
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700176 Box() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400177 void uninit() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400178 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800179 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400180};
David Tolnay69601622020-04-29 18:48:36 -0700181#endif // CXXBRIDGE03_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400182
David Tolnay69601622020-04-29 18:48:36 -0700183#ifndef CXXBRIDGE03_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700184template <typename T>
185class Vec final {
186public:
David Tolnayc87c2152020-04-24 17:07:41 -0700187 using value_type = T;
188
David Tolnayf97c2d52020-04-25 16:37:48 -0700189 Vec() noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700190 Vec(Vec &&) noexcept;
191 ~Vec() noexcept;
David Tolnaycb800572020-04-24 20:30:43 -0700192
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700193 Vec &operator=(Vec &&) noexcept;
David Tolnayf97c2d52020-04-25 16:37:48 -0700194
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700195 size_t size() const noexcept;
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700196 bool empty() const noexcept;
David Tolnay219c0792020-04-24 20:31:37 -0700197 const T *data() const noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700198
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700199 const T &operator[](size_t n) const noexcept;
200 const T &at(size_t n) const;
201
202 const T &front() const;
203 const T &back() const;
204
David Tolnayc87c2152020-04-24 17:07:41 -0700205 class const_iterator {
206 public:
myronahnda9be502020-04-29 05:47:23 +0700207 using difference_type = ptrdiff_t;
David Tolnayc87c2152020-04-24 17:07:41 -0700208 using value_type = typename std::add_const<T>::type;
David Tolnay74dd3792020-04-30 07:45:24 -0700209 using pointer =
210 typename std::add_pointer<typename std::add_const<T>::type>::type;
David Tolnayc87c2152020-04-24 17:07:41 -0700211 using reference = typename std::add_lvalue_reference<
212 typename std::add_const<T>::type>::type;
myronahnda9be502020-04-29 05:47:23 +0700213 using iterator_category = std::forward_iterator_tag;
David Tolnayc87c2152020-04-24 17:07:41 -0700214
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700215 const T &operator*() const noexcept;
216 const T *operator->() const noexcept;
217 const_iterator &operator++() noexcept;
218 const_iterator operator++(int) noexcept;
219 bool operator==(const const_iterator &) const noexcept;
220 bool operator!=(const const_iterator &) const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700221
222 private:
223 friend class Vec;
224 const void *pos;
225 size_t stride;
226 };
227
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700228 const_iterator begin() const noexcept;
229 const_iterator end() const noexcept;
David Tolnayc87c2152020-04-24 17:07:41 -0700230
David Tolnay313b10e2020-04-25 16:30:51 -0700231 // Internal API only intended for the cxxbridge code generator.
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700232 Vec(unsafe_bitcopy_t, const Vec &) noexcept;
David Tolnay313b10e2020-04-25 16:30:51 -0700233
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700234private:
David Tolnay503d0192020-04-24 22:18:56 -0700235 static size_t stride() noexcept;
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700236 void drop() noexcept;
237
238 // Size and alignment statically verified by rust_vec.rs.
239 std::array<uintptr_t, 3> repr;
240};
David Tolnay69601622020-04-29 18:48:36 -0700241#endif // CXXBRIDGE03_RUST_VEC
David Tolnay7f2dc3b2020-04-24 16:46:39 -0700242
David Tolnay69601622020-04-29 18:48:36 -0700243#ifndef CXXBRIDGE03_RUST_FN
244#define CXXBRIDGE03_RUST_FN
David Tolnayf262d382020-04-11 22:12:40 -0700245template <typename Signature, bool Throws = false>
246class Fn;
David Tolnay75dca2e2020-03-25 20:17:52 -0700247
248template <typename Ret, typename... Args, bool Throws>
249class Fn<Ret(Args...), Throws> {
250public:
David Tolnay533d4582020-04-08 20:29:14 -0700251 Ret operator()(Args... args) const noexcept(!Throws);
252 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700253
254private:
255 Ret (*trampoline)(Args..., void *fn) noexcept(!Throws);
256 void *fn;
257};
258
David Tolnayf262d382020-04-11 22:12:40 -0700259template <typename Signature>
260using TryFn = Fn<Signature, true>;
David Tolnay69601622020-04-29 18:48:36 -0700261#endif // CXXBRIDGE03_RUST_FN
David Tolnay75dca2e2020-03-25 20:17:52 -0700262
David Tolnay69601622020-04-29 18:48:36 -0700263#ifndef CXXBRIDGE03_RUST_ERROR
264#define CXXBRIDGE03_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700265class Error final : std::exception {
266public:
267 Error(const Error &);
268 Error(Error &&) noexcept;
269 Error(Str::Repr) noexcept;
270 ~Error() noexcept;
271 const char *what() const noexcept override;
272
273private:
274 Str::Repr msg;
275};
David Tolnay69601622020-04-29 18:48:36 -0700276#endif // CXXBRIDGE03_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700277
David Tolnay69601622020-04-29 18:48:36 -0700278#ifndef CXXBRIDGE03_RUST_ISIZE
279#define CXXBRIDGE03_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700280#if defined(_WIN32)
281using isize = SSIZE_T;
282#else
283using isize = ssize_t;
284#endif
David Tolnay69601622020-04-29 18:48:36 -0700285#endif // CXXBRIDGE03_RUST_ISIZE
David Tolnayb8a6fb22020-04-10 11:17:28 -0700286
David Tolnay851677c2020-03-01 23:49:46 -0800287std::ostream &operator<<(std::ostream &, const String &);
288std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400289
David Tolnay3b0c9882020-03-01 14:08:57 -0800290// Snake case aliases for use in code that uses this style for type names.
291using string = String;
292using str = Str;
David Tolnayf262d382020-04-11 22:12:40 -0700293template <class T>
294using box = Box<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700295using error = Error;
David Tolnay75dca2e2020-03-25 20:17:52 -0700296template <typename Signature, bool Throws = false>
297using fn = Fn<Signature, Throws>;
David Tolnayf262d382020-04-11 22:12:40 -0700298template <typename Signature>
299using try_fn = TryFn<Signature>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800300
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700301
302
303////////////////////////////////////////////////////////////////////////////////
304/// end public API, begin implementation details
305
David Tolnay75dca2e2020-03-25 20:17:52 -0700306template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700307Ret Fn<Ret(Args...), Throws>::operator()(Args... args) const noexcept(!Throws) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700308 return (*this->trampoline)(std::move(args)..., this->fn);
309}
310
David Tolnaya23129c2020-04-08 20:08:21 -0700311template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700312Fn<Ret(Args...), Throws> Fn<Ret(Args...), Throws>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700313 return *this;
314}
315
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700316#ifndef CXXBRIDGE03_RUST_BITCOPY
317#define CXXBRIDGE03_RUST_BITCOPY
318struct unsafe_bitcopy_t {
319 explicit unsafe_bitcopy_t() = default;
320};
321
322constexpr unsafe_bitcopy_t unsafe_bitcopy{};
323#endif // CXXBRIDGE03_RUST_BITCOPY
324
325#ifndef CXXBRIDGE03_RUST_SLICE
326#define CXXBRIDGE03_RUST_SLICE
327template <typename T>
328Slice<T>::Slice() noexcept : repr(Repr{reinterpret_cast<const T *>(this), 0}) {}
329
330template <typename T>
331Slice<T>::Slice(const Slice<T> &) noexcept = default;
332
333template <typename T>
334Slice<T>::Slice(const T *s, size_t count) noexcept : repr(Repr{s, count}) {}
335
336template <typename T>
337Slice<T> &Slice<T>::operator=(Slice<T> other) noexcept {
338 this->repr = other.repr;
339 return *this;
340}
341
342template <typename T>
343const T *Slice<T>::data() const noexcept {
344 return this->repr.ptr;
345}
346
347template <typename T>
348size_t Slice<T>::size() const noexcept {
349 return this->repr.len;
350}
351
352template <typename T>
353size_t Slice<T>::length() const noexcept {
354 return this->repr.len;
355}
356
357template <typename T>
358Slice<T>::Slice(Repr repr_) noexcept : repr(repr_) {}
359
360template <typename T>
361Slice<T>::operator Repr() noexcept {
362 return this->repr;
363}
364#endif // CXXBRIDGE03_RUST_SLICE
365
366#ifndef CXXBRIDGE03_RUST_BOX
367#define CXXBRIDGE03_RUST_BOX
368template <typename T>
369Box<T>::Box(const Box &other) : Box(*other) {}
370
371template <typename T>
372Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) {
373 other.ptr = nullptr;
374}
375
376template <typename T>
377Box<T>::Box(const T &val) {
378 this->uninit();
379 ::new (this->ptr) T(val);
380}
381
382template <typename T>
383Box<T>::Box(T &&val) {
384 this->uninit();
385 ::new (this->ptr) T(std::move(val));
386}
387
388template <typename T>
389Box<T>::~Box() noexcept {
390 if (this->ptr) {
391 this->drop();
392 }
393}
394
395template <typename T>
396Box<T> &Box<T>::operator=(const Box &other) {
397 if (this != &other) {
398 if (this->ptr) {
399 **this = *other;
400 } else {
401 this->uninit();
402 ::new (this->ptr) T(*other);
403 }
404 }
405 return *this;
406}
407
408template <typename T>
409Box<T> &Box<T>::operator=(Box &&other) noexcept {
410 if (this->ptr) {
411 this->drop();
412 }
413 this->ptr = other.ptr;
414 other.ptr = nullptr;
415 return *this;
416}
417
418template <typename T>
419const T *Box<T>::operator->() const noexcept {
420 return this->ptr;
421}
422
423template <typename T>
424const T &Box<T>::operator*() const noexcept {
425 return *this->ptr;
426}
427
428template <typename T>
429T *Box<T>::operator->() noexcept {
430 return this->ptr;
431}
432
433template <typename T>
434T &Box<T>::operator*() noexcept {
435 return *this->ptr;
436}
437
438template <typename T>
439template <typename... Fields>
440Box<T> Box<T>::in_place(Fields &&... fields) {
441 Box box;
442 box.uninit();
443 ::new (box.ptr) T{std::forward<Fields>(fields)...};
444 return box;
445}
446
447template <typename T>
448Box<T> Box<T>::from_raw(T *raw) noexcept {
449 Box box;
450 box.ptr = raw;
451 return box;
452}
453
454template <typename T>
455T *Box<T>::into_raw() noexcept {
456 T *raw = this->ptr;
457 this->ptr = nullptr;
458 return raw;
459}
460
461template <typename T>
462Box<T>::Box() noexcept {}
463#endif // CXXBRIDGE03_RUST_BOX
464
465#ifndef CXXBRIDGE03_RUST_VEC
466#define CXXBRIDGE03_RUST_VEC
467template <typename T>
468Vec<T>::Vec(Vec &&other) noexcept {
469 this->repr = other.repr;
470 new (&other) Vec();
471}
472
473template <typename T>
474Vec<T>::~Vec() noexcept {
475 this->drop();
476}
477
478template <typename T>
479Vec<T> &Vec<T>::operator=(Vec &&other) noexcept {
480 if (this != &other) {
481 this->drop();
482 this->repr = other.repr;
483 new (&other) Vec();
484 }
485 return *this;
486}
487
488template <typename T>
489bool Vec<T>::empty() const noexcept {
490 return size() == 0;
491}
492
493template <typename T>
Stephen Crane9e48d5b2020-08-21 12:17:02 -0700494const T &Vec<T>::operator[](size_t n) const noexcept {
495 auto data = reinterpret_cast<const char *>(this->data());
496 return *reinterpret_cast<const T *>(data + n * this->stride());
497}
498
499template <typename T>
500const T &Vec<T>::at(size_t n) const {
501 if (n >= this->size())
502 throw_out_of_range("Vec");
503 return (*this)[n];
504}
505
506template <typename T>
507const T &Vec<T>::front() const {
508 return (*this)[0];
509}
510
511template <typename T>
512const T &Vec<T>::back() const {
513 return (*this)[this->size()-1];
514}
515
516template <typename T>
David Tolnay2a2b9ad2020-05-12 20:07:26 -0700517const T &Vec<T>::const_iterator::operator*() const noexcept {
518 return *static_cast<const T *>(this->pos);
519}
520
521template <typename T>
522const T *Vec<T>::const_iterator::operator->() const noexcept {
523 return static_cast<const T *>(this->pos);
524}
525
526template <typename T>
527typename Vec<T>::const_iterator &Vec<T>::const_iterator::operator++() noexcept {
528 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
529 return *this;
530}
531
532template <typename T>
533typename Vec<T>::const_iterator
534Vec<T>::const_iterator::operator++(int) noexcept {
535 auto ret = const_iterator(*this);
536 this->pos = static_cast<const uint8_t *>(this->pos) + this->stride;
537 return ret;
538}
539
540template <typename T>
541bool Vec<T>::const_iterator::operator==(const const_iterator &other) const
542 noexcept {
543 return this->pos == other.pos;
544}
545
546template <typename T>
547bool Vec<T>::const_iterator::operator!=(const const_iterator &other) const
548 noexcept {
549 return this->pos != other.pos;
550}
551
552template <typename T>
553typename Vec<T>::const_iterator Vec<T>::begin() const noexcept {
554 const_iterator it;
555 it.pos = this->data();
556 it.stride = this->stride();
557 return it;
558}
559
560template <typename T>
561typename Vec<T>::const_iterator Vec<T>::end() const noexcept {
562 const_iterator it = this->begin();
563 it.pos = static_cast<const uint8_t *>(it.pos) + it.stride * this->size();
564 return it;
565}
566
567// Internal API only intended for the cxxbridge code generator.
568template <typename T>
569Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {}
570#endif // CXXBRIDGE03_RUST_VEC
571
David Tolnay69601622020-04-29 18:48:36 -0700572} // namespace cxxbridge03
David Tolnay750755e2020-03-01 13:04:08 -0800573} // namespace rust