| 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> |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 7 | #include <vector> |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 8 | |
| David Tolnay | f262d38 | 2020-04-11 22:12:40 -0700 | [diff] [blame] | 9 | template <typename Exception> |
| 10 | static void panic [[noreturn]] (const char *msg) { |
| David Tolnay | ce5af54 | 2020-04-10 18:08:30 -0700 | [diff] [blame] | 11 | #if defined(RUST_CXX_NO_EXCEPTIONS) |
| 12 | std::cerr << "Error: " << msg << ". Aborting." << std::endl; |
| David Tolnay | 71918ec | 2020-04-11 21:52:09 -0700 | [diff] [blame] | 13 | std::terminate(); |
| David Tolnay | ce5af54 | 2020-04-10 18:08:30 -0700 | [diff] [blame] | 14 | #else |
| 15 | throw Exception(msg); |
| 16 | #endif |
| 17 | } |
| 18 | |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 19 | extern "C" { |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 20 | const char *cxxbridge03$cxx_string$data(const std::string &s) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 21 | return s.data(); |
| 22 | } |
| 23 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 24 | size_t cxxbridge03$cxx_string$length(const std::string &s) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 25 | return s.length(); |
| 26 | } |
| 27 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 28 | // rust::String |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 29 | void cxxbridge03$string$new(rust::String *self) noexcept; |
| 30 | void cxxbridge03$string$clone(rust::String *self, |
| David Tolnay | 6c08910 | 2020-03-02 00:21:13 -0800 | [diff] [blame] | 31 | const rust::String &other) noexcept; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 32 | bool cxxbridge03$string$from(rust::String *self, const char *ptr, |
| David Tolnay | 6c08910 | 2020-03-02 00:21:13 -0800 | [diff] [blame] | 33 | size_t len) noexcept; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 34 | void cxxbridge03$string$drop(rust::String *self) noexcept; |
| 35 | const char *cxxbridge03$string$ptr(const rust::String *self) noexcept; |
| 36 | size_t cxxbridge03$string$len(const rust::String *self) noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 37 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 38 | // rust::Str |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 39 | bool cxxbridge03$str$valid(const char *ptr, size_t len) noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 40 | } // extern "C" |
| 41 | |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 42 | namespace rust { |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 43 | inline namespace cxxbridge03 { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 44 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 45 | String::String() noexcept { cxxbridge03$string$new(this); } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 46 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 47 | String::String(const String &other) noexcept { |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 48 | cxxbridge03$string$clone(this, other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 51 | String::String(String &&other) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 52 | this->repr = other.repr; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 53 | cxxbridge03$string$new(&other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 56 | String::~String() noexcept { cxxbridge03$string$drop(this); } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 57 | |
| David Tolnay | c2bbd95 | 2020-07-29 18:15:26 -0700 | [diff] [blame^] | 58 | String::String(const std::string &s) : String(s.data(), s.length()) {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 59 | |
| David Tolnay | c2bbd95 | 2020-07-29 18:15:26 -0700 | [diff] [blame^] | 60 | String::String(const char *s) : String(s, std::strlen(s)) {} |
| 61 | |
| 62 | String::String(const char *s, size_t len) { |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 63 | if (!cxxbridge03$string$from(this, s, len)) { |
| David Tolnay | ce5af54 | 2020-04-10 18:08:30 -0700 | [diff] [blame] | 64 | panic<std::invalid_argument>("data for rust::String is not utf-8"); |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 65 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 66 | } |
| 67 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 68 | String &String::operator=(const String &other) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 69 | if (this != &other) { |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 70 | cxxbridge03$string$drop(this); |
| 71 | cxxbridge03$string$clone(this, other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 72 | } |
| 73 | return *this; |
| 74 | } |
| 75 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 76 | String &String::operator=(String &&other) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 77 | if (this != &other) { |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 78 | cxxbridge03$string$drop(this); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 79 | this->repr = other.repr; |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 80 | cxxbridge03$string$new(&other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 81 | } |
| 82 | return *this; |
| 83 | } |
| 84 | |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 85 | String::operator std::string() const { |
| 86 | return std::string(this->data(), this->size()); |
| 87 | } |
| 88 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 89 | const char *String::data() const noexcept { |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 90 | return cxxbridge03$string$ptr(this); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 91 | } |
| 92 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 93 | size_t String::size() const noexcept { return cxxbridge03$string$len(this); } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 94 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 95 | size_t String::length() const noexcept { return cxxbridge03$string$len(this); } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 96 | |
| David Tolnay | d1e2efc | 2020-03-03 22:25:43 -0800 | [diff] [blame] | 97 | String::String(unsafe_bitcopy_t, const String &bits) noexcept |
| 98 | : repr(bits.repr) {} |
| 99 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame] | 100 | std::ostream &operator<<(std::ostream &os, const String &s) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 101 | os.write(s.data(), s.size()); |
| 102 | return os; |
| 103 | } |
| 104 | |
| David Tolnay | 69fe4c2 | 2020-03-01 13:57:24 -0800 | [diff] [blame] | 105 | Str::Str() noexcept : repr(Repr{reinterpret_cast<const char *>(this), 0}) {} |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 106 | |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 107 | Str::Str(const Str &) noexcept = default; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 108 | |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 109 | Str::Str(const std::string &s) : repr(Repr{s.data(), s.length()}) { |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 110 | if (!cxxbridge03$str$valid(this->repr.ptr, this->repr.len)) { |
| David Tolnay | ce5af54 | 2020-04-10 18:08:30 -0700 | [diff] [blame] | 111 | panic<std::invalid_argument>("data for rust::Str is not utf-8"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| David Tolnay | bb07a4f | 2020-03-16 23:04:00 -0700 | [diff] [blame] | 115 | Str::Str(const char *s) : repr(Repr{s, std::strlen(s)}) { |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 116 | if (!cxxbridge03$str$valid(this->repr.ptr, this->repr.len)) { |
| David Tolnay | ce5af54 | 2020-04-10 18:08:30 -0700 | [diff] [blame] | 117 | panic<std::invalid_argument>("data for rust::Str is not utf-8"); |
| David Tolnay | d9c4ac9 | 2020-03-01 20:33:58 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 120 | |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 121 | Str &Str::operator=(Str other) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 122 | this->repr = other.repr; |
| 123 | return *this; |
| 124 | } |
| 125 | |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 126 | Str::operator std::string() const { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 127 | return std::string(this->data(), this->size()); |
| 128 | } |
| 129 | |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 130 | const char *Str::data() const noexcept { return this->repr.ptr; } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 131 | |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 132 | size_t Str::size() const noexcept { return this->repr.len; } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 133 | |
| David Tolnay | 09dbe75 | 2020-03-01 13:00:40 -0800 | [diff] [blame] | 134 | size_t Str::length() const noexcept { return this->repr.len; } |
| 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 | Str::Str(Repr repr_) noexcept : repr(repr_) {} |
| 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 | Str::operator Repr() noexcept { return this->repr; } |
| 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 | std::ostream &operator<<(std::ostream &os, const Str &s) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 141 | os.write(s.data(), s.size()); |
| 142 | return os; |
| 143 | } |
| 144 | |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 145 | extern "C" { |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 146 | const char *cxxbridge03$error(const char *ptr, size_t len) { |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 147 | char *copy = new char[len]; |
| 148 | strncpy(copy, ptr, len); |
| 149 | return copy; |
| 150 | } |
| 151 | } // extern "C" |
| 152 | |
| 153 | Error::Error(Str::Repr msg) noexcept : msg(msg) {} |
| 154 | |
| 155 | Error::Error(const Error &other) { |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 156 | this->msg.ptr = cxxbridge03$error(other.msg.ptr, other.msg.len); |
| David Tolnay | 1e54817 | 2020-03-16 13:37:09 -0700 | [diff] [blame] | 157 | this->msg.len = other.msg.len; |
| 158 | } |
| 159 | |
| 160 | Error::Error(Error &&other) noexcept { |
| 161 | delete[] this->msg.ptr; |
| 162 | this->msg = other.msg; |
| 163 | other.msg.ptr = nullptr; |
| 164 | other.msg.len = 0; |
| 165 | } |
| 166 | |
| 167 | Error::~Error() noexcept { delete[] this->msg.ptr; } |
| 168 | |
| 169 | const char *Error::what() const noexcept { return this->msg.ptr; } |
| 170 | |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 171 | } // namespace cxxbridge03 |
| David Tolnay | 750755e | 2020-03-01 13:04:08 -0800 | [diff] [blame] | 172 | } // namespace rust |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 173 | |
| 174 | extern "C" { |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 175 | void cxxbridge03$unique_ptr$std$string$null( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 176 | std::unique_ptr<std::string> *ptr) noexcept { |
| 177 | new (ptr) std::unique_ptr<std::string>(); |
| 178 | } |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 179 | void cxxbridge03$unique_ptr$std$string$raw(std::unique_ptr<std::string> *ptr, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 180 | std::string *raw) noexcept { |
| 181 | new (ptr) std::unique_ptr<std::string>(raw); |
| 182 | } |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 183 | const std::string *cxxbridge03$unique_ptr$std$string$get( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 184 | const std::unique_ptr<std::string> &ptr) noexcept { |
| 185 | return ptr.get(); |
| 186 | } |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 187 | std::string *cxxbridge03$unique_ptr$std$string$release( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 188 | std::unique_ptr<std::string> &ptr) noexcept { |
| 189 | return ptr.release(); |
| 190 | } |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 191 | void cxxbridge03$unique_ptr$std$string$drop( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 192 | std::unique_ptr<std::string> *ptr) noexcept { |
| 193 | ptr->~unique_ptr(); |
| 194 | } |
| 195 | } // extern "C" |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 196 | |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 197 | #define STD_VECTOR_OPS(RUST_TYPE, CXX_TYPE) \ |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 198 | size_t cxxbridge03$std$vector$##RUST_TYPE##$size( \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 199 | const std::vector<CXX_TYPE> &s) noexcept { \ |
| 200 | return s.size(); \ |
| 201 | } \ |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 202 | const CXX_TYPE *cxxbridge03$std$vector$##RUST_TYPE##$get_unchecked( \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 203 | const std::vector<CXX_TYPE> &s, size_t pos) noexcept { \ |
| David Tolnay | 9626d08 | 2020-04-24 14:52:45 -0700 | [diff] [blame] | 204 | return &s[pos]; \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 205 | } \ |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 206 | void cxxbridge03$unique_ptr$std$vector$##RUST_TYPE##$null( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 207 | std::unique_ptr<std::vector<CXX_TYPE>> *ptr) noexcept { \ |
| 208 | new (ptr) std::unique_ptr<std::vector<CXX_TYPE>>(); \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 209 | } \ |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 210 | void cxxbridge03$unique_ptr$std$vector$##RUST_TYPE##$raw( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 211 | std::unique_ptr<std::vector<CXX_TYPE>> *ptr, \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 212 | std::vector<CXX_TYPE> *raw) noexcept { \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 213 | new (ptr) std::unique_ptr<std::vector<CXX_TYPE>>(raw); \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 214 | } \ |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 215 | const std::vector<CXX_TYPE> \ |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 216 | *cxxbridge03$unique_ptr$std$vector$##RUST_TYPE##$get( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 217 | const std::unique_ptr<std::vector<CXX_TYPE>> &ptr) noexcept { \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 218 | return ptr.get(); \ |
| 219 | } \ |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 220 | std::vector<CXX_TYPE> \ |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 221 | *cxxbridge03$unique_ptr$std$vector$##RUST_TYPE##$release( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 222 | std::unique_ptr<std::vector<CXX_TYPE>> &ptr) noexcept { \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 223 | return ptr.release(); \ |
| 224 | } \ |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 225 | void cxxbridge03$unique_ptr$std$vector$##RUST_TYPE##$drop( \ |
| David Tolnay | 996db1e | 2020-04-24 14:46:31 -0700 | [diff] [blame] | 226 | std::unique_ptr<std::vector<CXX_TYPE>> *ptr) noexcept { \ |
| David Tolnay | 37dd7e1 | 2020-04-25 12:51:59 -0700 | [diff] [blame] | 227 | ptr->~unique_ptr(); \ |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 228 | } |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 229 | |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 230 | #define RUST_VEC_EXTERNS(RUST_TYPE, CXX_TYPE) \ |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 231 | void cxxbridge03$rust_vec$##RUST_TYPE##$new( \ |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 232 | rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 233 | void cxxbridge03$rust_vec$##RUST_TYPE##$drop( \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 234 | rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 235 | size_t cxxbridge03$rust_vec$##RUST_TYPE##$len( \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 236 | const rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 237 | const CXX_TYPE *cxxbridge03$rust_vec$##RUST_TYPE##$data( \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 238 | const rust::Vec<CXX_TYPE> *ptr) noexcept; \ |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 239 | size_t cxxbridge03$rust_vec$##RUST_TYPE##$stride() noexcept; |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 240 | |
| 241 | #define RUST_VEC_OPS(RUST_TYPE, CXX_TYPE) \ |
| 242 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 243 | Vec<CXX_TYPE>::Vec() noexcept { \ |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 244 | cxxbridge03$rust_vec$##RUST_TYPE##$new(this); \ |
| David Tolnay | f97c2d5 | 2020-04-25 16:37:48 -0700 | [diff] [blame] | 245 | } \ |
| 246 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 247 | void Vec<CXX_TYPE>::drop() noexcept { \ |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 248 | return cxxbridge03$rust_vec$##RUST_TYPE##$drop(this); \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 249 | } \ |
| 250 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 251 | size_t Vec<CXX_TYPE>::size() const noexcept { \ |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 252 | return cxxbridge03$rust_vec$##RUST_TYPE##$len(this); \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 253 | } \ |
| 254 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 255 | const CXX_TYPE *Vec<CXX_TYPE>::data() const noexcept { \ |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 256 | return cxxbridge03$rust_vec$##RUST_TYPE##$data(this); \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 257 | } \ |
| 258 | template <> \ |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 259 | size_t Vec<CXX_TYPE>::stride() noexcept { \ |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 260 | return cxxbridge03$rust_vec$##RUST_TYPE##$stride(); \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | // Usize and isize are the same type as one of the below. |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 264 | #define FOR_EACH_NUMERIC(MACRO) \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 265 | MACRO(u8, uint8_t) \ |
| 266 | MACRO(u16, uint16_t) \ |
| 267 | MACRO(u32, uint32_t) \ |
| 268 | MACRO(u64, uint64_t) \ |
| 269 | MACRO(i8, int8_t) \ |
| 270 | MACRO(i16, int16_t) \ |
| 271 | MACRO(i32, int32_t) \ |
| 272 | MACRO(i64, int64_t) \ |
| 273 | MACRO(f32, float) \ |
| 274 | MACRO(f64, double) |
| 275 | |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 276 | #define FOR_EACH_STD_VECTOR(MACRO) \ |
| 277 | FOR_EACH_NUMERIC(MACRO) \ |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 278 | MACRO(usize, size_t) \ |
| 279 | MACRO(isize, rust::isize) |
| 280 | |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 281 | #define FOR_EACH_RUST_VEC(MACRO) \ |
| 282 | FOR_EACH_NUMERIC(MACRO) \ |
| 283 | MACRO(bool, bool) |
| 284 | |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 285 | extern "C" { |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 286 | FOR_EACH_STD_VECTOR(STD_VECTOR_OPS) |
| 287 | FOR_EACH_RUST_VEC(RUST_VEC_EXTERNS) |
| David Tolnay | 4e7e7c4 | 2020-04-24 14:48:07 -0700 | [diff] [blame] | 288 | } // extern "C" |
| David Tolnay | 6787be6 | 2020-04-25 11:01:02 -0700 | [diff] [blame] | 289 | |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 290 | namespace rust { |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 291 | inline namespace cxxbridge03 { |
| David Tolnay | f336b3b | 2020-04-30 08:45:54 -0700 | [diff] [blame] | 292 | FOR_EACH_RUST_VEC(RUST_VEC_OPS) |
| David Tolnay | 6960162 | 2020-04-29 18:48:36 -0700 | [diff] [blame] | 293 | } // namespace cxxbridge03 |
| David Tolnay | 1768d8f | 2020-04-25 18:15:11 -0700 | [diff] [blame] | 294 | } // namespace rust |