blob: cbda8b944b559986a4997e14559cc485c2c0b30e [file] [log] [blame]
David Tolnay736cbca2020-03-11 16:49:18 -07001#include "../include/cxx.h"
David Tolnay7db73692019-10-20 14:51:12 -04002#include <cstring>
David Tolnay71918ec2020-04-11 21:52:09 -07003#include <exception>
David Tolnay001102a2020-03-01 20:05:04 -08004#include <iostream>
David Tolnay7db73692019-10-20 14:51:12 -04005#include <memory>
6#include <stdexcept>
David Tolnay37dd7e12020-04-25 12:51:59 -07007#include <vector>
David Tolnay7db73692019-10-20 14:51:12 -04008
David Tolnay7db73692019-10-20 14:51:12 -04009extern "C" {
David Tolnay8f16ae72020-10-08 18:21:13 -070010const char *cxxbridge05$cxx_string$data(const std::string &s) noexcept {
David Tolnay7db73692019-10-20 14:51:12 -040011 return s.data();
12}
13
David Tolnay8f16ae72020-10-08 18:21:13 -070014size_t cxxbridge05$cxx_string$length(const std::string &s) noexcept {
David Tolnay7db73692019-10-20 14:51:12 -040015 return s.length();
16}
17
David Tolnay750755e2020-03-01 13:04:08 -080018// rust::String
David Tolnay8f16ae72020-10-08 18:21:13 -070019void cxxbridge05$string$new(rust::String *self) noexcept;
20void cxxbridge05$string$clone(rust::String *self,
David Tolnay6c089102020-03-02 00:21:13 -080021 const rust::String &other) noexcept;
David Tolnay8f16ae72020-10-08 18:21:13 -070022bool cxxbridge05$string$from(rust::String *self, const char *ptr,
David Tolnay6c089102020-03-02 00:21:13 -080023 size_t len) noexcept;
David Tolnay8f16ae72020-10-08 18:21:13 -070024void cxxbridge05$string$drop(rust::String *self) noexcept;
25const char *cxxbridge05$string$ptr(const rust::String *self) noexcept;
26size_t cxxbridge05$string$len(const rust::String *self) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -040027
David Tolnay750755e2020-03-01 13:04:08 -080028// rust::Str
David Tolnay8f16ae72020-10-08 18:21:13 -070029bool cxxbridge05$str$valid(const char *ptr, size_t len) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -040030} // extern "C"
31
David Tolnay750755e2020-03-01 13:04:08 -080032namespace rust {
David Tolnay8f16ae72020-10-08 18:21:13 -070033inline namespace cxxbridge05 {
David Tolnay7db73692019-10-20 14:51:12 -040034
David Tolnay521d99d2020-08-26 20:45:40 -070035template <typename Exception>
36void panic [[noreturn]] (const char *msg) {
37#if defined(RUST_CXX_NO_EXCEPTIONS)
38 std::cerr << "Error: " << msg << ". Aborting." << std::endl;
39 std::terminate();
40#else
41 throw Exception(msg);
42#endif
43}
44
David Tolnayb10c4bc2020-08-26 21:55:29 -070045template void panic<std::out_of_range>[[noreturn]] (const char *msg);
David Tolnay521d99d2020-08-26 20:45:40 -070046
David Tolnay8f16ae72020-10-08 18:21:13 -070047String::String() noexcept { cxxbridge05$string$new(this); }
David Tolnay7db73692019-10-20 14:51:12 -040048
David Tolnay56082162020-03-01 12:57:33 -080049String::String(const String &other) noexcept {
David Tolnay8f16ae72020-10-08 18:21:13 -070050 cxxbridge05$string$clone(this, other);
David Tolnay7db73692019-10-20 14:51:12 -040051}
52
David Tolnay56082162020-03-01 12:57:33 -080053String::String(String &&other) noexcept {
David Tolnay7db73692019-10-20 14:51:12 -040054 this->repr = other.repr;
David Tolnay8f16ae72020-10-08 18:21:13 -070055 cxxbridge05$string$new(&other);
David Tolnay7db73692019-10-20 14:51:12 -040056}
57
David Tolnay8f16ae72020-10-08 18:21:13 -070058String::~String() noexcept { cxxbridge05$string$drop(this); }
David Tolnay7db73692019-10-20 14:51:12 -040059
David Tolnay8d323662020-10-30 19:32:26 -070060String::String(const std::string &s) {
61 if (!cxxbridge05$string$from(this, s.data(), s.length())) {
62 panic<std::invalid_argument>("data for rust::String is not utf-8");
63 }
64}
David Tolnay7db73692019-10-20 14:51:12 -040065
David Tolnay8d323662020-10-30 19:32:26 -070066String::String(const char *s) {
67 if (!cxxbridge05$string$from(this, s, std::strlen(s))) {
68 panic<std::invalid_argument>("data for rust::String is not utf-8");
69 }
70}
David Tolnayc2bbd952020-07-29 18:15:26 -070071
72String::String(const char *s, size_t len) {
David Tolnay8d323662020-10-30 19:32:26 -070073 if (!cxxbridge05$string$from(
74 this,
75 s == nullptr && len == 0 ? reinterpret_cast<const char *>(1) : s,
76 len)) {
David Tolnayce5af542020-04-10 18:08:30 -070077 panic<std::invalid_argument>("data for rust::String is not utf-8");
David Tolnayd9c4ac92020-03-01 20:33:58 -080078 }
David Tolnay7db73692019-10-20 14:51:12 -040079}
80
David Tolnay56082162020-03-01 12:57:33 -080081String &String::operator=(const String &other) noexcept {
David Tolnay7db73692019-10-20 14:51:12 -040082 if (this != &other) {
David Tolnay8f16ae72020-10-08 18:21:13 -070083 cxxbridge05$string$drop(this);
84 cxxbridge05$string$clone(this, other);
David Tolnay7db73692019-10-20 14:51:12 -040085 }
86 return *this;
87}
88
David Tolnay56082162020-03-01 12:57:33 -080089String &String::operator=(String &&other) noexcept {
David Tolnay7db73692019-10-20 14:51:12 -040090 if (this != &other) {
David Tolnay8f16ae72020-10-08 18:21:13 -070091 cxxbridge05$string$drop(this);
David Tolnay7db73692019-10-20 14:51:12 -040092 this->repr = other.repr;
David Tolnay8f16ae72020-10-08 18:21:13 -070093 cxxbridge05$string$new(&other);
David Tolnay7db73692019-10-20 14:51:12 -040094 }
95 return *this;
96}
97
David Tolnayd9c4ac92020-03-01 20:33:58 -080098String::operator std::string() const {
99 return std::string(this->data(), this->size());
100}
101
David Tolnay56082162020-03-01 12:57:33 -0800102const char *String::data() const noexcept {
David Tolnay8f16ae72020-10-08 18:21:13 -0700103 return cxxbridge05$string$ptr(this);
David Tolnay7db73692019-10-20 14:51:12 -0400104}
105
David Tolnay8f16ae72020-10-08 18:21:13 -0700106size_t String::size() const noexcept { return cxxbridge05$string$len(this); }
David Tolnay7db73692019-10-20 14:51:12 -0400107
David Tolnay8f16ae72020-10-08 18:21:13 -0700108size_t String::length() const noexcept { return cxxbridge05$string$len(this); }
David Tolnay7db73692019-10-20 14:51:12 -0400109
David Tolnayd1e2efc2020-03-03 22:25:43 -0800110String::String(unsafe_bitcopy_t, const String &bits) noexcept
111 : repr(bits.repr) {}
112
David Tolnay56082162020-03-01 12:57:33 -0800113std::ostream &operator<<(std::ostream &os, const String &s) {
David Tolnay7db73692019-10-20 14:51:12 -0400114 os.write(s.data(), s.size());
115 return os;
116}
117
David Tolnay8d323662020-10-30 19:32:26 -0700118Str::Str() noexcept : repr(Repr{reinterpret_cast<const char *>(1), 0}) {}
David Tolnay7db73692019-10-20 14:51:12 -0400119
David Tolnayd9c4ac92020-03-01 20:33:58 -0800120Str::Str(const Str &) noexcept = default;
David Tolnay7db73692019-10-20 14:51:12 -0400121
David Tolnay8d323662020-10-30 19:32:26 -0700122Str::Str(const std::string &s) : repr(Repr{s.data(), s.length()}) {
123 if (!cxxbridge05$str$valid(this->repr.ptr, this->repr.len)) {
124 panic<std::invalid_argument>("data for rust::Str is not utf-8");
125 }
126}
David Tolnay7db73692019-10-20 14:51:12 -0400127
David Tolnay8d323662020-10-30 19:32:26 -0700128Str::Str(const char *s) : repr(Repr{s, std::strlen(s)}) {
129 if (!cxxbridge05$str$valid(this->repr.ptr, this->repr.len)) {
130 panic<std::invalid_argument>("data for rust::Str is not utf-8");
131 }
132}
David Tolnay894c5e42020-07-29 18:20:00 -0700133
David Tolnay8d323662020-10-30 19:32:26 -0700134Str::Str(const char *s, size_t len)
135 : repr(
136 Repr{s == nullptr && len == 0 ? reinterpret_cast<const char *>(1) : s,
137 len}) {
David Tolnay8f16ae72020-10-08 18:21:13 -0700138 if (!cxxbridge05$str$valid(this->repr.ptr, this->repr.len)) {
David Tolnayce5af542020-04-10 18:08:30 -0700139 panic<std::invalid_argument>("data for rust::Str is not utf-8");
David Tolnayd9c4ac92020-03-01 20:33:58 -0800140 }
141}
David Tolnay7db73692019-10-20 14:51:12 -0400142
David Tolnay09dbe752020-03-01 13:00:40 -0800143Str &Str::operator=(Str other) noexcept {
David Tolnay7db73692019-10-20 14:51:12 -0400144 this->repr = other.repr;
145 return *this;
146}
147
David Tolnay09dbe752020-03-01 13:00:40 -0800148Str::operator std::string() const {
David Tolnay7db73692019-10-20 14:51:12 -0400149 return std::string(this->data(), this->size());
150}
151
David Tolnay09dbe752020-03-01 13:00:40 -0800152const char *Str::data() const noexcept { return this->repr.ptr; }
David Tolnay7db73692019-10-20 14:51:12 -0400153
David Tolnay09dbe752020-03-01 13:00:40 -0800154size_t Str::size() const noexcept { return this->repr.len; }
David Tolnay7db73692019-10-20 14:51:12 -0400155
David Tolnay09dbe752020-03-01 13:00:40 -0800156size_t Str::length() const noexcept { return this->repr.len; }
David Tolnay7db73692019-10-20 14:51:12 -0400157
David Tolnay09dbe752020-03-01 13:00:40 -0800158Str::Str(Repr repr_) noexcept : repr(repr_) {}
David Tolnay7db73692019-10-20 14:51:12 -0400159
David Tolnay09dbe752020-03-01 13:00:40 -0800160Str::operator Repr() noexcept { return this->repr; }
David Tolnay7db73692019-10-20 14:51:12 -0400161
David Tolnay09dbe752020-03-01 13:00:40 -0800162std::ostream &operator<<(std::ostream &os, const Str &s) {
David Tolnay7db73692019-10-20 14:51:12 -0400163 os.write(s.data(), s.size());
164 return os;
165}
166
David Tolnay1e548172020-03-16 13:37:09 -0700167extern "C" {
David Tolnay8f16ae72020-10-08 18:21:13 -0700168const char *cxxbridge05$error(const char *ptr, size_t len) {
David Tolnay1e548172020-03-16 13:37:09 -0700169 char *copy = new char[len];
170 strncpy(copy, ptr, len);
171 return copy;
172}
173} // extern "C"
174
175Error::Error(Str::Repr msg) noexcept : msg(msg) {}
176
177Error::Error(const Error &other) {
David Tolnay8f16ae72020-10-08 18:21:13 -0700178 this->msg.ptr = cxxbridge05$error(other.msg.ptr, other.msg.len);
David Tolnay1e548172020-03-16 13:37:09 -0700179 this->msg.len = other.msg.len;
180}
181
182Error::Error(Error &&other) noexcept {
183 delete[] this->msg.ptr;
184 this->msg = other.msg;
185 other.msg.ptr = nullptr;
186 other.msg.len = 0;
187}
188
189Error::~Error() noexcept { delete[] this->msg.ptr; }
190
191const char *Error::what() const noexcept { return this->msg.ptr; }
192
David Tolnay8f16ae72020-10-08 18:21:13 -0700193} // namespace cxxbridge05
David Tolnay750755e2020-03-01 13:04:08 -0800194} // namespace rust
David Tolnay7db73692019-10-20 14:51:12 -0400195
196extern "C" {
David Tolnay8f16ae72020-10-08 18:21:13 -0700197void cxxbridge05$unique_ptr$std$string$null(
David Tolnay7db73692019-10-20 14:51:12 -0400198 std::unique_ptr<std::string> *ptr) noexcept {
199 new (ptr) std::unique_ptr<std::string>();
200}
David Tolnay8f16ae72020-10-08 18:21:13 -0700201void cxxbridge05$unique_ptr$std$string$raw(std::unique_ptr<std::string> *ptr,
David Tolnay7db73692019-10-20 14:51:12 -0400202 std::string *raw) noexcept {
203 new (ptr) std::unique_ptr<std::string>(raw);
204}
David Tolnay8f16ae72020-10-08 18:21:13 -0700205const std::string *cxxbridge05$unique_ptr$std$string$get(
David Tolnay7db73692019-10-20 14:51:12 -0400206 const std::unique_ptr<std::string> &ptr) noexcept {
207 return ptr.get();
208}
David Tolnay8f16ae72020-10-08 18:21:13 -0700209std::string *cxxbridge05$unique_ptr$std$string$release(
David Tolnay7db73692019-10-20 14:51:12 -0400210 std::unique_ptr<std::string> &ptr) noexcept {
211 return ptr.release();
212}
David Tolnay8f16ae72020-10-08 18:21:13 -0700213void cxxbridge05$unique_ptr$std$string$drop(
David Tolnay7db73692019-10-20 14:51:12 -0400214 std::unique_ptr<std::string> *ptr) noexcept {
215 ptr->~unique_ptr();
216}
217} // extern "C"
Myron Ahneba35cf2020-02-05 19:41:51 +0700218
David Tolnay37dd7e12020-04-25 12:51:59 -0700219#define STD_VECTOR_OPS(RUST_TYPE, CXX_TYPE) \
David Tolnay8f16ae72020-10-08 18:21:13 -0700220 size_t cxxbridge05$std$vector$##RUST_TYPE##$size( \
David Tolnay37dd7e12020-04-25 12:51:59 -0700221 const std::vector<CXX_TYPE> &s) noexcept { \
222 return s.size(); \
223 } \
David Tolnay8f16ae72020-10-08 18:21:13 -0700224 const CXX_TYPE *cxxbridge05$std$vector$##RUST_TYPE##$get_unchecked( \
David Tolnay37dd7e12020-04-25 12:51:59 -0700225 const std::vector<CXX_TYPE> &s, size_t pos) noexcept { \
David Tolnay9626d082020-04-24 14:52:45 -0700226 return &s[pos]; \
David Tolnay37dd7e12020-04-25 12:51:59 -0700227 } \
David Tolnay8f16ae72020-10-08 18:21:13 -0700228 void cxxbridge05$unique_ptr$std$vector$##RUST_TYPE##$null( \
David Tolnay996db1e2020-04-24 14:46:31 -0700229 std::unique_ptr<std::vector<CXX_TYPE>> *ptr) noexcept { \
230 new (ptr) std::unique_ptr<std::vector<CXX_TYPE>>(); \
David Tolnay37dd7e12020-04-25 12:51:59 -0700231 } \
David Tolnay8f16ae72020-10-08 18:21:13 -0700232 void cxxbridge05$unique_ptr$std$vector$##RUST_TYPE##$raw( \
David Tolnay996db1e2020-04-24 14:46:31 -0700233 std::unique_ptr<std::vector<CXX_TYPE>> *ptr, \
David Tolnay37dd7e12020-04-25 12:51:59 -0700234 std::vector<CXX_TYPE> *raw) noexcept { \
David Tolnay996db1e2020-04-24 14:46:31 -0700235 new (ptr) std::unique_ptr<std::vector<CXX_TYPE>>(raw); \
David Tolnay37dd7e12020-04-25 12:51:59 -0700236 } \
David Tolnay4e7e7c42020-04-24 14:48:07 -0700237 const std::vector<CXX_TYPE> \
David Tolnay8f16ae72020-10-08 18:21:13 -0700238 *cxxbridge05$unique_ptr$std$vector$##RUST_TYPE##$get( \
David Tolnay996db1e2020-04-24 14:46:31 -0700239 const std::unique_ptr<std::vector<CXX_TYPE>> &ptr) noexcept { \
David Tolnay37dd7e12020-04-25 12:51:59 -0700240 return ptr.get(); \
241 } \
David Tolnay4e7e7c42020-04-24 14:48:07 -0700242 std::vector<CXX_TYPE> \
David Tolnay8f16ae72020-10-08 18:21:13 -0700243 *cxxbridge05$unique_ptr$std$vector$##RUST_TYPE##$release( \
David Tolnay996db1e2020-04-24 14:46:31 -0700244 std::unique_ptr<std::vector<CXX_TYPE>> &ptr) noexcept { \
David Tolnay37dd7e12020-04-25 12:51:59 -0700245 return ptr.release(); \
246 } \
David Tolnay8f16ae72020-10-08 18:21:13 -0700247 void cxxbridge05$unique_ptr$std$vector$##RUST_TYPE##$drop( \
David Tolnay996db1e2020-04-24 14:46:31 -0700248 std::unique_ptr<std::vector<CXX_TYPE>> *ptr) noexcept { \
David Tolnay37dd7e12020-04-25 12:51:59 -0700249 ptr->~unique_ptr(); \
David Tolnay4e7e7c42020-04-24 14:48:07 -0700250 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700251
David Tolnay6787be62020-04-25 11:01:02 -0700252#define RUST_VEC_EXTERNS(RUST_TYPE, CXX_TYPE) \
David Tolnay8f16ae72020-10-08 18:21:13 -0700253 void cxxbridge05$rust_vec$##RUST_TYPE##$new( \
David Tolnayf97c2d52020-04-25 16:37:48 -0700254 rust::Vec<CXX_TYPE> *ptr) noexcept; \
David Tolnay8f16ae72020-10-08 18:21:13 -0700255 void cxxbridge05$rust_vec$##RUST_TYPE##$drop( \
David Tolnay6787be62020-04-25 11:01:02 -0700256 rust::Vec<CXX_TYPE> *ptr) noexcept; \
David Tolnay8f16ae72020-10-08 18:21:13 -0700257 size_t cxxbridge05$rust_vec$##RUST_TYPE##$len( \
David Tolnay6787be62020-04-25 11:01:02 -0700258 const rust::Vec<CXX_TYPE> *ptr) noexcept; \
David Tolnay8f16ae72020-10-08 18:21:13 -0700259 const CXX_TYPE *cxxbridge05$rust_vec$##RUST_TYPE##$data( \
David Tolnay6787be62020-04-25 11:01:02 -0700260 const rust::Vec<CXX_TYPE> *ptr) noexcept; \
David Tolnay8f16ae72020-10-08 18:21:13 -0700261 size_t cxxbridge05$rust_vec$##RUST_TYPE##$stride() noexcept;
David Tolnay6787be62020-04-25 11:01:02 -0700262
263#define RUST_VEC_OPS(RUST_TYPE, CXX_TYPE) \
264 template <> \
David Tolnay1768d8f2020-04-25 18:15:11 -0700265 Vec<CXX_TYPE>::Vec() noexcept { \
David Tolnay8f16ae72020-10-08 18:21:13 -0700266 cxxbridge05$rust_vec$##RUST_TYPE##$new(this); \
David Tolnayf97c2d52020-04-25 16:37:48 -0700267 } \
268 template <> \
David Tolnay1768d8f2020-04-25 18:15:11 -0700269 void Vec<CXX_TYPE>::drop() noexcept { \
David Tolnay8f16ae72020-10-08 18:21:13 -0700270 return cxxbridge05$rust_vec$##RUST_TYPE##$drop(this); \
David Tolnay6787be62020-04-25 11:01:02 -0700271 } \
272 template <> \
David Tolnay1768d8f2020-04-25 18:15:11 -0700273 size_t Vec<CXX_TYPE>::size() const noexcept { \
David Tolnay8f16ae72020-10-08 18:21:13 -0700274 return cxxbridge05$rust_vec$##RUST_TYPE##$len(this); \
David Tolnay6787be62020-04-25 11:01:02 -0700275 } \
276 template <> \
David Tolnay1768d8f2020-04-25 18:15:11 -0700277 const CXX_TYPE *Vec<CXX_TYPE>::data() const noexcept { \
David Tolnay8f16ae72020-10-08 18:21:13 -0700278 return cxxbridge05$rust_vec$##RUST_TYPE##$data(this); \
David Tolnay6787be62020-04-25 11:01:02 -0700279 } \
280 template <> \
David Tolnay1768d8f2020-04-25 18:15:11 -0700281 size_t Vec<CXX_TYPE>::stride() noexcept { \
David Tolnay8f16ae72020-10-08 18:21:13 -0700282 return cxxbridge05$rust_vec$##RUST_TYPE##$stride(); \
David Tolnay6787be62020-04-25 11:01:02 -0700283 }
284
285// Usize and isize are the same type as one of the below.
David Tolnayf336b3b2020-04-30 08:45:54 -0700286#define FOR_EACH_NUMERIC(MACRO) \
David Tolnay6787be62020-04-25 11:01:02 -0700287 MACRO(u8, uint8_t) \
288 MACRO(u16, uint16_t) \
289 MACRO(u32, uint32_t) \
290 MACRO(u64, uint64_t) \
291 MACRO(i8, int8_t) \
292 MACRO(i16, int16_t) \
293 MACRO(i32, int32_t) \
294 MACRO(i64, int64_t) \
295 MACRO(f32, float) \
296 MACRO(f64, double)
297
David Tolnayf336b3b2020-04-30 08:45:54 -0700298#define FOR_EACH_STD_VECTOR(MACRO) \
299 FOR_EACH_NUMERIC(MACRO) \
David Tolnay6787be62020-04-25 11:01:02 -0700300 MACRO(usize, size_t) \
David Tolnay47e239d2020-08-28 00:32:04 -0700301 MACRO(isize, rust::isize) \
302 MACRO(string, std::string)
David Tolnay6787be62020-04-25 11:01:02 -0700303
David Tolnayf336b3b2020-04-30 08:45:54 -0700304#define FOR_EACH_RUST_VEC(MACRO) \
305 FOR_EACH_NUMERIC(MACRO) \
David Tolnay33f56ad2020-08-27 17:06:35 -0700306 MACRO(bool, bool) \
307 MACRO(string, rust::String)
David Tolnayf336b3b2020-04-30 08:45:54 -0700308
David Tolnay4e7e7c42020-04-24 14:48:07 -0700309extern "C" {
David Tolnayf336b3b2020-04-30 08:45:54 -0700310FOR_EACH_STD_VECTOR(STD_VECTOR_OPS)
311FOR_EACH_RUST_VEC(RUST_VEC_EXTERNS)
David Tolnay4e7e7c42020-04-24 14:48:07 -0700312} // extern "C"
David Tolnay6787be62020-04-25 11:01:02 -0700313
David Tolnay1768d8f2020-04-25 18:15:11 -0700314namespace rust {
David Tolnay8f16ae72020-10-08 18:21:13 -0700315inline namespace cxxbridge05 {
David Tolnayf336b3b2020-04-30 08:45:54 -0700316FOR_EACH_RUST_VEC(RUST_VEC_OPS)
David Tolnay8f16ae72020-10-08 18:21:13 -0700317} // namespace cxxbridge05
David Tolnay1768d8f2020-04-25 18:15:11 -0700318} // namespace rust