| David Tolnay | 736cbca | 2020-03-11 16:49:18 -0700 | [diff] [blame] | 1 | #include "../include/cxx.h" |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 2 | #include <cstring> |
| David Tolnay | 71918ec | 2020-04-11 21:52:09 -0700 | [diff] [blame] | 3 | #include <exception> |
| David Tolnay | 001102a | 2020-03-01 20:05:04 -0800 | [diff] [blame] | 4 | #include <iostream> |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 5 | #include <memory> |
| 6 | #include <stdexcept> |
| 7 | |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame^] | 8 | template <typename Exception> |
| 9 | static void panic [[noreturn]] (const char *msg) { |
| David Tolnay | ce5af54 | 2020-04-10 18:08:30 -0700 | [diff] [blame] | 10 | #if defined(RUST_CXX_NO_EXCEPTIONS) |
| 11 | std::cerr << "Error: " << msg << ". Aborting." << std::endl; |
| David Tolnay | 71918ec | 2020-04-11 21:52:09 -0700 | [diff] [blame] | 12 | std::terminate(); |
| David Tolnay | ce5af54 | 2020-04-10 18:08:30 -0700 | [diff] [blame] | 13 | #else |
| 14 | throw Exception(msg); |
| 15 | #endif |
| 16 | } |
| 17 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 18 | extern "C" { |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 19 | const char *cxxbridge02$cxx_string$data(const std::string &s) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 20 | return s.data(); |
| 21 | } |
| 22 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 23 | size_t cxxbridge02$cxx_string$length(const std::string &s) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 24 | return s.length(); |
| 25 | } |
| 26 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 27 | // rust::String |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 28 | void cxxbridge02$string$new(rust::String *self) noexcept; |
| 29 | void cxxbridge02$string$clone(rust::String *self, |
| David Tolnay | 6c08910 | 2020-03-02 00:21:13 -0800 | [diff] [blame] | 30 | const rust::String &other) noexcept; |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 31 | bool cxxbridge02$string$from(rust::String *self, const char *ptr, |
| David Tolnay | 6c08910 | 2020-03-02 00:21:13 -0800 | [diff] [blame] | 32 | size_t len) noexcept; |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 33 | void cxxbridge02$string$drop(rust::String *self) noexcept; |
| 34 | const char *cxxbridge02$string$ptr(const rust::String *self) noexcept; |
| 35 | size_t cxxbridge02$string$len(const rust::String *self) noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 36 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 37 | // rust::Str |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 38 | bool cxxbridge02$str$valid(const char *ptr, size_t len) noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 39 | } // extern "C" |
| 40 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 41 | namespace rust { |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 42 | inline namespace cxxbridge02 { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 43 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 44 | String::String() noexcept { cxxbridge02$string$new(this); } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 45 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 46 | String::String(const String &other) noexcept { |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 47 | cxxbridge02$string$clone(this, other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 48 | } |
| 49 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 50 | String::String(String &&other) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 51 | this->repr = other.repr; |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 52 | cxxbridge02$string$new(&other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 55 | String::~String() noexcept { cxxbridge02$string$drop(this); } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 56 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 57 | String::String(const std::string &s) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 58 | auto ptr = s.data(); |
| 59 | auto len = s.length(); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 60 | if (!cxxbridge02$string$from(this, ptr, len)) { |
| David Tolnay | ce5af54 | 2020-04-10 18:08:30 -0700 | [diff] [blame] | 61 | panic<std::invalid_argument>("data for rust::String is not utf-8"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 65 | String::String(const char *s) { |
| David Tolnay | bb07a4f | 2020-03-16 23:04:00 -0700 | [diff] [blame] | 66 | auto len = std::strlen(s); |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 67 | if (!cxxbridge02$string$from(this, s, len)) { |
| David Tolnay | ce5af54 | 2020-04-10 18:08:30 -0700 | [diff] [blame] | 68 | panic<std::invalid_argument>("data for rust::String is not utf-8"); |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 69 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 72 | String &String::operator=(const String &other) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 73 | if (this != &other) { |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 74 | cxxbridge02$string$drop(this); |
| 75 | cxxbridge02$string$clone(this, other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 76 | } |
| 77 | return *this; |
| 78 | } |
| 79 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 80 | String &String::operator=(String &&other) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 81 | if (this != &other) { |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 82 | cxxbridge02$string$drop(this); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 83 | this->repr = other.repr; |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 84 | cxxbridge02$string$new(&other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 85 | } |
| 86 | return *this; |
| 87 | } |
| 88 | |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 89 | String::operator std::string() const { |
| 90 | return std::string(this->data(), this->size()); |
| 91 | } |
| 92 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 93 | const char *String::data() const noexcept { |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 94 | return cxxbridge02$string$ptr(this); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 95 | } |
| 96 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 97 | size_t String::size() const noexcept { return cxxbridge02$string$len(this); } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 98 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 99 | size_t String::length() const noexcept { return cxxbridge02$string$len(this); } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 100 | |
| David Tolnay | d1e2efc | 2020-03-03 22:25:43 -0800 | [diff] [blame] | 101 | String::String(unsafe_bitcopy_t, const String &bits) noexcept |
| 102 | : repr(bits.repr) {} |
| 103 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 104 | std::ostream &operator<<(std::ostream &os, const String &s) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 105 | os.write(s.data(), s.size()); |
| 106 | return os; |
| 107 | } |
| 108 | |
| David Tolnay | 69fe4c2 | 2020-03-01 13:57:24 -0800 | [diff] [blame] | 109 | Str::Str() noexcept : repr(Repr{reinterpret_cast<const char *>(this), 0}) {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 110 | |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 111 | Str::Str(const Str &) noexcept = default; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 112 | |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 113 | Str::Str(const std::string &s) : repr(Repr{s.data(), s.length()}) { |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 114 | if (!cxxbridge02$str$valid(this->repr.ptr, this->repr.len)) { |
| David Tolnay | ce5af54 | 2020-04-10 18:08:30 -0700 | [diff] [blame] | 115 | panic<std::invalid_argument>("data for rust::Str is not utf-8"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| David Tolnay | bb07a4f | 2020-03-16 23:04:00 -0700 | [diff] [blame] | 119 | Str::Str(const char *s) : repr(Repr{s, std::strlen(s)}) { |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 120 | if (!cxxbridge02$str$valid(this->repr.ptr, this->repr.len)) { |
| David Tolnay | ce5af54 | 2020-04-10 18:08:30 -0700 | [diff] [blame] | 121 | panic<std::invalid_argument>("data for rust::Str is not utf-8"); |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 122 | } |
| 123 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 124 | |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 125 | Str &Str::operator=(Str other) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 126 | this->repr = other.repr; |
| 127 | return *this; |
| 128 | } |
| 129 | |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 130 | Str::operator std::string() const { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 131 | return std::string(this->data(), this->size()); |
| 132 | } |
| 133 | |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 134 | const char *Str::data() const noexcept { return this->repr.ptr; } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 135 | |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 136 | size_t Str::size() const noexcept { return this->repr.len; } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 137 | |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 138 | size_t Str::length() const noexcept { return this->repr.len; } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 139 | |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 140 | Str::Str(Repr repr_) noexcept : repr(repr_) {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 141 | |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 142 | Str::operator Repr() noexcept { return this->repr; } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 143 | |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 144 | std::ostream &operator<<(std::ostream &os, const Str &s) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 145 | os.write(s.data(), s.size()); |
| 146 | return os; |
| 147 | } |
| 148 | |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 149 | extern "C" { |
| 150 | const char *cxxbridge02$error(const char *ptr, size_t len) { |
| 151 | char *copy = new char[len]; |
| 152 | strncpy(copy, ptr, len); |
| 153 | return copy; |
| 154 | } |
| 155 | } // extern "C" |
| 156 | |
| 157 | Error::Error(Str::Repr msg) noexcept : msg(msg) {} |
| 158 | |
| 159 | Error::Error(const Error &other) { |
| 160 | this->msg.ptr = cxxbridge02$error(other.msg.ptr, other.msg.len); |
| 161 | this->msg.len = other.msg.len; |
| 162 | } |
| 163 | |
| 164 | Error::Error(Error &&other) noexcept { |
| 165 | delete[] this->msg.ptr; |
| 166 | this->msg = other.msg; |
| 167 | other.msg.ptr = nullptr; |
| 168 | other.msg.len = 0; |
| 169 | } |
| 170 | |
| 171 | Error::~Error() noexcept { delete[] this->msg.ptr; } |
| 172 | |
| 173 | const char *Error::what() const noexcept { return this->msg.ptr; } |
| 174 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 175 | } // namespace cxxbridge02 |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 176 | } // namespace rust |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 177 | |
| 178 | extern "C" { |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 179 | void cxxbridge02$unique_ptr$std$string$null( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 180 | std::unique_ptr<std::string> *ptr) noexcept { |
| 181 | new (ptr) std::unique_ptr<std::string>(); |
| 182 | } |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 183 | void cxxbridge02$unique_ptr$std$string$raw(std::unique_ptr<std::string> *ptr, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 184 | std::string *raw) noexcept { |
| 185 | new (ptr) std::unique_ptr<std::string>(raw); |
| 186 | } |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 187 | const std::string *cxxbridge02$unique_ptr$std$string$get( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 188 | const std::unique_ptr<std::string> &ptr) noexcept { |
| 189 | return ptr.get(); |
| 190 | } |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 191 | std::string *cxxbridge02$unique_ptr$std$string$release( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 192 | std::unique_ptr<std::string> &ptr) noexcept { |
| 193 | return ptr.release(); |
| 194 | } |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 195 | void cxxbridge02$unique_ptr$std$string$drop( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 196 | std::unique_ptr<std::string> *ptr) noexcept { |
| 197 | ptr->~unique_ptr(); |
| 198 | } |
| 199 | } // extern "C" |