| 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 | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 7 | #include <string> |
| David Tolnay | f629237 | 2020-03-01 21:09:11 -0800 | [diff] [blame] | 8 | #include <type_traits> |
| David Tolnay | 4791f1c | 2020-03-17 21:53:16 -0700 | [diff] [blame] | 9 | #include <utility> |
| David Tolnay | 59b5ba1 | 2020-04-10 11:32:19 -0700 | [diff] [blame] | 10 | #if defined(_WIN32) |
| 11 | #include <BaseTsd.h> |
| 12 | #endif |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 13 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 14 | namespace rust { |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 15 | inline namespace cxxbridge02 { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 16 | |
| David Tolnay | d1e2efc | 2020-03-03 22:25:43 -0800 | [diff] [blame] | 17 | struct unsafe_bitcopy_t; |
| 18 | |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 19 | #ifndef CXXBRIDGE02_RUST_STRING |
| 20 | #define CXXBRIDGE02_RUST_STRING |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 21 | class String final { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 22 | public: |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 23 | String() noexcept; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 24 | String(const String &) noexcept; |
| 25 | String(String &&) noexcept; |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 26 | ~String() noexcept; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 27 | |
| 28 | String(const std::string &); |
| 29 | String(const char *); |
| 30 | |
| 31 | String &operator=(const String &) noexcept; |
| 32 | String &operator=(String &&) noexcept; |
| 33 | |
| David Tolnay | 404d689 | 2020-03-01 20:19:41 -0800 | [diff] [blame] | 34 | explicit operator std::string() const; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 35 | |
| 36 | // Note: no null terminator. |
| 37 | const char *data() const noexcept; |
| 38 | size_t size() const noexcept; |
| 39 | size_t length() const noexcept; |
| 40 | |
| David Tolnay | d1e2efc | 2020-03-03 22:25:43 -0800 | [diff] [blame] | 41 | // Internal API only intended for the cxxbridge code generator. |
| 42 | String(unsafe_bitcopy_t, const String &) noexcept; |
| 43 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 44 | private: |
| 45 | // Size and alignment statically verified by rust_string.rs. |
| 46 | std::array<uintptr_t, 3> repr; |
| 47 | }; |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 48 | #endif // CXXBRIDGE02_RUST_STRING |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 49 | |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 50 | #ifndef CXXBRIDGE02_RUST_STR |
| 51 | #define CXXBRIDGE02_RUST_STR |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 52 | class Str final { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 53 | public: |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 54 | Str() noexcept; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 55 | Str(const Str &) noexcept; |
| 56 | |
| David Tolnay | 851677c | 2020-03-01 23:49:46 -0800 | [diff] [blame] | 57 | Str(const std::string &); |
| 58 | Str(const char *); |
| 59 | Str(std::string &&) = delete; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 60 | |
| 61 | Str &operator=(Str) noexcept; |
| 62 | |
| David Tolnay | 404d689 | 2020-03-01 20:19:41 -0800 | [diff] [blame] | 63 | explicit operator std::string() const; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 64 | |
| 65 | // Note: no null terminator. |
| 66 | const char *data() const noexcept; |
| 67 | size_t size() const noexcept; |
| 68 | size_t length() const noexcept; |
| 69 | |
| 70 | // Repr is PRIVATE; must not be used other than by our generated code. |
| 71 | // |
| 72 | // Not necessarily ABI compatible with &str. Codegen will translate to |
| 73 | // cxx::rust_str::RustStr which matches this layout. |
| 74 | struct Repr { |
| 75 | const char *ptr; |
| 76 | size_t len; |
| 77 | }; |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 78 | Str(Repr) noexcept; |
| David Tolnay | baae443 | 2020-03-01 20:20:10 -0800 | [diff] [blame] | 79 | explicit operator Repr() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 80 | |
| 81 | private: |
| 82 | Repr repr; |
| 83 | }; |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 84 | #endif // CXXBRIDGE02_RUST_STR |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 85 | |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 86 | #ifndef CXXBRIDGE02_RUST_SLICE |
| 87 | #define CXXBRIDGE02_RUST_SLICE |
| 88 | template <typename T> |
| 89 | class Slice final { |
| 90 | public: |
| 91 | Slice() noexcept : repr(Repr{reinterpret_cast<const T *>(this), 0}) {} |
| 92 | Slice(const Slice<T> &) noexcept = default; |
| 93 | |
| 94 | Slice(const T *s, size_t size) : repr(Repr{s, size}) {} |
| 95 | |
| 96 | Slice &operator=(Slice<T> other) noexcept { |
| 97 | this->repr = other.repr; |
| 98 | return *this; |
| 99 | } |
| 100 | |
| 101 | const T *data() const noexcept { return this->repr.ptr; } |
| 102 | size_t size() const noexcept { return this->repr.len; } |
| 103 | size_t length() const noexcept { return this->repr.len; } |
| 104 | |
| 105 | // Repr is PRIVATE; must not be used other than by our generated code. |
| 106 | // |
| 107 | // At present this class is only used for &[u8] slices. |
| 108 | // Not necessarily ABI compatible with &[u8]. Codegen will translate to |
| David Tolnay | e710af1 | 2020-04-14 16:31:54 -0700 | [diff] [blame^] | 109 | // cxx::rust_sliceu8::RustSliceU8 which matches this layout. |
| David Tolnay | efe8105 | 2020-04-14 16:28:24 -0700 | [diff] [blame] | 110 | struct Repr { |
| 111 | const T *ptr; |
| 112 | size_t len; |
| 113 | }; |
| 114 | Slice(Repr repr_) noexcept : repr(repr_) {} |
| 115 | explicit operator Repr() noexcept { return this->repr; } |
| 116 | |
| 117 | private: |
| 118 | Repr repr; |
| 119 | }; |
| 120 | #endif // CXXBRIDGE02_RUST_SLICE |
| 121 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 122 | #ifndef CXXBRIDGE02_RUST_BOX |
| 123 | #define CXXBRIDGE02_RUST_BOX |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 124 | template <typename T> |
| 125 | class Box final { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 126 | public: |
| David Tolnay | f629237 | 2020-03-01 21:09:11 -0800 | [diff] [blame] | 127 | using value_type = T; |
| David Tolnay | 9f92137 | 2020-03-01 21:09:25 -0800 | [diff] [blame] | 128 | using const_pointer = typename std::add_pointer< |
| 129 | typename std::add_const<value_type>::type>::type; |
| 130 | using pointer = typename std::add_pointer<value_type>::type; |
| David Tolnay | f629237 | 2020-03-01 21:09:11 -0800 | [diff] [blame] | 131 | |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 132 | Box(const Box &other) : Box(*other) {} |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 133 | Box(Box &&other) noexcept : ptr(other.ptr) { other.ptr = nullptr; } |
| David Tolnay | 7db7dad | 2020-04-11 14:12:49 -0700 | [diff] [blame] | 134 | explicit Box(const T &val) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 135 | this->uninit(); |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 136 | ::new (this->ptr) T(val); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 137 | } |
| David Tolnay | 7db7dad | 2020-04-11 14:12:49 -0700 | [diff] [blame] | 138 | explicit Box(T &&val) { |
| David Tolnay | 47b3cf2 | 2020-04-11 00:54:39 -0700 | [diff] [blame] | 139 | this->uninit(); |
| 140 | ::new (this->ptr) T(std::move(val)); |
| 141 | } |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 142 | Box &operator=(const Box &other) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 143 | if (this != &other) { |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 144 | if (this->ptr) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 145 | **this = *other; |
| 146 | } else { |
| 147 | this->uninit(); |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 148 | ::new (this->ptr) T(*other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | return *this; |
| 152 | } |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 153 | Box &operator=(Box &&other) noexcept { |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 154 | if (this->ptr) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 155 | this->drop(); |
| 156 | } |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 157 | this->ptr = other.ptr; |
| 158 | other.ptr = nullptr; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 159 | return *this; |
| 160 | } |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 161 | ~Box() noexcept { |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 162 | if (this->ptr) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 163 | this->drop(); |
| 164 | } |
| 165 | } |
| 166 | |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 167 | const T *operator->() const noexcept { return this->ptr; } |
| 168 | const T &operator*() const noexcept { return *this->ptr; } |
| 169 | T *operator->() noexcept { return this->ptr; } |
| 170 | T &operator*() noexcept { return *this->ptr; } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 171 | |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 172 | template <typename... Fields> |
| 173 | static Box in_place(Fields &&... fields) { |
| David Tolnay | 7ce59fc | 2020-04-11 11:46:33 -0700 | [diff] [blame] | 174 | Box box; |
| 175 | box.uninit(); |
| 176 | ::new (box.ptr) T{std::forward<Fields>(fields)...}; |
| 177 | return box; |
| 178 | } |
| 179 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 180 | // Important: requires that `raw` came from an into_raw call. Do not pass a |
| 181 | // pointer from `new` or any other source. |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 182 | static Box from_raw(T *raw) noexcept { |
| 183 | Box box; |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 184 | box.ptr = raw; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 185 | return box; |
| 186 | } |
| 187 | |
| 188 | T *into_raw() noexcept { |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 189 | T *raw = this->ptr; |
| 190 | this->ptr = nullptr; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 191 | return raw; |
| 192 | } |
| 193 | |
| 194 | private: |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 195 | Box() noexcept {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 196 | void uninit() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 197 | void drop() noexcept; |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 198 | T *ptr; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 199 | }; |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 200 | #endif // CXXBRIDGE02_RUST_BOX |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 201 | |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 202 | #ifndef CXXBRIDGE02_RUST_FN |
| 203 | #define CXXBRIDGE02_RUST_FN |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 204 | template <typename Signature, bool Throws = false> |
| 205 | class Fn; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 206 | |
| 207 | template <typename Ret, typename... Args, bool Throws> |
| 208 | class Fn<Ret(Args...), Throws> { |
| 209 | public: |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 210 | Ret operator()(Args... args) const noexcept(!Throws); |
| 211 | Fn operator*() const noexcept; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 212 | |
| 213 | private: |
| 214 | Ret (*trampoline)(Args..., void *fn) noexcept(!Throws); |
| 215 | void *fn; |
| 216 | }; |
| 217 | |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 218 | template <typename Signature> |
| 219 | using TryFn = Fn<Signature, true>; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 220 | #endif // CXXBRIDGE02_RUST_FN |
| 221 | |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 222 | #ifndef CXXBRIDGE02_RUST_ERROR |
| 223 | #define CXXBRIDGE02_RUST_ERROR |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 224 | class Error final : std::exception { |
| 225 | public: |
| 226 | Error(const Error &); |
| 227 | Error(Error &&) noexcept; |
| 228 | Error(Str::Repr) noexcept; |
| 229 | ~Error() noexcept; |
| 230 | const char *what() const noexcept override; |
| 231 | |
| 232 | private: |
| 233 | Str::Repr msg; |
| 234 | }; |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 235 | #endif // CXXBRIDGE02_RUST_ERROR |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 236 | |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 237 | #ifndef CXXBRIDGE02_RUST_ISIZE |
| 238 | #define CXXBRIDGE02_RUST_ISIZE |
| 239 | #if defined(_WIN32) |
| 240 | using isize = SSIZE_T; |
| 241 | #else |
| 242 | using isize = ssize_t; |
| 243 | #endif |
| 244 | #endif // CXXBRIDGE02_RUST_ISIZE |
| 245 | |
| David Tolnay | 851677c | 2020-03-01 23:49:46 -0800 | [diff] [blame] | 246 | std::ostream &operator<<(std::ostream &, const String &); |
| 247 | std::ostream &operator<<(std::ostream &, const Str &); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 248 | |
| David Tolnay | 3b0c988 | 2020-03-01 14:08:57 -0800 | [diff] [blame] | 249 | // Snake case aliases for use in code that uses this style for type names. |
| 250 | using string = String; |
| 251 | using str = Str; |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 252 | template <class T> |
| 253 | using box = Box<T>; |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 254 | using error = Error; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 255 | template <typename Signature, bool Throws = false> |
| 256 | using fn = Fn<Signature, Throws>; |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 257 | template <typename Signature> |
| 258 | using try_fn = TryFn<Signature>; |
| David Tolnay | 3b0c988 | 2020-03-01 14:08:57 -0800 | [diff] [blame] | 259 | |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 260 | #ifndef CXXBRIDGE02_RUST_BITCOPY |
| 261 | #define CXXBRIDGE02_RUST_BITCOPY |
| David Tolnay | d1e2efc | 2020-03-03 22:25:43 -0800 | [diff] [blame] | 262 | struct unsafe_bitcopy_t { |
| 263 | explicit unsafe_bitcopy_t() = default; |
| 264 | }; |
| 265 | constexpr unsafe_bitcopy_t unsafe_bitcopy{}; |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 266 | #endif // CXXBRIDGE02_RUST_BITCOPY |
| David Tolnay | d1e2efc | 2020-03-03 22:25:43 -0800 | [diff] [blame] | 267 | |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 268 | template <typename Ret, typename... Args, bool Throws> |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 269 | Ret Fn<Ret(Args...), Throws>::operator()(Args... args) const noexcept(!Throws) { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 270 | return (*this->trampoline)(std::move(args)..., this->fn); |
| 271 | } |
| 272 | |
| David Tolnay | a23129c | 2020-04-08 20:08:21 -0700 | [diff] [blame] | 273 | template <typename Ret, typename... Args, bool Throws> |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 274 | Fn<Ret(Args...), Throws> Fn<Ret(Args...), Throws>::operator*() const noexcept { |
| David Tolnay | a23129c | 2020-04-08 20:08:21 -0700 | [diff] [blame] | 275 | return *this; |
| 276 | } |
| 277 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 278 | } // namespace cxxbridge02 |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 279 | } // namespace rust |