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