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