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