| 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 | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 86 | #ifndef CXXBRIDGE02_RUST_BOX |
| 87 | #define CXXBRIDGE02_RUST_BOX |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame^] | 88 | template <typename T> |
| 89 | class Box final { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 90 | public: |
| David Tolnay | f629237 | 2020-03-01 21:09:11 -0800 | [diff] [blame] | 91 | using value_type = T; |
| David Tolnay | 9f92137 | 2020-03-01 21:09:25 -0800 | [diff] [blame] | 92 | using const_pointer = typename std::add_pointer< |
| 93 | typename std::add_const<value_type>::type>::type; |
| 94 | using pointer = typename std::add_pointer<value_type>::type; |
| David Tolnay | f629237 | 2020-03-01 21:09:11 -0800 | [diff] [blame] | 95 | |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 96 | Box(const Box &other) : Box(*other) {} |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 97 | Box(Box &&other) noexcept : ptr(other.ptr) { other.ptr = nullptr; } |
| David Tolnay | 7db7dad | 2020-04-11 14:12:49 -0700 | [diff] [blame] | 98 | explicit Box(const T &val) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 99 | this->uninit(); |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 100 | ::new (this->ptr) T(val); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 101 | } |
| David Tolnay | 7db7dad | 2020-04-11 14:12:49 -0700 | [diff] [blame] | 102 | explicit Box(T &&val) { |
| David Tolnay | 47b3cf2 | 2020-04-11 00:54:39 -0700 | [diff] [blame] | 103 | this->uninit(); |
| 104 | ::new (this->ptr) T(std::move(val)); |
| 105 | } |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 106 | Box &operator=(const Box &other) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 107 | if (this != &other) { |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 108 | if (this->ptr) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 109 | **this = *other; |
| 110 | } else { |
| 111 | this->uninit(); |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 112 | ::new (this->ptr) T(*other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | return *this; |
| 116 | } |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 117 | Box &operator=(Box &&other) noexcept { |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 118 | if (this->ptr) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 119 | this->drop(); |
| 120 | } |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 121 | this->ptr = other.ptr; |
| 122 | other.ptr = nullptr; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 123 | return *this; |
| 124 | } |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 125 | ~Box() noexcept { |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 126 | if (this->ptr) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 127 | this->drop(); |
| 128 | } |
| 129 | } |
| 130 | |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 131 | const T *operator->() const noexcept { return this->ptr; } |
| 132 | const T &operator*() const noexcept { return *this->ptr; } |
| 133 | T *operator->() noexcept { return this->ptr; } |
| 134 | T &operator*() noexcept { return *this->ptr; } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 135 | |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame^] | 136 | template <typename... Fields> |
| 137 | static Box in_place(Fields &&... fields) { |
| David Tolnay | 7ce59fc | 2020-04-11 11:46:33 -0700 | [diff] [blame] | 138 | Box box; |
| 139 | box.uninit(); |
| 140 | ::new (box.ptr) T{std::forward<Fields>(fields)...}; |
| 141 | return box; |
| 142 | } |
| 143 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 144 | // Important: requires that `raw` came from an into_raw call. Do not pass a |
| 145 | // pointer from `new` or any other source. |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 146 | static Box from_raw(T *raw) noexcept { |
| 147 | Box box; |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 148 | box.ptr = raw; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 149 | return box; |
| 150 | } |
| 151 | |
| 152 | T *into_raw() noexcept { |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 153 | T *raw = this->ptr; |
| 154 | this->ptr = nullptr; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 155 | return raw; |
| 156 | } |
| 157 | |
| 158 | private: |
| David Tolnay | 324437a | 2020-03-01 13:02:24 -0800 | [diff] [blame] | 159 | Box() noexcept {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 160 | void uninit() noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 161 | void drop() noexcept; |
| David Tolnay | 33169bd | 2020-03-06 13:02:08 -0800 | [diff] [blame] | 162 | T *ptr; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 163 | }; |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 164 | #endif // CXXBRIDGE02_RUST_BOX |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 165 | |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 166 | #ifndef CXXBRIDGE02_RUST_FN |
| 167 | #define CXXBRIDGE02_RUST_FN |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame^] | 168 | template <typename Signature, bool Throws = false> |
| 169 | class Fn; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 170 | |
| 171 | template <typename Ret, typename... Args, bool Throws> |
| 172 | class Fn<Ret(Args...), Throws> { |
| 173 | public: |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 174 | Ret operator()(Args... args) const noexcept(!Throws); |
| 175 | Fn operator*() const noexcept; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 176 | |
| 177 | private: |
| 178 | Ret (*trampoline)(Args..., void *fn) noexcept(!Throws); |
| 179 | void *fn; |
| 180 | }; |
| 181 | |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame^] | 182 | template <typename Signature> |
| 183 | using TryFn = Fn<Signature, true>; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 184 | #endif // CXXBRIDGE02_RUST_FN |
| 185 | |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 186 | #ifndef CXXBRIDGE02_RUST_ERROR |
| 187 | #define CXXBRIDGE02_RUST_ERROR |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 188 | class Error final : std::exception { |
| 189 | public: |
| 190 | Error(const Error &); |
| 191 | Error(Error &&) noexcept; |
| 192 | Error(Str::Repr) noexcept; |
| 193 | ~Error() noexcept; |
| 194 | const char *what() const noexcept override; |
| 195 | |
| 196 | private: |
| 197 | Str::Repr msg; |
| 198 | }; |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 199 | #endif // CXXBRIDGE02_RUST_ERROR |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 200 | |
| David Tolnay | b8a6fb2 | 2020-04-10 11:17:28 -0700 | [diff] [blame] | 201 | #ifndef CXXBRIDGE02_RUST_ISIZE |
| 202 | #define CXXBRIDGE02_RUST_ISIZE |
| 203 | #if defined(_WIN32) |
| 204 | using isize = SSIZE_T; |
| 205 | #else |
| 206 | using isize = ssize_t; |
| 207 | #endif |
| 208 | #endif // CXXBRIDGE02_RUST_ISIZE |
| 209 | |
| David Tolnay | 851677c | 2020-03-01 23:49:46 -0800 | [diff] [blame] | 210 | std::ostream &operator<<(std::ostream &, const String &); |
| 211 | std::ostream &operator<<(std::ostream &, const Str &); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 212 | |
| David Tolnay | 3b0c988 | 2020-03-01 14:08:57 -0800 | [diff] [blame] | 213 | // Snake case aliases for use in code that uses this style for type names. |
| 214 | using string = String; |
| 215 | using str = Str; |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame^] | 216 | template <class T> |
| 217 | using box = Box<T>; |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 218 | using error = Error; |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 219 | template <typename Signature, bool Throws = false> |
| 220 | using fn = Fn<Signature, Throws>; |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame^] | 221 | template <typename Signature> |
| 222 | using try_fn = TryFn<Signature>; |
| David Tolnay | 3b0c988 | 2020-03-01 14:08:57 -0800 | [diff] [blame] | 223 | |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 224 | #ifndef CXXBRIDGE02_RUST_BITCOPY |
| 225 | #define CXXBRIDGE02_RUST_BITCOPY |
| David Tolnay | d1e2efc | 2020-03-03 22:25:43 -0800 | [diff] [blame] | 226 | struct unsafe_bitcopy_t { |
| 227 | explicit unsafe_bitcopy_t() = default; |
| 228 | }; |
| 229 | constexpr unsafe_bitcopy_t unsafe_bitcopy{}; |
| David Tolnay | b7a7cb6 | 2020-03-17 21:18:40 -0700 | [diff] [blame] | 230 | #endif // CXXBRIDGE02_RUST_BITCOPY |
| David Tolnay | d1e2efc | 2020-03-03 22:25:43 -0800 | [diff] [blame] | 231 | |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 232 | template <typename Ret, typename... Args, bool Throws> |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 233 | Ret Fn<Ret(Args...), Throws>::operator()(Args... args) const noexcept(!Throws) { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 234 | return (*this->trampoline)(std::move(args)..., this->fn); |
| 235 | } |
| 236 | |
| David Tolnay | a23129c | 2020-04-08 20:08:21 -0700 | [diff] [blame] | 237 | template <typename Ret, typename... Args, bool Throws> |
| David Tolnay | 533d458 | 2020-04-08 20:29:14 -0700 | [diff] [blame] | 238 | Fn<Ret(Args...), Throws> Fn<Ret(Args...), Throws>::operator*() const noexcept { |
| David Tolnay | a23129c | 2020-04-08 20:08:21 -0700 | [diff] [blame] | 239 | return *this; |
| 240 | } |
| 241 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 242 | } // namespace cxxbridge02 |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 243 | } // namespace rust |