| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | #include <array> |
| 3 | #include <cstdint> |
| 4 | #include <iostream> |
| 5 | #include <string> |
| 6 | |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 7 | namespace cxxbridge01 { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 8 | |
| 9 | class RustString final { |
| 10 | public: |
| 11 | RustString() noexcept; |
| 12 | RustString(const RustString &other) noexcept; |
| 13 | RustString(RustString &&other) noexcept; |
| 14 | RustString(const char *s); |
| 15 | RustString(const std::string &s); |
| 16 | RustString &operator=(const RustString &other) noexcept; |
| 17 | RustString &operator=(RustString &&other) noexcept; |
| 18 | ~RustString() noexcept; |
| 19 | operator std::string() const; |
| 20 | |
| 21 | // Note: no null terminator. |
| 22 | const char *data() const noexcept; |
| 23 | size_t size() const noexcept; |
| 24 | size_t length() const noexcept; |
| 25 | |
| 26 | private: |
| 27 | // Size and alignment statically verified by rust_string.rs. |
| 28 | std::array<uintptr_t, 3> repr; |
| 29 | }; |
| 30 | |
| 31 | class RustStr final { |
| 32 | public: |
| 33 | RustStr() noexcept; |
| 34 | RustStr(const char *s); |
| 35 | RustStr(const std::string &s); |
| 36 | RustStr(std::string &&s) = delete; |
| 37 | RustStr(const RustStr &other) noexcept; |
| 38 | RustStr &operator=(RustStr other) noexcept; |
| 39 | operator std::string() const; |
| 40 | |
| 41 | // Note: no null terminator. |
| 42 | const char *data() const noexcept; |
| 43 | size_t size() const noexcept; |
| 44 | size_t length() const noexcept; |
| 45 | |
| 46 | // Repr is PRIVATE; must not be used other than by our generated code. |
| 47 | // |
| 48 | // Not necessarily ABI compatible with &str. Codegen will translate to |
| 49 | // cxx::rust_str::RustStr which matches this layout. |
| 50 | struct Repr { |
| 51 | const char *ptr; |
| 52 | size_t len; |
| 53 | }; |
| 54 | RustStr(Repr repr) noexcept; |
| 55 | operator Repr() noexcept; |
| 56 | |
| 57 | private: |
| 58 | Repr repr; |
| 59 | }; |
| 60 | |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 61 | #ifndef CXXBRIDGE01_RUST_BOX |
| 62 | #define CXXBRIDGE01_RUST_BOX |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 63 | template <typename T> class RustBox final { |
| 64 | public: |
| 65 | RustBox(const RustBox &other) : RustBox(*other) {} |
| 66 | RustBox(RustBox &&other) noexcept : repr(other.repr) { other.repr = 0; } |
| 67 | RustBox(const T &val) { |
| 68 | this->uninit(); |
| David Tolnay | 9083858 | 2020-02-23 00:57:03 -0800 | [diff] [blame^] | 69 | ::new (this->deref_mut()) T(val); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 70 | } |
| 71 | RustBox &operator=(const RustBox &other) { |
| 72 | if (this != &other) { |
| 73 | if (this->repr) { |
| 74 | **this = *other; |
| 75 | } else { |
| 76 | this->uninit(); |
| David Tolnay | 9083858 | 2020-02-23 00:57:03 -0800 | [diff] [blame^] | 77 | ::new (this->deref_mut()) T(*other); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | return *this; |
| 81 | } |
| 82 | RustBox &operator=(RustBox &&other) noexcept { |
| 83 | if (this->repr) { |
| 84 | this->drop(); |
| 85 | } |
| 86 | this->repr = other.repr; |
| 87 | other.repr = 0; |
| 88 | return *this; |
| 89 | } |
| 90 | ~RustBox() noexcept { |
| 91 | if (this->repr) { |
| 92 | this->drop(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | const T *operator->() const noexcept { return this->deref(); } |
| 97 | const T &operator*() const noexcept { return *this->deref(); } |
| 98 | T *operator->() noexcept { return this->deref_mut(); } |
| 99 | T &operator*() noexcept { return *this->deref_mut(); } |
| 100 | |
| 101 | // Important: requires that `raw` came from an into_raw call. Do not pass a |
| 102 | // pointer from `new` or any other source. |
| 103 | static RustBox from_raw(T *raw) noexcept { |
| 104 | RustBox box; |
| 105 | box.set_raw(raw); |
| 106 | return box; |
| 107 | } |
| 108 | |
| 109 | T *into_raw() noexcept { |
| 110 | T *raw = this->deref_mut(); |
| 111 | this->repr = 0; |
| 112 | return raw; |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | RustBox() noexcept {} |
| 117 | void uninit() noexcept; |
| 118 | void set_raw(T *) noexcept; |
| 119 | T *get_raw() noexcept; |
| 120 | void drop() noexcept; |
| 121 | const T *deref() const noexcept; |
| 122 | T *deref_mut() noexcept; |
| 123 | uintptr_t repr; |
| 124 | }; |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 125 | #endif // CXXBRIDGE01_RUST_BOX |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 126 | |
| 127 | std::ostream &operator<<(std::ostream &os, const RustString &s); |
| 128 | std::ostream &operator<<(std::ostream &os, const RustStr &s); |
| 129 | |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 130 | } // namespace cxxbridge01 |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 131 | |
| David Tolnay | e43b737 | 2020-01-08 08:46:20 -0800 | [diff] [blame] | 132 | namespace cxxbridge = cxxbridge01; |