| 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) |
| David Tolnay | da38b7c | 2020-09-16 11:50:04 -0400 | [diff] [blame] | 14 | #include <basetsd.h> |
| David Tolnay | 59b5ba1 | 2020-04-10 11:32:19 -0700 | [diff] [blame] | 15 | #endif |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 16 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 17 | namespace rust { |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 18 | inline namespace cxxbridge1 { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 19 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 20 | struct unsafe_bitcopy_t; |
| David Tolnay | d1e2efc | 2020-03-03 22:25:43 -0800 | [diff] [blame] | 21 | |
| David Tolnay | 84ddf9e | 2020-10-31 15:36:48 -0700 | [diff] [blame] | 22 | namespace { |
| 23 | template <typename T> |
| 24 | class impl; |
| 25 | } |
| 26 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 27 | #ifndef CXXBRIDGE1_RUST_STRING |
| 28 | #define CXXBRIDGE1_RUST_STRING |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 29 | class String final { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 30 | public: |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 31 | String() noexcept; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 32 | String(const String &) noexcept; |
| 33 | String(String &&) noexcept; |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 34 | ~String() noexcept; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 35 | |
| 36 | String(const std::string &); |
| 37 | String(const char *); |
| David Tolnay | c2bbd95 | 2020-07-29 18:15:26 -0700 | [diff] [blame] | 38 | String(const char *, size_t); |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 39 | |
| 40 | String &operator=(const String &) noexcept; |
| 41 | String &operator=(String &&) noexcept; |
| 42 | |
| David Tolnay | 404d689 | 2020-03-01 20:19:41 -0800 | [diff] [blame] | 43 | explicit operator std::string() const; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 44 | |
| 45 | // Note: no null terminator. |
| 46 | const char *data() const noexcept; |
| 47 | size_t size() const noexcept; |
| 48 | size_t length() const noexcept; |
| 49 | |
| David Tolnay | d1e2efc | 2020-03-03 22:25:43 -0800 | [diff] [blame] | 50 | // Internal API only intended for the cxxbridge code generator. |
| 51 | String(unsafe_bitcopy_t, const String &) noexcept; |
| 52 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 53 | private: |
| 54 | // Size and alignment statically verified by rust_string.rs. |
| 55 | std::array<uintptr_t, 3> repr; |
| 56 | }; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 57 | #endif // CXXBRIDGE1_RUST_STRING |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 58 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 59 | #ifndef CXXBRIDGE1_RUST_STR |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 60 | class Str final { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 61 | public: |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 62 | Str() noexcept; |
| David Tolnay | 851677c | 2020-03-01 23:49:46 -0800 | [diff] [blame] | 63 | Str(const std::string &); |
| 64 | Str(const char *); |
| David Tolnay | 894c5e4 | 2020-07-29 18:20:00 -0700 | [diff] [blame] | 65 | Str(const char *, size_t); |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 66 | |
| David Tolnay | 2d7f117 | 2020-10-31 17:58:31 -0700 | [diff] [blame] | 67 | Str &operator=(const Str &) noexcept = default; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 68 | |
| David Tolnay | 404d689 | 2020-03-01 20:19:41 -0800 | [diff] [blame] | 69 | explicit operator std::string() const; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 70 | |
| 71 | // Note: no null terminator. |
| 72 | const char *data() const noexcept; |
| 73 | size_t size() const noexcept; |
| 74 | size_t length() const noexcept; |
| 75 | |
| David Tolnay | de9a5b1 | 2020-10-31 12:15:43 -0700 | [diff] [blame] | 76 | // Important in order for System V ABI to pass in registers. |
| 77 | Str(const Str &) noexcept = default; |
| 78 | ~Str() noexcept = default; |
| 79 | |
| David Tolnay | 5df1f06 | 2020-10-31 12:31:10 -0700 | [diff] [blame] | 80 | private: |
| David Tolnay | 0356d33 | 2020-10-31 19:46:41 -0700 | [diff] [blame] | 81 | friend impl<Str>; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 82 | // Not necessarily ABI compatible with &str. Codegen will translate to |
| 83 | // cxx::rust_str::RustStr which matches this layout. |
| David Tolnay | 5df1f06 | 2020-10-31 12:31:10 -0700 | [diff] [blame] | 84 | const char *ptr; |
| 85 | size_t len; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 86 | }; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 87 | #endif // CXXBRIDGE1_RUST_STR |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 88 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 89 | #ifndef CXXBRIDGE1_RUST_SLICE |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 90 | template <typename T> |
| 91 | class Slice final { |
| David Tolnay | ce29823 | 2020-11-11 10:08:54 -0800 | [diff] [blame] | 92 | static_assert(std::is_const<T>::value, |
| 93 | "&[T] needs to be written as rust::Slice<const T> in C++"); |
| 94 | |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 95 | public: |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 96 | Slice() noexcept; |
| David Tolnay | ce29823 | 2020-11-11 10:08:54 -0800 | [diff] [blame] | 97 | Slice(T *, size_t count) noexcept; |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 98 | |
| David Tolnay | 2d7f117 | 2020-10-31 17:58:31 -0700 | [diff] [blame] | 99 | Slice &operator=(const Slice<T> &) noexcept = default; |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 100 | |
| David Tolnay | ce29823 | 2020-11-11 10:08:54 -0800 | [diff] [blame] | 101 | T *data() const noexcept; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 102 | size_t size() const noexcept; |
| 103 | size_t length() const noexcept; |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 104 | |
| David Tolnay | de9a5b1 | 2020-10-31 12:15:43 -0700 | [diff] [blame] | 105 | // Important in order for System V ABI to pass in registers. |
| 106 | Slice(const Slice<T> &) noexcept = default; |
| 107 | ~Slice() noexcept = default; |
| 108 | |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 109 | private: |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 110 | friend impl<Slice>; |
| 111 | // Not necessarily ABI compatible with &[T]. Codegen will translate to |
| 112 | // cxx::rust_sliceu8::RustSliceU8 which matches this layout. |
| David Tolnay | ce29823 | 2020-11-11 10:08:54 -0800 | [diff] [blame] | 113 | T *ptr; |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 114 | size_t len; |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 115 | }; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 116 | #endif // CXXBRIDGE1_RUST_SLICE |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 117 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 118 | #ifndef CXXBRIDGE1_RUST_BOX |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 119 | template <typename T> |
| 120 | class Box final { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 121 | public: |
| David Tolnay | f629237 | 2020-03-01 21:09:11 -0800 | [diff] [blame] | 122 | using value_type = T; |
| David Tolnay | 9706a51 | 2020-04-24 17:09:01 -0700 | [diff] [blame] | 123 | using const_pointer = |
| 124 | typename std::add_pointer<typename std::add_const<T>::type>::type; |
| 125 | using pointer = typename std::add_pointer<T>::type; |
| David Tolnay | f629237 | 2020-03-01 21:09:11 -0800 | [diff] [blame] | 126 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 127 | Box(const Box &); |
| 128 | Box(Box &&) noexcept; |
| 129 | ~Box() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 130 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 131 | explicit Box(const T &); |
| 132 | explicit Box(T &&); |
| 133 | |
| 134 | Box &operator=(const Box &); |
| 135 | Box &operator=(Box &&) noexcept; |
| 136 | |
| 137 | const T *operator->() const noexcept; |
| 138 | const T &operator*() const noexcept; |
| 139 | T *operator->() noexcept; |
| 140 | T &operator*() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 141 | |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 142 | template <typename... Fields> |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 143 | static Box in_place(Fields &&...); |
| David Tolnay | 7ce59fc | 2020-04-11 11:46:33 -0700 | [diff] [blame] | 144 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 145 | // Important: requires that `raw` came from an into_raw call. Do not pass a |
| 146 | // pointer from `new` or any other source. |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 147 | static Box from_raw(T *) noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 148 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 149 | T *into_raw() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 150 | |
| 151 | private: |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 152 | Box() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 153 | void uninit() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 154 | void drop() noexcept; |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 155 | T *ptr; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 156 | }; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 157 | #endif // CXXBRIDGE1_RUST_BOX |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 158 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 159 | #ifndef CXXBRIDGE1_RUST_VEC |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 160 | template <typename T> |
| 161 | class Vec final { |
| 162 | public: |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 163 | using value_type = T; |
| 164 | |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 165 | Vec() noexcept; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 166 | Vec(Vec &&) noexcept; |
| 167 | ~Vec() noexcept; |
| David Tolnay | cb80057 | 2020-04-24 20:30:43 -0700 | [diff] [blame] | 168 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 169 | Vec &operator=(Vec &&) noexcept; |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 170 | |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 171 | size_t size() const noexcept; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 172 | bool empty() const noexcept; |
| David Tolnay | 219c079 | 2020-04-24 20:31:37 -0700 | [diff] [blame] | 173 | const T *data() const noexcept; |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 174 | T *data() noexcept; |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 175 | |
| Stephen Crane | 9e48d5b | 2020-08-21 12:17:02 -0700 | [diff] [blame] | 176 | const T &operator[](size_t n) const noexcept; |
| 177 | const T &at(size_t n) const; |
| 178 | |
| 179 | const T &front() const; |
| 180 | const T &back() const; |
| 181 | |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 182 | void reserve(size_t new_cap); |
| 183 | void push_back(const T &value); |
| 184 | void push_back(T &&value); |
| David Tolnay | 4e8c49a | 2020-11-11 10:00:18 -0800 | [diff] [blame] | 185 | template <typename... Args> |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 186 | void emplace_back(Args &&... args); |
| 187 | |
| David Tolnay | 4852122 | 2020-10-31 14:59:42 -0700 | [diff] [blame] | 188 | class const_iterator final { |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 189 | public: |
| myronahn | da9be50 | 2020-04-29 05:47:23 +0700 | [diff] [blame] | 190 | using difference_type = ptrdiff_t; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 191 | using value_type = typename std::add_const<T>::type; |
| David Tolnay | 74dd379 | 2020-04-30 07:45:24 -0700 | [diff] [blame] | 192 | using pointer = |
| 193 | typename std::add_pointer<typename std::add_const<T>::type>::type; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 194 | using reference = typename std::add_lvalue_reference< |
| 195 | typename std::add_const<T>::type>::type; |
| myronahn | da9be50 | 2020-04-29 05:47:23 +0700 | [diff] [blame] | 196 | using iterator_category = std::forward_iterator_tag; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 197 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 198 | const T &operator*() const noexcept; |
| 199 | const T *operator->() const noexcept; |
| 200 | const_iterator &operator++() noexcept; |
| 201 | const_iterator operator++(int) noexcept; |
| 202 | bool operator==(const const_iterator &) const noexcept; |
| 203 | bool operator!=(const const_iterator &) const noexcept; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 204 | |
| 205 | private: |
| 206 | friend class Vec; |
| 207 | const void *pos; |
| 208 | size_t stride; |
| 209 | }; |
| 210 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 211 | const_iterator begin() const noexcept; |
| 212 | const_iterator end() const noexcept; |
| David Tolnay | c87c215 | 2020-04-24 17:07:41 -0700 | [diff] [blame] | 213 | |
| David Tolnay | 313b10e | 2020-04-25 16:30:51 -0700 | [diff] [blame] | 214 | // Internal API only intended for the cxxbridge code generator. |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 215 | Vec(unsafe_bitcopy_t, const Vec &) noexcept; |
| David Tolnay | 313b10e | 2020-04-25 16:30:51 -0700 | [diff] [blame] | 216 | |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 217 | private: |
| David Tolnay | 503d019 | 2020-04-24 22:18:56 -0700 | [diff] [blame] | 218 | static size_t stride() noexcept; |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 219 | void reserve_total(size_t cap) noexcept; |
| 220 | void set_len(size_t len) noexcept; |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 221 | void drop() noexcept; |
| 222 | |
| 223 | // Size and alignment statically verified by rust_vec.rs. |
| 224 | std::array<uintptr_t, 3> repr; |
| 225 | }; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 226 | #endif // CXXBRIDGE1_RUST_VEC |
| David Tolnay | 7f2dc3b | 2020-04-24 16:46:39 -0700 | [diff] [blame] | 227 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 228 | #ifndef CXXBRIDGE1_RUST_FN |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 229 | template <typename Signature, bool Throws = false> |
| 230 | class Fn; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 231 | |
| 232 | template <typename Ret, typename... Args, bool Throws> |
| David Tolnay | 4852122 | 2020-10-31 14:59:42 -0700 | [diff] [blame] | 233 | class Fn<Ret(Args...), Throws> final { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 234 | public: |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 235 | Ret operator()(Args... args) const noexcept(!Throws); |
| 236 | Fn operator*() const noexcept; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 237 | |
| 238 | private: |
| 239 | Ret (*trampoline)(Args..., void *fn) noexcept(!Throws); |
| 240 | void *fn; |
| 241 | }; |
| 242 | |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 243 | template <typename Signature> |
| 244 | using TryFn = Fn<Signature, true>; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 245 | #endif // CXXBRIDGE1_RUST_FN |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 246 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 247 | #ifndef CXXBRIDGE1_RUST_ERROR |
| 248 | #define CXXBRIDGE1_RUST_ERROR |
| David Tolnay | e4fa873 | 2020-09-08 15:04:56 -0700 | [diff] [blame] | 249 | class Error final : public std::exception { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 250 | public: |
| 251 | Error(const Error &); |
| 252 | Error(Error &&) noexcept; |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 253 | ~Error() noexcept; |
| David Tolnay | 7c6ac71 | 2020-10-31 17:22:28 -0700 | [diff] [blame] | 254 | |
| 255 | Error &operator=(const Error &); |
| David Tolnay | 1549106 | 2020-10-31 17:25:13 -0700 | [diff] [blame] | 256 | Error &operator=(Error &&) noexcept; |
| David Tolnay | 7c6ac71 | 2020-10-31 17:22:28 -0700 | [diff] [blame] | 257 | |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 258 | const char *what() const noexcept override; |
| 259 | |
| 260 | private: |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 261 | Error() noexcept = default; |
| David Tolnay | 84ddf9e | 2020-10-31 15:36:48 -0700 | [diff] [blame] | 262 | friend impl<Error>; |
| David Tolnay | a0c9bc7 | 2020-10-31 14:37:14 -0700 | [diff] [blame] | 263 | const char *msg; |
| 264 | size_t len; |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 265 | }; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 266 | #endif // CXXBRIDGE1_RUST_ERROR |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 267 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 268 | #ifndef CXXBRIDGE1_RUST_ISIZE |
| 269 | #define CXXBRIDGE1_RUST_ISIZE |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 270 | #if defined(_WIN32) |
| 271 | using isize = SSIZE_T; |
| 272 | #else |
| 273 | using isize = ssize_t; |
| 274 | #endif |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 275 | #endif // CXXBRIDGE1_RUST_ISIZE |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 276 | |
| David Tolnay | 851677c | 2020-03-01 23:49:46 -0800 | [diff] [blame] | 277 | std::ostream &operator<<(std::ostream &, const String &); |
| 278 | std::ostream &operator<<(std::ostream &, const Str &); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 279 | |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame] | 280 | // IsRelocatable<T> is used in assertions that a C++ type passed by value |
| 281 | // between Rust and C++ is soundly relocatable by Rust. |
| 282 | // |
| 283 | // There may be legitimate reasons to opt out of the check for support of types |
| 284 | // that the programmer knows are soundly Rust-movable despite not being |
| 285 | // recognized as such by the C++ type system due to a move constructor or |
| 286 | // destructor. To opt out of the relocatability check, do either of the |
| 287 | // following things in any header used by `include!` in the bridge. |
| 288 | // |
| 289 | // --- if you define the type: |
| 290 | // struct MyType { |
| 291 | // ... |
| 292 | // + using IsRelocatable = std::true_type; |
| 293 | // }; |
| 294 | // |
| 295 | // --- otherwise: |
| 296 | // + template <> |
| 297 | // + struct rust::IsRelocatable<MyType> : std::true_type {}; |
| 298 | template <typename T> |
| 299 | struct IsRelocatable; |
| 300 | |
| David Tolnay | 3b0c988 | 2020-03-01 14:08:57 -0800 | [diff] [blame] | 301 | // Snake case aliases for use in code that uses this style for type names. |
| 302 | using string = String; |
| 303 | using str = Str; |
| David Tolnay | 4e8c49a | 2020-11-11 10:00:18 -0800 | [diff] [blame] | 304 | template <typename T> |
| David Tolnay | 38c8764 | 2020-09-06 22:18:08 -0700 | [diff] [blame] | 305 | using slice = Slice<T>; |
| David Tolnay | 4e8c49a | 2020-11-11 10:00:18 -0800 | [diff] [blame] | 306 | template <typename T> |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 307 | using box = Box<T>; |
| David Tolnay | 4e8c49a | 2020-11-11 10:00:18 -0800 | [diff] [blame] | 308 | template <typename T> |
| David Tolnay | 38c8764 | 2020-09-06 22:18:08 -0700 | [diff] [blame] | 309 | using vec = Vec<T>; |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 310 | using error = Error; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 311 | template <typename Signature, bool Throws = false> |
| 312 | using fn = Fn<Signature, Throws>; |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 313 | template <typename Signature> |
| 314 | using try_fn = TryFn<Signature>; |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame] | 315 | template <typename T> |
| 316 | using is_relocatable = IsRelocatable<T>; |
| David Tolnay | 3b0c988 | 2020-03-01 14:08:57 -0800 | [diff] [blame] | 317 | |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 318 | |
| 319 | |
| 320 | //////////////////////////////////////////////////////////////////////////////// |
| 321 | /// end public API, begin implementation details |
| 322 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 323 | #ifndef CXXBRIDGE1_PANIC |
| 324 | #define CXXBRIDGE1_PANIC |
| David Tolnay | 521d99d | 2020-08-26 20:45:40 -0700 | [diff] [blame] | 325 | template <typename Exception> |
| 326 | void panic [[noreturn]] (const char *msg); |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 327 | #endif // CXXBRIDGE1_PANIC |
| David Tolnay | 521d99d | 2020-08-26 20:45:40 -0700 | [diff] [blame] | 328 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 329 | #ifndef CXXBRIDGE1_RUST_FN |
| 330 | #define CXXBRIDGE1_RUST_FN |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 331 | template <typename Ret, typename... Args, bool Throws> |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 332 | Ret Fn<Ret(Args...), Throws>::operator()(Args... args) const noexcept(!Throws) { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 333 | return (*this->trampoline)(std::move(args)..., this->fn); |
| 334 | } |
| 335 | |
| David Tolnay | a23129c | 2020-04-08 20:08:21 -0700 | [diff] [blame] | 336 | template <typename Ret, typename... Args, bool Throws> |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 337 | Fn<Ret(Args...), Throws> Fn<Ret(Args...), Throws>::operator*() const noexcept { |
| David Tolnay | a23129c | 2020-04-08 20:08:21 -0700 | [diff] [blame] | 338 | return *this; |
| 339 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 340 | #endif // CXXBRIDGE1_RUST_FN |
| David Tolnay | a23129c | 2020-04-08 20:08:21 -0700 | [diff] [blame] | 341 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 342 | #ifndef CXXBRIDGE1_RUST_BITCOPY |
| 343 | #define CXXBRIDGE1_RUST_BITCOPY |
| David Tolnay | 4852122 | 2020-10-31 14:59:42 -0700 | [diff] [blame] | 344 | struct unsafe_bitcopy_t final { |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 345 | explicit unsafe_bitcopy_t() = default; |
| 346 | }; |
| 347 | |
| 348 | constexpr unsafe_bitcopy_t unsafe_bitcopy{}; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 349 | #endif // CXXBRIDGE1_RUST_BITCOPY |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 350 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 351 | #ifndef CXXBRIDGE1_RUST_STR |
| 352 | #define CXXBRIDGE1_RUST_STR |
| David Tolnay | 5b1ee1f | 2020-10-31 18:21:39 -0700 | [diff] [blame] | 353 | inline const char *Str::data() const noexcept { return this->ptr; } |
| 354 | |
| 355 | inline size_t Str::size() const noexcept { return this->len; } |
| 356 | |
| 357 | inline size_t Str::length() const noexcept { return this->len; } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 358 | #endif // CXXBRIDGE1_RUST_STR |
| David Tolnay | 5b1ee1f | 2020-10-31 18:21:39 -0700 | [diff] [blame] | 359 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 360 | #ifndef CXXBRIDGE1_RUST_SLICE |
| 361 | #define CXXBRIDGE1_RUST_SLICE |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 362 | template <typename T> |
| David Tolnay | ce29823 | 2020-11-11 10:08:54 -0800 | [diff] [blame] | 363 | Slice<T>::Slice() noexcept : ptr(reinterpret_cast<T *>(this)), len(0) {} |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 364 | |
| 365 | template <typename T> |
| David Tolnay | ce29823 | 2020-11-11 10:08:54 -0800 | [diff] [blame] | 366 | Slice<T>::Slice(T *s, size_t count) noexcept : ptr(s), len(count) {} |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 367 | |
| 368 | template <typename T> |
| David Tolnay | ce29823 | 2020-11-11 10:08:54 -0800 | [diff] [blame] | 369 | T *Slice<T>::data() const noexcept { |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 370 | return this->ptr; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | template <typename T> |
| 374 | size_t Slice<T>::size() const noexcept { |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 375 | return this->len; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | template <typename T> |
| 379 | size_t Slice<T>::length() const noexcept { |
| David Tolnay | 36aa9e0 | 2020-10-31 23:08:21 -0700 | [diff] [blame] | 380 | return this->len; |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 381 | } |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 382 | #endif // CXXBRIDGE1_RUST_SLICE |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 383 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 384 | #ifndef CXXBRIDGE1_RUST_BOX |
| 385 | #define CXXBRIDGE1_RUST_BOX |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 386 | template <typename T> |
| 387 | Box<T>::Box(const Box &other) : Box(*other) {} |
| 388 | |
| 389 | template <typename T> |
| 390 | Box<T>::Box(Box &&other) noexcept : ptr(other.ptr) { |
| 391 | other.ptr = nullptr; |
| 392 | } |
| 393 | |
| 394 | template <typename T> |
| 395 | Box<T>::Box(const T &val) { |
| 396 | this->uninit(); |
| 397 | ::new (this->ptr) T(val); |
| 398 | } |
| 399 | |
| 400 | template <typename T> |
| 401 | Box<T>::Box(T &&val) { |
| 402 | this->uninit(); |
| 403 | ::new (this->ptr) T(std::move(val)); |
| 404 | } |
| 405 | |
| 406 | template <typename T> |
| 407 | Box<T>::~Box() noexcept { |
| 408 | if (this->ptr) { |
| 409 | this->drop(); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | template <typename T> |
| 414 | Box<T> &Box<T>::operator=(const Box &other) { |
| 415 | if (this != &other) { |
| 416 | if (this->ptr) { |
| 417 | **this = *other; |
| 418 | } else { |
| 419 | this->uninit(); |
| 420 | ::new (this->ptr) T(*other); |
| 421 | } |
| 422 | } |
| 423 | return *this; |
| 424 | } |
| 425 | |
| 426 | template <typename T> |
| 427 | Box<T> &Box<T>::operator=(Box &&other) noexcept { |
| 428 | if (this->ptr) { |
| 429 | this->drop(); |
| 430 | } |
| 431 | this->ptr = other.ptr; |
| 432 | other.ptr = nullptr; |
| 433 | return *this; |
| 434 | } |
| 435 | |
| 436 | template <typename T> |
| 437 | const T *Box<T>::operator->() const noexcept { |
| 438 | return this->ptr; |
| 439 | } |
| 440 | |
| 441 | template <typename T> |
| 442 | const T &Box<T>::operator*() const noexcept { |
| 443 | return *this->ptr; |
| 444 | } |
| 445 | |
| 446 | template <typename T> |
| 447 | T *Box<T>::operator->() noexcept { |
| 448 | return this->ptr; |
| 449 | } |
| 450 | |
| 451 | template <typename T> |
| 452 | T &Box<T>::operator*() noexcept { |
| 453 | return *this->ptr; |
| 454 | } |
| 455 | |
| 456 | template <typename T> |
| 457 | template <typename... Fields> |
| 458 | Box<T> Box<T>::in_place(Fields &&... fields) { |
| 459 | Box box; |
| 460 | box.uninit(); |
| 461 | ::new (box.ptr) T{std::forward<Fields>(fields)...}; |
| 462 | return box; |
| 463 | } |
| 464 | |
| 465 | template <typename T> |
| 466 | Box<T> Box<T>::from_raw(T *raw) noexcept { |
| 467 | Box box; |
| 468 | box.ptr = raw; |
| 469 | return box; |
| 470 | } |
| 471 | |
| 472 | template <typename T> |
| 473 | T *Box<T>::into_raw() noexcept { |
| 474 | T *raw = this->ptr; |
| 475 | this->ptr = nullptr; |
| 476 | return raw; |
| 477 | } |
| 478 | |
| 479 | template <typename T> |
| 480 | Box<T>::Box() noexcept {} |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 481 | #endif // CXXBRIDGE1_RUST_BOX |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 482 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 483 | #ifndef CXXBRIDGE1_RUST_VEC |
| 484 | #define CXXBRIDGE1_RUST_VEC |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 485 | template <typename T> |
| David Tolnay | 1567186 | 2020-11-23 18:13:56 -0800 | [diff] [blame^] | 486 | Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) { |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 487 | new (&other) Vec(); |
| 488 | } |
| 489 | |
| 490 | template <typename T> |
| 491 | Vec<T>::~Vec() noexcept { |
| 492 | this->drop(); |
| 493 | } |
| 494 | |
| 495 | template <typename T> |
| 496 | Vec<T> &Vec<T>::operator=(Vec &&other) noexcept { |
| 497 | if (this != &other) { |
| 498 | this->drop(); |
| 499 | this->repr = other.repr; |
| 500 | new (&other) Vec(); |
| 501 | } |
| 502 | return *this; |
| 503 | } |
| 504 | |
| 505 | template <typename T> |
| 506 | bool Vec<T>::empty() const noexcept { |
| 507 | return size() == 0; |
| 508 | } |
| 509 | |
| 510 | template <typename T> |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 511 | T *Vec<T>::data() noexcept { |
| 512 | return const_cast<T *>(const_cast<const Vec<T> *>(this)->data()); |
| 513 | } |
| 514 | |
| 515 | template <typename T> |
| Stephen Crane | 9e48d5b | 2020-08-21 12:17:02 -0700 | [diff] [blame] | 516 | const T &Vec<T>::operator[](size_t n) const noexcept { |
| 517 | auto data = reinterpret_cast<const char *>(this->data()); |
| 518 | return *reinterpret_cast<const T *>(data + n * this->stride()); |
| 519 | } |
| 520 | |
| 521 | template <typename T> |
| 522 | const T &Vec<T>::at(size_t n) const { |
| David Tolnay | 8e1e6ac | 2020-08-26 20:51:43 -0700 | [diff] [blame] | 523 | if (n >= this->size()) { |
| 524 | panic<std::out_of_range>("rust::Vec index out of range"); |
| 525 | } |
| Stephen Crane | 9e48d5b | 2020-08-21 12:17:02 -0700 | [diff] [blame] | 526 | return (*this)[n]; |
| 527 | } |
| 528 | |
| 529 | template <typename T> |
| 530 | const T &Vec<T>::front() const { |
| 531 | return (*this)[0]; |
| 532 | } |
| 533 | |
| 534 | template <typename T> |
| 535 | const T &Vec<T>::back() const { |
| David Tolnay | b10c4bc | 2020-08-26 21:55:29 -0700 | [diff] [blame] | 536 | return (*this)[this->size() - 1]; |
| Stephen Crane | 9e48d5b | 2020-08-21 12:17:02 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | template <typename T> |
| David Tolnay | fb6b73c | 2020-11-10 14:32:16 -0800 | [diff] [blame] | 540 | void Vec<T>::reserve(size_t new_cap) { |
| 541 | this->reserve_total(new_cap); |
| 542 | } |
| 543 | |
| 544 | template <typename T> |
| 545 | void Vec<T>::push_back(const T &value) { |
| 546 | this->emplace_back(value); |
| 547 | } |
| 548 | |
| 549 | template <typename T> |
| 550 | void Vec<T>::push_back(T &&value) { |
| 551 | this->emplace_back(std::move(value)); |
| 552 | } |
| 553 | |
| 554 | template <typename T> |
| 555 | template <typename... Args> |
| 556 | void Vec<T>::emplace_back(Args &&... args) { |
| 557 | auto size = this->size(); |
| 558 | this->reserve_total(size + 1); |
| 559 | ::new (reinterpret_cast<T *>(reinterpret_cast<char *>(this->data()) + |
| 560 | size * this->stride())) |
| 561 | T(std::forward<Args>(args)...); |
| 562 | this->set_len(size + 1); |
| 563 | } |
| 564 | |
| 565 | template <typename T> |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 566 | const T &Vec<T>::const_iterator::operator*() const noexcept { |
| 567 | return *static_cast<const T *>(this->pos); |
| 568 | } |
| 569 | |
| 570 | template <typename T> |
| 571 | const T *Vec<T>::const_iterator::operator->() const noexcept { |
| 572 | return static_cast<const T *>(this->pos); |
| 573 | } |
| 574 | |
| 575 | template <typename T> |
| 576 | typename Vec<T>::const_iterator &Vec<T>::const_iterator::operator++() noexcept { |
| 577 | this->pos = static_cast<const uint8_t *>(this->pos) + this->stride; |
| 578 | return *this; |
| 579 | } |
| 580 | |
| 581 | template <typename T> |
| 582 | typename Vec<T>::const_iterator |
| 583 | Vec<T>::const_iterator::operator++(int) noexcept { |
| 584 | auto ret = const_iterator(*this); |
| 585 | this->pos = static_cast<const uint8_t *>(this->pos) + this->stride; |
| 586 | return ret; |
| 587 | } |
| 588 | |
| 589 | template <typename T> |
| David Tolnay | b10c4bc | 2020-08-26 21:55:29 -0700 | [diff] [blame] | 590 | bool Vec<T>::const_iterator::operator==( |
| 591 | const const_iterator &other) const noexcept { |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 592 | return this->pos == other.pos; |
| 593 | } |
| 594 | |
| 595 | template <typename T> |
| David Tolnay | b10c4bc | 2020-08-26 21:55:29 -0700 | [diff] [blame] | 596 | bool Vec<T>::const_iterator::operator!=( |
| 597 | const const_iterator &other) const noexcept { |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 598 | return this->pos != other.pos; |
| 599 | } |
| 600 | |
| 601 | template <typename T> |
| 602 | typename Vec<T>::const_iterator Vec<T>::begin() const noexcept { |
| 603 | const_iterator it; |
| 604 | it.pos = this->data(); |
| 605 | it.stride = this->stride(); |
| 606 | return it; |
| 607 | } |
| 608 | |
| 609 | template <typename T> |
| 610 | typename Vec<T>::const_iterator Vec<T>::end() const noexcept { |
| 611 | const_iterator it = this->begin(); |
| 612 | it.pos = static_cast<const uint8_t *>(it.pos) + it.stride * this->size(); |
| 613 | return it; |
| 614 | } |
| 615 | |
| 616 | // Internal API only intended for the cxxbridge code generator. |
| 617 | template <typename T> |
| 618 | Vec<T>::Vec(unsafe_bitcopy_t, const Vec &bits) noexcept : repr(bits.repr) {} |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 619 | #endif // CXXBRIDGE1_RUST_VEC |
| David Tolnay | 2a2b9ad | 2020-05-12 20:07:26 -0700 | [diff] [blame] | 620 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 621 | #ifndef CXXBRIDGE1_RELOCATABLE |
| 622 | #define CXXBRIDGE1_RELOCATABLE |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame] | 623 | namespace detail { |
| 624 | template <typename... Ts> |
| 625 | struct make_void { |
| 626 | using type = void; |
| 627 | }; |
| 628 | |
| 629 | template <typename... Ts> |
| 630 | using void_t = typename make_void<Ts...>::type; |
| 631 | |
| 632 | template <typename Void, template <typename...> class, typename...> |
| 633 | struct detect : std::false_type {}; |
| 634 | template <template <typename...> class T, typename... A> |
| 635 | struct detect<void_t<T<A...>>, T, A...> : std::true_type {}; |
| 636 | |
| 637 | template <template <typename...> class T, typename... A> |
| 638 | using is_detected = detect<void, T, A...>; |
| 639 | |
| 640 | template <typename T> |
| 641 | using detect_IsRelocatable = typename T::IsRelocatable; |
| 642 | |
| 643 | template <typename T> |
| 644 | struct get_IsRelocatable |
| 645 | : std::is_same<typename T::IsRelocatable, std::true_type> {}; |
| 646 | } // namespace detail |
| 647 | |
| 648 | template <typename T> |
| 649 | struct IsRelocatable |
| 650 | : std::conditional< |
| 651 | detail::is_detected<detail::detect_IsRelocatable, T>::value, |
| 652 | detail::get_IsRelocatable<T>, |
| 653 | std::integral_constant< |
| 654 | bool, std::is_trivially_move_constructible<T>::value && |
| 655 | std::is_trivially_destructible<T>::value>>::type {}; |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 656 | #endif // CXXBRIDGE1_RELOCATABLE |
| David Tolnay | 174bd95 | 2020-11-02 09:23:12 -0800 | [diff] [blame] | 657 | |
| David Tolnay | 0f0162f | 2020-11-16 23:43:37 -0800 | [diff] [blame] | 658 | } // namespace cxxbridge1 |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 659 | } // namespace rust |