blob: a8239a122600a0dc8c555c937678762bb5fcab35 [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 Tolnayf262d382020-04-11 22:12:40 -070088template <typename T>
89class Box final {
David Tolnay7db73692019-10-20 14:51:12 -040090public:
David Tolnayf6292372020-03-01 21:09:11 -080091 using value_type = T;
David Tolnay9f921372020-03-01 21:09:25 -080092 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 Tolnayf6292372020-03-01 21:09:11 -080095
David Tolnay324437a2020-03-01 13:02:24 -080096 Box(const Box &other) : Box(*other) {}
David Tolnay33169bd2020-03-06 13:02:08 -080097 Box(Box &&other) noexcept : ptr(other.ptr) { other.ptr = nullptr; }
David Tolnay7db7dad2020-04-11 14:12:49 -070098 explicit Box(const T &val) {
David Tolnay7db73692019-10-20 14:51:12 -040099 this->uninit();
David Tolnay33169bd2020-03-06 13:02:08 -0800100 ::new (this->ptr) T(val);
David Tolnay7db73692019-10-20 14:51:12 -0400101 }
David Tolnay7db7dad2020-04-11 14:12:49 -0700102 explicit Box(T &&val) {
David Tolnay47b3cf22020-04-11 00:54:39 -0700103 this->uninit();
104 ::new (this->ptr) T(std::move(val));
105 }
David Tolnay324437a2020-03-01 13:02:24 -0800106 Box &operator=(const Box &other) {
David Tolnay7db73692019-10-20 14:51:12 -0400107 if (this != &other) {
David Tolnay33169bd2020-03-06 13:02:08 -0800108 if (this->ptr) {
David Tolnay7db73692019-10-20 14:51:12 -0400109 **this = *other;
110 } else {
111 this->uninit();
David Tolnay33169bd2020-03-06 13:02:08 -0800112 ::new (this->ptr) T(*other);
David Tolnay7db73692019-10-20 14:51:12 -0400113 }
114 }
115 return *this;
116 }
David Tolnay324437a2020-03-01 13:02:24 -0800117 Box &operator=(Box &&other) noexcept {
David Tolnay33169bd2020-03-06 13:02:08 -0800118 if (this->ptr) {
David Tolnay7db73692019-10-20 14:51:12 -0400119 this->drop();
120 }
David Tolnay33169bd2020-03-06 13:02:08 -0800121 this->ptr = other.ptr;
122 other.ptr = nullptr;
David Tolnay7db73692019-10-20 14:51:12 -0400123 return *this;
124 }
David Tolnay324437a2020-03-01 13:02:24 -0800125 ~Box() noexcept {
David Tolnay33169bd2020-03-06 13:02:08 -0800126 if (this->ptr) {
David Tolnay7db73692019-10-20 14:51:12 -0400127 this->drop();
128 }
129 }
130
David Tolnay33169bd2020-03-06 13:02:08 -0800131 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 Tolnay7db73692019-10-20 14:51:12 -0400135
David Tolnayf262d382020-04-11 22:12:40 -0700136 template <typename... Fields>
137 static Box in_place(Fields &&... fields) {
David Tolnay7ce59fc2020-04-11 11:46:33 -0700138 Box box;
139 box.uninit();
140 ::new (box.ptr) T{std::forward<Fields>(fields)...};
141 return box;
142 }
143
David Tolnay7db73692019-10-20 14:51:12 -0400144 // Important: requires that `raw` came from an into_raw call. Do not pass a
145 // pointer from `new` or any other source.
David Tolnay324437a2020-03-01 13:02:24 -0800146 static Box from_raw(T *raw) noexcept {
147 Box box;
David Tolnay33169bd2020-03-06 13:02:08 -0800148 box.ptr = raw;
David Tolnay7db73692019-10-20 14:51:12 -0400149 return box;
150 }
151
152 T *into_raw() noexcept {
David Tolnay33169bd2020-03-06 13:02:08 -0800153 T *raw = this->ptr;
154 this->ptr = nullptr;
David Tolnay7db73692019-10-20 14:51:12 -0400155 return raw;
156 }
157
158private:
David Tolnay324437a2020-03-01 13:02:24 -0800159 Box() noexcept {}
David Tolnay7db73692019-10-20 14:51:12 -0400160 void uninit() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400161 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800162 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400163};
David Tolnay8c730492020-03-13 01:29:06 -0700164#endif // CXXBRIDGE02_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400165
David Tolnay75dca2e2020-03-25 20:17:52 -0700166#ifndef CXXBRIDGE02_RUST_FN
167#define CXXBRIDGE02_RUST_FN
David Tolnayf262d382020-04-11 22:12:40 -0700168template <typename Signature, bool Throws = false>
169class Fn;
David Tolnay75dca2e2020-03-25 20:17:52 -0700170
171template <typename Ret, typename... Args, bool Throws>
172class Fn<Ret(Args...), Throws> {
173public:
David Tolnay533d4582020-04-08 20:29:14 -0700174 Ret operator()(Args... args) const noexcept(!Throws);
175 Fn operator*() const noexcept;
David Tolnay75dca2e2020-03-25 20:17:52 -0700176
177private:
178 Ret (*trampoline)(Args..., void *fn) noexcept(!Throws);
179 void *fn;
180};
181
David Tolnayf262d382020-04-11 22:12:40 -0700182template <typename Signature>
183using TryFn = Fn<Signature, true>;
David Tolnay75dca2e2020-03-25 20:17:52 -0700184#endif // CXXBRIDGE02_RUST_FN
185
David Tolnayb7a7cb62020-03-17 21:18:40 -0700186#ifndef CXXBRIDGE02_RUST_ERROR
187#define CXXBRIDGE02_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700188class Error final : std::exception {
189public:
190 Error(const Error &);
191 Error(Error &&) noexcept;
192 Error(Str::Repr) noexcept;
193 ~Error() noexcept;
194 const char *what() const noexcept override;
195
196private:
197 Str::Repr msg;
198};
David Tolnayb7a7cb62020-03-17 21:18:40 -0700199#endif // CXXBRIDGE02_RUST_ERROR
David Tolnay1e548172020-03-16 13:37:09 -0700200
David Tolnayb8a6fb22020-04-10 11:17:28 -0700201#ifndef CXXBRIDGE02_RUST_ISIZE
202#define CXXBRIDGE02_RUST_ISIZE
203#if defined(_WIN32)
204using isize = SSIZE_T;
205#else
206using isize = ssize_t;
207#endif
208#endif // CXXBRIDGE02_RUST_ISIZE
209
David Tolnay851677c2020-03-01 23:49:46 -0800210std::ostream &operator<<(std::ostream &, const String &);
211std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400212
David Tolnay3b0c9882020-03-01 14:08:57 -0800213// Snake case aliases for use in code that uses this style for type names.
214using string = String;
215using str = Str;
David Tolnayf262d382020-04-11 22:12:40 -0700216template <class T>
217using box = Box<T>;
David Tolnay1e548172020-03-16 13:37:09 -0700218using error = Error;
David Tolnay75dca2e2020-03-25 20:17:52 -0700219template <typename Signature, bool Throws = false>
220using fn = Fn<Signature, Throws>;
David Tolnayf262d382020-04-11 22:12:40 -0700221template <typename Signature>
222using try_fn = TryFn<Signature>;
David Tolnay3b0c9882020-03-01 14:08:57 -0800223
David Tolnayb7a7cb62020-03-17 21:18:40 -0700224#ifndef CXXBRIDGE02_RUST_BITCOPY
225#define CXXBRIDGE02_RUST_BITCOPY
David Tolnayd1e2efc2020-03-03 22:25:43 -0800226struct unsafe_bitcopy_t {
227 explicit unsafe_bitcopy_t() = default;
228};
229constexpr unsafe_bitcopy_t unsafe_bitcopy{};
David Tolnayb7a7cb62020-03-17 21:18:40 -0700230#endif // CXXBRIDGE02_RUST_BITCOPY
David Tolnayd1e2efc2020-03-03 22:25:43 -0800231
David Tolnay75dca2e2020-03-25 20:17:52 -0700232template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700233Ret Fn<Ret(Args...), Throws>::operator()(Args... args) const noexcept(!Throws) {
David Tolnay75dca2e2020-03-25 20:17:52 -0700234 return (*this->trampoline)(std::move(args)..., this->fn);
235}
236
David Tolnaya23129c2020-04-08 20:08:21 -0700237template <typename Ret, typename... Args, bool Throws>
David Tolnay533d4582020-04-08 20:29:14 -0700238Fn<Ret(Args...), Throws> Fn<Ret(Args...), Throws>::operator*() const noexcept {
David Tolnaya23129c2020-04-08 20:08:21 -0700239 return *this;
240}
241
David Tolnay8c730492020-03-13 01:29:06 -0700242} // namespace cxxbridge02
David Tolnay750755e2020-03-01 13:04:08 -0800243} // namespace rust