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