blob: 338dafe4958650eae8d4bb716c6bd282519bc52f [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001#pragma once
2#include <array>
David Tolnay30430f12020-03-19 20:49:00 -07003#include <cstddef>
David Tolnay7db73692019-10-20 14:51:12 -04004#include <cstdint>
David Tolnayb7a7cb62020-03-17 21:18:40 -07005#include <exception>
David Tolnay001102a2020-03-01 20:05:04 -08006#include <iosfwd>
David Tolnay7db73692019-10-20 14:51:12 -04007#include <string>
David Tolnayf6292372020-03-01 21:09:11 -08008#include <type_traits>
David Tolnay4791f1c2020-03-17 21:53:16 -07009#include <utility>
David Tolnay59b5ba12020-04-10 11:32:19 -070010#if defined(_WIN32)
11#include <BaseTsd.h>
12#endif
David Tolnay7db73692019-10-20 14:51:12 -040013
David Tolnay750755e2020-03-01 13:04:08 -080014namespace rust {
David Tolnay8c730492020-03-13 01:29:06 -070015inline namespace cxxbridge02 {
David Tolnay7db73692019-10-20 14:51:12 -040016
David Tolnayd1e2efc2020-03-03 22:25:43 -080017struct unsafe_bitcopy_t;
18
David Tolnayb7a7cb62020-03-17 21:18:40 -070019#ifndef CXXBRIDGE02_RUST_STRING
20#define CXXBRIDGE02_RUST_STRING
David Tolnay56082162020-03-01 12:57:33 -080021class String final {
David Tolnay7db73692019-10-20 14:51:12 -040022public:
David Tolnay56082162020-03-01 12:57:33 -080023 String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080024 String(const String &) noexcept;
25 String(String &&) noexcept;
David Tolnay56082162020-03-01 12:57:33 -080026 ~String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080027
28 String(const std::string &);
29 String(const char *);
30
31 String &operator=(const String &) noexcept;
32 String &operator=(String &&) noexcept;
33
David Tolnay404d6892020-03-01 20:19:41 -080034 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040035
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 Tolnayd1e2efc2020-03-03 22:25:43 -080041 // Internal API only intended for the cxxbridge code generator.
42 String(unsafe_bitcopy_t, const String &) noexcept;
43
David Tolnay7db73692019-10-20 14:51:12 -040044private:
45 // Size and alignment statically verified by rust_string.rs.
46 std::array<uintptr_t, 3> repr;
47};
David Tolnayb7a7cb62020-03-17 21:18:40 -070048#endif // CXXBRIDGE02_RUST_STRING
David Tolnay7db73692019-10-20 14:51:12 -040049
David Tolnayb7a7cb62020-03-17 21:18:40 -070050#ifndef CXXBRIDGE02_RUST_STR
51#define CXXBRIDGE02_RUST_STR
David Tolnay09dbe752020-03-01 13:00:40 -080052class Str final {
David Tolnay7db73692019-10-20 14:51:12 -040053public:
David Tolnay09dbe752020-03-01 13:00:40 -080054 Str() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080055 Str(const Str &) noexcept;
56
David Tolnay851677c2020-03-01 23:49:46 -080057 Str(const std::string &);
58 Str(const char *);
59 Str(std::string &&) = delete;
David Tolnayd9c4ac92020-03-01 20:33:58 -080060
61 Str &operator=(Str) noexcept;
62
David Tolnay404d6892020-03-01 20:19:41 -080063 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040064
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 Tolnayd9c4ac92020-03-01 20:33:58 -080078 Str(Repr) noexcept;
David Tolnaybaae4432020-03-01 20:20:10 -080079 explicit operator Repr() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -040080
81private:
82 Repr repr;
83};
David Tolnayb7a7cb62020-03-17 21:18:40 -070084#endif // CXXBRIDGE02_RUST_STR
David Tolnay7db73692019-10-20 14:51:12 -040085
David Tolnay8c730492020-03-13 01:29:06 -070086#ifndef CXXBRIDGE02_RUST_BOX
87#define CXXBRIDGE02_RUST_BOX
David Tolnay324437a2020-03-01 13:02:24 -080088template <typename T> class Box final {
David Tolnay7db73692019-10-20 14:51:12 -040089public:
David Tolnayf6292372020-03-01 21:09:11 -080090 using value_type = T;
David Tolnay9f921372020-03-01 21:09:25 -080091 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 Tolnayf6292372020-03-01 21:09:11 -080094
David Tolnay324437a2020-03-01 13:02:24 -080095 Box(const Box &other) : Box(*other) {}
David Tolnay33169bd2020-03-06 13:02:08 -080096 Box(Box &&other) noexcept : ptr(other.ptr) { other.ptr = nullptr; }
David Tolnay324437a2020-03-01 13:02:24 -080097 Box(const T &val) {
David Tolnay7db73692019-10-20 14:51:12 -040098 this->uninit();
David Tolnay33169bd2020-03-06 13:02:08 -080099 ::new (this->ptr) T(val);
David Tolnay7db73692019-10-20 14:51:12 -0400100 }
David Tolnay47b3cf22020-04-11 00:54:39 -0700101 Box(T &&val) {
102 this->uninit();
103 ::new (this->ptr) T(std::move(val));
104 }
David Tolnay324437a2020-03-01 13:02:24 -0800105 Box &operator=(const Box &other) {
David Tolnay7db73692019-10-20 14:51:12 -0400106 if (this != &other) {
David Tolnay33169bd2020-03-06 13:02:08 -0800107 if (this->ptr) {
David Tolnay7db73692019-10-20 14:51:12 -0400108 **this = *other;
109 } else {
110 this->uninit();
David Tolnay33169bd2020-03-06 13:02:08 -0800111 ::new (this->ptr) T(*other);
David Tolnay7db73692019-10-20 14:51:12 -0400112 }
113 }
114 return *this;
115 }
David Tolnay324437a2020-03-01 13:02:24 -0800116 Box &operator=(Box &&other) noexcept {
David Tolnay33169bd2020-03-06 13:02:08 -0800117 if (this->ptr) {
David Tolnay7db73692019-10-20 14:51:12 -0400118 this->drop();
119 }
David Tolnay33169bd2020-03-06 13:02:08 -0800120 this->ptr = other.ptr;
121 other.ptr = nullptr;
David Tolnay7db73692019-10-20 14:51:12 -0400122 return *this;
123 }
David Tolnay324437a2020-03-01 13:02:24 -0800124 ~Box() noexcept {
David Tolnay33169bd2020-03-06 13:02:08 -0800125 if (this->ptr) {
David Tolnay7db73692019-10-20 14:51:12 -0400126 this->drop();
127 }
128 }
129
David Tolnay33169bd2020-03-06 13:02:08 -0800130 const T *operator->() const noexcept { return this->ptr; }
131 const T &operator*() const noexcept { return *this->ptr; }
132 T *operator->() noexcept { return this->ptr; }
133 T &operator*() noexcept { return *this->ptr; }
David Tolnay7db73692019-10-20 14:51:12 -0400134
135 // Important: requires that `raw` came from an into_raw call. Do not pass a
136 // pointer from `new` or any other source.
David Tolnay324437a2020-03-01 13:02:24 -0800137 static Box from_raw(T *raw) noexcept {
138 Box box;
David Tolnay33169bd2020-03-06 13:02:08 -0800139 box.ptr = raw;
David Tolnay7db73692019-10-20 14:51:12 -0400140 return box;
141 }
142
143 T *into_raw() noexcept {
David Tolnay33169bd2020-03-06 13:02:08 -0800144 T *raw = this->ptr;
145 this->ptr = nullptr;
David Tolnay7db73692019-10-20 14:51:12 -0400146 return raw;
147 }
148
149private:
David Tolnay324437a2020-03-01 13:02:24 -0800150 Box() noexcept {}
David Tolnay7db73692019-10-20 14:51:12 -0400151 void uninit() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400152 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800153 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400154};
David Tolnay8c730492020-03-13 01:29:06 -0700155#endif // CXXBRIDGE02_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400156
David Tolnay75dca2e2020-03-25 20:17:52 -0700157#ifndef CXXBRIDGE02_RUST_FN
158#define CXXBRIDGE02_RUST_FN
159template <typename Signature, bool Throws = false> class Fn;
160
161template <typename Ret, typename... Args, bool Throws>
162class Fn<Ret(Args...), Throws> {
163public:
David Tolnay533d4582020-04-08 20:29:14 -0700164 Ret operator()(Args... args) const noexcept(!Throws);
165 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700166
167private:
168 Ret (*trampoline)(Args..., void *fn) noexcept(!Throws);
169 void *fn;
170};
171
172template <typename Signature> using TryFn = Fn<Signature, true>;
173#endif // CXXBRIDGE02_RUST_FN
174
David Tolnayb7a7cb62020-03-17 21:18:40 -0700175#ifndef CXXBRIDGE02_RUST_ERROR
176#define CXXBRIDGE02_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700177class Error final : std::exception {
178public:
179 Error(const Error &);
180 Error(Error &&) noexcept;
181 Error(Str::Repr) noexcept;
182 ~Error() noexcept;
183 const char *what() const noexcept override;
184
185private:
186 Str::Repr msg;
187};
David Tolnayb7a7cb62020-03-17 21:18:40 -0700188#endif // CXXBRIDGE02_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700189
David Tolnayb8a6fb22020-04-10 11:17:28 -0700190#ifndef CXXBRIDGE02_RUST_ISIZE
191#define CXXBRIDGE02_RUST_ISIZE
192#if defined(_WIN32)
193using isize = SSIZE_T;
194#else
195using isize = ssize_t;
196#endif
197#endif // CXXBRIDGE02_RUST_ISIZE
198
David Tolnay851677c2020-03-01 23:49:46 -0800199std::ostream &operator<<(std::ostream &, const String &);
200std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400201
David Tolnay3b0c9882020-03-01 14:08:57 -0800202// Snake case aliases for use in code that uses this style for type names.
203using string = String;
204using str = Str;
205template <class T> using box = Box<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700206using error = Error;
David Tolnay75dca2e2020-03-25 20:17:52 -0700207template <typename Signature, bool Throws = false>
208using fn = Fn<Signature, Throws>;
209template <typename Signature> using try_fn = TryFn<Signature>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800210
David Tolnayb7a7cb62020-03-17 21:18:40 -0700211#ifndef CXXBRIDGE02_RUST_BITCOPY
212#define CXXBRIDGE02_RUST_BITCOPY
David Tolnayd1e2efc2020-03-03 22:25:43 -0800213struct unsafe_bitcopy_t {
214 explicit unsafe_bitcopy_t() = default;
215};
216constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnayb7a7cb62020-03-17 21:18:40 -0700217#endif // CXXBRIDGE02_RUST_BITCOPY
David Tolnayd1e2efc2020-03-03 22:25:43 -0800218
David Tolnay75dca2e2020-03-25 20:17:52 -0700219template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700220Ret Fn<Ret(Args...), Throws>::operator()(Args... args) const noexcept(!Throws) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700221 return (*this->trampoline)(std::move(args)..., this->fn);
222}
223
David Tolnaya23129c2020-04-08 20:08:21 -0700224template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700225Fn<Ret(Args...), Throws> Fn<Ret(Args...), Throws>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700226 return *this;
227}
228
David Tolnay8c730492020-03-13 01:29:06 -0700229} // namespace cxxbridge02
David Tolnay750755e2020-03-01 13:04:08 -0800230} // namespace rust