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