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