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