| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1 | #include "../include/cxxbridge.h" |
| 2 | #include <cstring> |
| 3 | #include <memory> |
| 4 | #include <stdexcept> |
| 5 | |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 6 | namespace cxxbridge = cxxbridge01; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 7 | |
| 8 | extern "C" { |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 9 | const char *cxxbridge01$cxx_string$data(const std::string &s) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 10 | return s.data(); |
| 11 | } |
| 12 | |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 13 | size_t cxxbridge01$cxx_string$length(const std::string &s) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 14 | return s.length(); |
| 15 | } |
| 16 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame^] | 17 | // cxxbridge::String |
| 18 | void cxxbridge01$rust_string$new(cxxbridge::String *self) noexcept; |
| 19 | void cxxbridge01$rust_string$clone(cxxbridge::String *self, |
| 20 | const cxxbridge::String &other) noexcept; |
| 21 | bool cxxbridge01$rust_string$from(cxxbridge::String *self, const char *ptr, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 22 | size_t len) noexcept; |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame^] | 23 | void cxxbridge01$rust_string$drop(cxxbridge::String *self) noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 24 | const char * |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame^] | 25 | cxxbridge01$rust_string$ptr(const cxxbridge::String *self) noexcept; |
| 26 | size_t cxxbridge01$rust_string$len(const cxxbridge::String *self) noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 27 | |
| 28 | // RustStr |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 29 | bool cxxbridge01$rust_str$valid(const char *ptr, size_t len) noexcept; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 30 | } // extern "C" |
| 31 | |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 32 | namespace cxxbridge01 { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 33 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame^] | 34 | String::String() noexcept { cxxbridge01$rust_string$new(this); } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 35 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame^] | 36 | String::String(const String &other) noexcept { |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 37 | cxxbridge01$rust_string$clone(this, other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 38 | } |
| 39 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame^] | 40 | String::String(String &&other) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 41 | this->repr = other.repr; |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 42 | cxxbridge01$rust_string$new(&other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 43 | } |
| 44 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame^] | 45 | String::String(const char *s) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 46 | auto len = strlen(s); |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 47 | if (!cxxbridge01$rust_string$from(this, s, len)) { |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame^] | 48 | throw std::invalid_argument("data for cxxbridge::String is not utf-8"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame^] | 52 | String::String(const std::string &s) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 53 | auto ptr = s.data(); |
| 54 | auto len = s.length(); |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 55 | if (!cxxbridge01$rust_string$from(this, ptr, len)) { |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame^] | 56 | throw std::invalid_argument("data for cxxbridge::String is not utf-8"); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame^] | 60 | String::~String() noexcept { cxxbridge01$rust_string$drop(this); } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 61 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame^] | 62 | String::operator std::string() const { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 63 | return std::string(this->data(), this->size()); |
| 64 | } |
| 65 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame^] | 66 | String &String::operator=(const String &other) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 67 | if (this != &other) { |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 68 | cxxbridge01$rust_string$drop(this); |
| 69 | cxxbridge01$rust_string$clone(this, other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 70 | } |
| 71 | return *this; |
| 72 | } |
| 73 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame^] | 74 | String &String::operator=(String &&other) noexcept { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 75 | if (this != &other) { |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 76 | cxxbridge01$rust_string$drop(this); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 77 | this->repr = other.repr; |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 78 | cxxbridge01$rust_string$new(&other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 79 | } |
| 80 | return *this; |
| 81 | } |
| 82 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame^] | 83 | const char *String::data() const noexcept { |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 84 | return cxxbridge01$rust_string$ptr(this); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame^] | 87 | size_t String::size() const noexcept { |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 88 | return cxxbridge01$rust_string$len(this); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame^] | 91 | size_t String::length() const noexcept { |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 92 | return cxxbridge01$rust_string$len(this); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| David Tolnay | 5608216 | 2020-03-01 12:57:33 -0800 | [diff] [blame^] | 95 | std::ostream &operator<<(std::ostream &os, const String &s) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 96 | os.write(s.data(), s.size()); |
| 97 | return os; |
| 98 | } |
| 99 | |
| 100 | RustStr::RustStr() noexcept |
| 101 | : repr(Repr{reinterpret_cast<const char *>(this), 0}) {} |
| 102 | |
| 103 | RustStr::RustStr(const char *s) : repr(Repr{s, strlen(s)}) { |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 104 | if (!cxxbridge01$rust_str$valid(this->repr.ptr, this->repr.len)) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 105 | throw std::invalid_argument("data for RustStr is not utf-8"); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | RustStr::RustStr(const std::string &s) : repr(Repr{s.data(), s.length()}) { |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 110 | if (!cxxbridge01$rust_str$valid(this->repr.ptr, this->repr.len)) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 111 | throw std::invalid_argument("data for RustStr is not utf-8"); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | RustStr::RustStr(const RustStr &) noexcept = default; |
| 116 | |
| 117 | RustStr &RustStr::operator=(RustStr other) noexcept { |
| 118 | this->repr = other.repr; |
| 119 | return *this; |
| 120 | } |
| 121 | |
| 122 | RustStr::operator std::string() const { |
| 123 | return std::string(this->data(), this->size()); |
| 124 | } |
| 125 | |
| 126 | const char *RustStr::data() const noexcept { return this->repr.ptr; } |
| 127 | |
| 128 | size_t RustStr::size() const noexcept { return this->repr.len; } |
| 129 | |
| 130 | size_t RustStr::length() const noexcept { return this->repr.len; } |
| 131 | |
| 132 | RustStr::RustStr(Repr repr_) noexcept : repr(repr_) {} |
| 133 | |
| 134 | RustStr::operator Repr() noexcept { return this->repr; } |
| 135 | |
| 136 | std::ostream &operator<<(std::ostream &os, const RustStr &s) { |
| 137 | os.write(s.data(), s.size()); |
| 138 | return os; |
| 139 | } |
| 140 | |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 141 | } // namespace cxxbridge01 |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 142 | |
| 143 | extern "C" { |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 144 | void cxxbridge01$unique_ptr$std$string$null( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 145 | std::unique_ptr<std::string> *ptr) noexcept { |
| 146 | new (ptr) std::unique_ptr<std::string>(); |
| 147 | } |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 148 | void cxxbridge01$unique_ptr$std$string$new(std::unique_ptr<std::string> *ptr, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 149 | std::string *value) noexcept { |
| 150 | new (ptr) std::unique_ptr<std::string>(new std::string(std::move(*value))); |
| 151 | } |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 152 | void cxxbridge01$unique_ptr$std$string$raw(std::unique_ptr<std::string> *ptr, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 153 | std::string *raw) noexcept { |
| 154 | new (ptr) std::unique_ptr<std::string>(raw); |
| 155 | } |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 156 | const std::string *cxxbridge01$unique_ptr$std$string$get( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 157 | const std::unique_ptr<std::string> &ptr) noexcept { |
| 158 | return ptr.get(); |
| 159 | } |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 160 | std::string *cxxbridge01$unique_ptr$std$string$release( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 161 | std::unique_ptr<std::string> &ptr) noexcept { |
| 162 | return ptr.release(); |
| 163 | } |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 164 | void cxxbridge01$unique_ptr$std$string$drop( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 165 | std::unique_ptr<std::string> *ptr) noexcept { |
| 166 | ptr->~unique_ptr(); |
| 167 | } |
| 168 | } // extern "C" |