| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | #include <array> |
| David Tolnay | 30430f1 | 2020-03-19 20:49:00 -0700 | [diff] [blame] | 3 | #include <cstddef> |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 4 | #include <cstdint> |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 5 | #include <exception> |
| David Tolnay | 001102a | 2020-03-01 20:05:04 -0800 | [diff] [blame] | 6 | #include <iosfwd> |
| David Tolnay | 0ecd05a | 2020-07-29 16:32:03 -0700 | [diff] [blame] | 7 | #include <new> |
| Stephen Crane | 9e48d5b | 2020-08-21 12:17:02 -0700 | [diff] [blame^] | 8 | #include <stdexcept> |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 9 | #include <string> |
| David Tolnay | f629237 | 2020-03-01 21:09:11 -0800 | [diff] [blame] | 10 | #include <type_traits> |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 11 | #include <utility> |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 12 | #include <vector> |
| David Tolnay | 59b5ba1 | 2020-04-10 11:32:19 -0700 | [diff] [blame] | 13 | #if defined(_WIN32) |
| 14 | #include <BaseTsd.h> |
| 15 | #endif |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 16 | |
| Stephen Crane | 9e48d5b | 2020-08-21 12:17:02 -0700 | [diff] [blame^] | 17 | #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 Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 37 | namespace rust { |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 38 | inline namespace cxxbridge03 { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 39 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 40 | struct unsafe_bitcopy_t; |
| David Tolnay | d1e2efc | 2020-03-03 22:25:43 -0800 | [diff] [blame] | 41 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 42 | #ifndef CXXBRIDGE03_RUST_STRING |
| 43 | #define CXXBRIDGE03_RUST_STRING |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 44 | class String final { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 45 | public: |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 46 | String() noexcept; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 47 | String(const String &) noexcept; |
| 48 | String(String &&) noexcept; |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 49 | ~String() noexcept; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 50 | |
| 51 | String(const std::string &); |
| 52 | String(const char *); |
| David Tolnay | c2bbd95 | 2020-07-29 18:15:26 -0700 | [diff] [blame] | 53 | String(const char *, size_t); |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 54 | |
| 55 | String &operator=(const String &) noexcept; |
| 56 | String &operator=(String &&) noexcept; |
| 57 | |
| David Tolnay | 404d689 | 2020-03-01 20:19:41 -0800 | [diff] [blame] | 58 | explicit operator std::string() const; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 59 | |
| 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 Tolnay | d1e2efc | 2020-03-03 22:25:43 -0800 | [diff] [blame] | 65 | // Internal API only intended for the cxxbridge code generator. |
| 66 | String(unsafe_bitcopy_t, const String &) noexcept; |
| 67 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 68 | private: |
| 69 | // Size and alignment statically verified by rust_string.rs. |
| 70 | std::array<uintptr_t, 3> repr; |
| 71 | }; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 72 | #endif // CXXBRIDGE03_RUST_STRING |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 73 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 74 | #ifndef CXXBRIDGE03_RUST_STR |
| 75 | #define CXXBRIDGE03_RUST_STR |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 76 | class Str final { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 77 | public: |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 78 | Str() noexcept; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 79 | Str(const Str &) noexcept; |
| 80 | |
| David Tolnay | 851677c | 2020-03-01 23:49:46 -0800 | [diff] [blame] | 81 | Str(const std::string &); |
| 82 | Str(const char *); |
| David Tolnay | 894c5e4 | 2020-07-29 18:20:00 -0700 | [diff] [blame] | 83 | Str(const char *, size_t); |
| David Tolnay | 851677c | 2020-03-01 23:49:46 -0800 | [diff] [blame] | 84 | Str(std::string &&) = delete; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 85 | |
| 86 | Str &operator=(Str) noexcept; |
| 87 | |
| David Tolnay | 404d689 | 2020-03-01 20:19:41 -0800 | [diff] [blame] | 88 | explicit operator std::string() const; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 89 | |
| 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 Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 103 | Str(Repr) noexcept; |
| David Tolnay | baae443 | 2020-03-01 20:20:10 -0800 | [diff] [blame] | 104 | explicit operator Repr() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 105 | |
| 106 | private: |
| 107 | Repr repr; |
| 108 | }; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 109 | #endif // CXXBRIDGE03_RUST_STR |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 110 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 111 | #ifndef CXXBRIDGE03_RUST_SLICE |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 112 | template <typename T> |
| 113 | class Slice final { |
| 114 | public: |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 115 | Slice() noexcept; |
| 116 | Slice(const Slice<T> &) noexcept; |
| 117 | Slice(const T *, size_t count) noexcept; |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 118 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 119 | Slice &operator=(Slice<T>) noexcept; |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 120 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 121 | const T *data() const noexcept; |
| 122 | size_t size() const noexcept; |
| 123 | size_t length() const noexcept; |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 124 | |
| 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 Tolnay | e710af1 | 2020-04-14 16:31:54 -0700 | [diff] [blame] | 129 | // cxx::rust_sliceu8::RustSliceU8 which matches this layout. |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 130 | struct Repr { |
| 131 | const T *ptr; |
| 132 | size_t len; |
| 133 | }; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 134 | Slice(Repr) noexcept; |
| 135 | explicit operator Repr() noexcept; |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 136 | |
| 137 | private: |
| 138 | Repr repr; |
| 139 | }; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 140 | #endif // CXXBRIDGE03_RUST_SLICE |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 141 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 142 | #ifndef CXXBRIDGE03_RUST_BOX |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 143 | template <typename T> |
| 144 | class Box final { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 145 | public: |
| David Tolnay | f629237 | 2020-03-01 21:09:11 -0800 | [diff] [blame] | 146 | using value_type = T; |
| David Tolnay | 9706a51 | 2020-04-24 17:09:01 -0700 | [diff] [blame] | 147 | 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 Tolnay | f629237 | 2020-03-01 21:09:11 -0800 | [diff] [blame] | 150 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 151 | Box(const Box &); |
| 152 | Box(Box &&) noexcept; |
| 153 | ~Box() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 154 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 155 | 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 Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 165 | |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 166 | template <typename... Fields> |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 167 | static Box in_place(Fields &&...); |
| David Tolnay | 7ce59fc | 2020-04-11 11:46:33 -0700 | [diff] [blame] | 168 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 169 | // Important: requires that `raw` came from an into_raw call. Do not pass a |
| 170 | // pointer from `new` or any other source. |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 171 | static Box from_raw(T *) noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 172 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 173 | T *into_raw() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 174 | |
| 175 | private: |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 176 | Box() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 177 | void uninit() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 178 | void drop() noexcept; |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 179 | T *ptr; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 180 | }; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 181 | #endif // CXXBRIDGE03_RUST_BOX |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 182 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 183 | #ifndef CXXBRIDGE03_RUST_VEC |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 184 | template <typename T> |
| 185 | class Vec final { |
| 186 | public: |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 187 | using value_type = T; |
| 188 | |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 189 | Vec() noexcept; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 190 | Vec(Vec &&) noexcept; |
| 191 | ~Vec() noexcept; |
| David Tolnay | cb80057 | 2020-04-24 20:30:43 -0700 | [diff] [blame] | 192 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 193 | Vec &operator=(Vec &&) noexcept; |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 194 | |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 195 | size_t size() const noexcept; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 196 | bool empty() const noexcept; |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 197 | const T *data() const noexcept; |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 198 | |
| Stephen Crane | 9e48d5b | 2020-08-21 12:17:02 -0700 | [diff] [blame^] | 199 | 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 Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 205 | class const_iterator { |
| 206 | public: |
| myronahn | da9be50 | 2020-04-29 05:47:23 +0700 | [diff] [blame] | 207 | using difference_type = ptrdiff_t; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 208 | using value_type = typename std::add_const<T>::type; |
| David Tolnay | 74dd379 | 2020-04-30 07:45:24 -0700 | [diff] [blame] | 209 | using pointer = |
| 210 | typename std::add_pointer<typename std::add_const<T>::type>::type; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 211 | using reference = typename std::add_lvalue_reference< |
| 212 | typename std::add_const<T>::type>::type; |
| myronahn | da9be50 | 2020-04-29 05:47:23 +0700 | [diff] [blame] | 213 | using iterator_category = std::forward_iterator_tag; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 214 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 215 | 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 Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 221 | |
| 222 | private: |
| 223 | friend class Vec; |
| 224 | const void *pos; |
| 225 | size_t stride; |
| 226 | }; |
| 227 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 228 | const_iterator begin() const noexcept; |
| 229 | const_iterator end() const noexcept; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 230 | |
| David Tolnay | 313b10e | 2020-04-25 16:30:51 -0700 | [diff] [blame] | 231 | // Internal API only intended for the cxxbridge code generator. |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 232 | Vec(unsafe_bitcopy_t, const Vec &) noexcept; |
| David Tolnay | 313b10e | 2020-04-25 16:30:51 -0700 | [diff] [blame] | 233 | |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 234 | private: |
| David Tolnay | 503d019 | 2020-04-24 22:18:56 -0700 | [diff] [blame] | 235 | static size_t stride() noexcept; |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 236 | void drop() noexcept; |
| 237 | |
| 238 | // Size and alignment statically verified by rust_vec.rs. |
| 239 | std::array<uintptr_t, 3> repr; |
| 240 | }; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 241 | #endif // CXXBRIDGE03_RUST_VEC |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 242 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 243 | #ifndef CXXBRIDGE03_RUST_FN |
| 244 | #define CXXBRIDGE03_RUST_FN |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 245 | template <typename Signature, bool Throws = false> |
| 246 | class Fn; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 247 | |
| 248 | template <typename Ret, typename... Args, bool Throws> |
| 249 | class Fn<Ret(Args...), Throws> { |
| 250 | public: |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 251 | Ret operator()(Args... args) const noexcept(!Throws); |
| 252 | Fn operator*() const noexcept; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 253 | |
| 254 | private: |
| 255 | Ret (*trampoline)(Args..., void *fn) noexcept(!Throws); |
| 256 | void *fn; |
| 257 | }; |
| 258 | |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 259 | template <typename Signature> |
| 260 | using TryFn = Fn<Signature, true>; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 261 | #endif // CXXBRIDGE03_RUST_FN |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 262 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 263 | #ifndef CXXBRIDGE03_RUST_ERROR |
| 264 | #define CXXBRIDGE03_RUST_ERROR |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 265 | class Error final : std::exception { |
| 266 | public: |
| 267 | Error(const Error &); |
| 268 | Error(Error &&) noexcept; |
| 269 | Error(Str::Repr) noexcept; |
| 270 | ~Error() noexcept; |
| 271 | const char *what() const noexcept override; |
| 272 | |
| 273 | private: |
| 274 | Str::Repr msg; |
| 275 | }; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 276 | #endif // CXXBRIDGE03_RUST_ERROR |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 277 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 278 | #ifndef CXXBRIDGE03_RUST_ISIZE |
| 279 | #define CXXBRIDGE03_RUST_ISIZE |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 280 | #if defined(_WIN32) |
| 281 | using isize = SSIZE_T; |
| 282 | #else |
| 283 | using isize = ssize_t; |
| 284 | #endif |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 285 | #endif // CXXBRIDGE03_RUST_ISIZE |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 286 | |
| David Tolnay | 851677c | 2020-03-01 23:49:46 -0800 | [diff] [blame] | 287 | std::ostream &operator<<(std::ostream &, const String &); |
| 288 | std::ostream &operator<<(std::ostream &, const Str &); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 289 | |
| David Tolnay | 3b0c988 | 2020-03-01 14:08:57 -0800 | [diff] [blame] | 290 | // Snake case aliases for use in code that uses this style for type names. |
| 291 | using string = String; |
| 292 | using str = Str; |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 293 | template <class T> |
| 294 | using box = Box<T>; |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 295 | using error = Error; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 296 | template <typename Signature, bool Throws = false> |
| 297 | using fn = Fn<Signature, Throws>; |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 298 | template <typename Signature> |
| 299 | using try_fn = TryFn<Signature>; |
| David Tolnay | 3b0c988 | 2020-03-01 14:08:57 -0800 | [diff] [blame] | 300 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 301 | |
| 302 | |
| 303 | //////////////////////////////////////////////////////////////////////////////// |
| 304 | /// end public API, begin implementation details |
| 305 | |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 306 | template <typename Ret, typename... Args, bool Throws> |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 307 | Ret Fn<Ret(Args...), Throws>::operator()(Args... args) const noexcept(!Throws) { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 308 | return (*this->trampoline)(std::move(args)..., this->fn); |
| 309 | } |
| 310 | |
| David Tolnay | a23129c | 2020-04-08 20:08:21 -0700 | [diff] [blame] | 311 | template <typename Ret, typename... Args, bool Throws> |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 312 | Fn<Ret(Args...), Throws> Fn<Ret(Args...), Throws>::operator*() const noexcept { |
| David Tolnay | a23129c | 2020-04-08 20:08:21 -0700 | [diff] [blame] | 313 | return *this; |
| 314 | } |
| 315 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 316 | #ifndef CXXBRIDGE03_RUST_BITCOPY |
| 317 | #define CXXBRIDGE03_RUST_BITCOPY |
| 318 | struct unsafe_bitcopy_t { |
| 319 | explicit unsafe_bitcopy_t() = default; |
| 320 | }; |
| 321 | |
| 322 | constexpr unsafe_bitcopy_t unsafe_bitcopy{}; |
| 323 | #endif // CXXBRIDGE03_RUST_BITCOPY |
| 324 | |
| 325 | #ifndef CXXBRIDGE03_RUST_SLICE |
| 326 | #define CXXBRIDGE03_RUST_SLICE |
| 327 | template <typename T> |
| 328 | Slice<T>::Slice() noexcept : repr(Repr{reinterpret_cast<const T *>(this), 0}) {} |
| 329 | |
| 330 | template <typename T> |
| 331 | Slice<T>::Slice(const Slice<T> &) noexcept = default; |
| 332 | |
| 333 | template <typename T> |
| 334 | Slice<T>::Slice(const T *s, size_t count) noexcept : repr(Repr{s, count}) {} |
| 335 | |
| 336 | template <typename T> |
| 337 | Slice<T> &Slice<T>::operator=(Slice<T> other) noexcept { |
| 338 | this->repr = other.repr; |
| 339 | return *this; |
| 340 | } |
| 341 | |
| 342 | template <typename T> |
| 343 | const T *Slice<T>::data() const noexcept { |
| 344 | return this->repr.ptr; |
| 345 | } |
| 346 | |
| 347 | template <typename T> |
| 348 | size_t Slice<T>::size() const noexcept { |
| 349 | return this->repr.len; |
| 350 | } |
| 351 | |
| 352 | template <typename T> |
| 353 | size_t Slice<T>::length() const noexcept { |
| 354 | return this->repr.len; |
| 355 | } |
| 356 | |
| 357 | template <typename T> |
| 358 | Slice<T>::Slice(Repr repr_) noexcept : repr(repr_) {} |
| 359 | |
| 360 | template <typename T> |
| 361 | Slice<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 |
| 368 | template <typename T> |
| 369 | Box<T>::Box(const Box &other) : Box(*other) {} |
| 370 | |
| 371 | template <typename T> |
| 372 | Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) { |
| 373 | other.ptr = nullptr; |
| 374 | } |
| 375 | |
| 376 | template <typename T> |
| 377 | Box<T>::Box(const T &val) { |
| 378 | this->uninit(); |
| 379 | ::new (this->ptr) T(val); |
| 380 | } |
| 381 | |
| 382 | template <typename T> |
| 383 | Box<T>::Box(T &&val) { |
| 384 | this->uninit(); |
| 385 | ::new (this->ptr) T(std::move(val)); |
| 386 | } |
| 387 | |
| 388 | template <typename T> |
| 389 | Box<T>::~Box() noexcept { |
| 390 | if (this->ptr) { |
| 391 | this->drop(); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | template <typename T> |
| 396 | Box<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 | |
| 408 | template <typename T> |
| 409 | Box<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 | |
| 418 | template <typename T> |
| 419 | const T *Box<T>::operator->() const noexcept { |
| 420 | return this->ptr; |
| 421 | } |
| 422 | |
| 423 | template <typename T> |
| 424 | const T &Box<T>::operator*() const noexcept { |
| 425 | return *this->ptr; |
| 426 | } |
| 427 | |
| 428 | template <typename T> |
| 429 | T *Box<T>::operator->() noexcept { |
| 430 | return this->ptr; |
| 431 | } |
| 432 | |
| 433 | template <typename T> |
| 434 | T &Box<T>::operator*() noexcept { |
| 435 | return *this->ptr; |
| 436 | } |
| 437 | |
| 438 | template <typename T> |
| 439 | template <typename... Fields> |
| 440 | Box<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 | |
| 447 | template <typename T> |
| 448 | Box<T> Box<T>::from_raw(T *raw) noexcept { |
| 449 | Box box; |
| 450 | box.ptr = raw; |
| 451 | return box; |
| 452 | } |
| 453 | |
| 454 | template <typename T> |
| 455 | T *Box<T>::into_raw() noexcept { |
| 456 | T *raw = this->ptr; |
| 457 | this->ptr = nullptr; |
| 458 | return raw; |
| 459 | } |
| 460 | |
| 461 | template <typename T> |
| 462 | Box<T>::Box() noexcept {} |
| 463 | #endif // CXXBRIDGE03_RUST_BOX |
| 464 | |
| 465 | #ifndef CXXBRIDGE03_RUST_VEC |
| 466 | #define CXXBRIDGE03_RUST_VEC |
| 467 | template <typename T> |
| 468 | Vec<T>::Vec(Vec &&other) noexcept { |
| 469 | this->repr = other.repr; |
| 470 | new (&other) Vec(); |
| 471 | } |
| 472 | |
| 473 | template <typename T> |
| 474 | Vec<T>::~Vec() noexcept { |
| 475 | this->drop(); |
| 476 | } |
| 477 | |
| 478 | template <typename T> |
| 479 | Vec<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 | |
| 488 | template <typename T> |
| 489 | bool Vec<T>::empty() const noexcept { |
| 490 | return size() == 0; |
| 491 | } |
| 492 | |
| 493 | template <typename T> |
| Stephen Crane | 9e48d5b | 2020-08-21 12:17:02 -0700 | [diff] [blame^] | 494 | const 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 | |
| 499 | template <typename T> |
| 500 | const 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 | |
| 506 | template <typename T> |
| 507 | const T &Vec<T>::front() const { |
| 508 | return (*this)[0]; |
| 509 | } |
| 510 | |
| 511 | template <typename T> |
| 512 | const T &Vec<T>::back() const { |
| 513 | return (*this)[this->size()-1]; |
| 514 | } |
| 515 | |
| 516 | template <typename T> |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 517 | const T &Vec<T>::const_iterator::operator*() const noexcept { |
| 518 | return *static_cast<const T *>(this->pos); |
| 519 | } |
| 520 | |
| 521 | template <typename T> |
| 522 | const T *Vec<T>::const_iterator::operator->() const noexcept { |
| 523 | return static_cast<const T *>(this->pos); |
| 524 | } |
| 525 | |
| 526 | template <typename T> |
| 527 | typename 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 | |
| 532 | template <typename T> |
| 533 | typename Vec<T>::const_iterator |
| 534 | Vec<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 | |
| 540 | template <typename T> |
| 541 | bool Vec<T>::const_iterator::operator==(const const_iterator &other) const |
| 542 | noexcept { |
| 543 | return this->pos == other.pos; |
| 544 | } |
| 545 | |
| 546 | template <typename T> |
| 547 | bool Vec<T>::const_iterator::operator!=(const const_iterator &other) const |
| 548 | noexcept { |
| 549 | return this->pos != other.pos; |
| 550 | } |
| 551 | |
| 552 | template <typename T> |
| 553 | typename 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 | |
| 560 | template <typename T> |
| 561 | typename 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. |
| 568 | template <typename T> |
| 569 | Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {} |
| 570 | #endif // CXXBRIDGE03_RUST_VEC |
| 571 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 572 | } // namespace cxxbridge03 |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 573 | } // namespace rust |