blob: 1ac313343c1c8d1cfe727470cdb2c6d86b198542 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001#pragma once
2#include <array>
3#include <cstdint>
David Tolnay001102a2020-03-01 20:05:04 -08004#include <iosfwd>
David Tolnay7db73692019-10-20 14:51:12 -04005#include <string>
David Tolnayf6292372020-03-01 21:09:11 -08006#include <type_traits>
David Tolnay7db73692019-10-20 14:51:12 -04007
David Tolnay750755e2020-03-01 13:04:08 -08008namespace rust {
9inline namespace cxxbridge01 {
David Tolnay7db73692019-10-20 14:51:12 -040010
David Tolnayd1e2efc2020-03-03 22:25:43 -080011struct unsafe_bitcopy_t;
12
David Tolnay56082162020-03-01 12:57:33 -080013class String final {
David Tolnay7db73692019-10-20 14:51:12 -040014public:
David Tolnay56082162020-03-01 12:57:33 -080015 String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080016 String(const String &) noexcept;
17 String(String &&) noexcept;
David Tolnay56082162020-03-01 12:57:33 -080018 ~String() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080019
20 String(const std::string &);
21 String(const char *);
22
23 String &operator=(const String &) noexcept;
24 String &operator=(String &&) noexcept;
25
David Tolnay404d6892020-03-01 20:19:41 -080026 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040027
28 // Note: no null terminator.
29 const char *data() const noexcept;
30 size_t size() const noexcept;
31 size_t length() const noexcept;
32
David Tolnayd1e2efc2020-03-03 22:25:43 -080033 // Internal API only intended for the cxxbridge code generator.
34 String(unsafe_bitcopy_t, const String &) noexcept;
35
David Tolnay7db73692019-10-20 14:51:12 -040036private:
37 // Size and alignment statically verified by rust_string.rs.
38 std::array<uintptr_t, 3> repr;
39};
40
David Tolnay09dbe752020-03-01 13:00:40 -080041class Str final {
David Tolnay7db73692019-10-20 14:51:12 -040042public:
David Tolnay09dbe752020-03-01 13:00:40 -080043 Str() noexcept;
David Tolnayd9c4ac92020-03-01 20:33:58 -080044 Str(const Str &) noexcept;
45
David Tolnay851677c2020-03-01 23:49:46 -080046 Str(const std::string &);
47 Str(const char *);
48 Str(std::string &&) = delete;
David Tolnayd9c4ac92020-03-01 20:33:58 -080049
50 Str &operator=(Str) noexcept;
51
David Tolnay404d6892020-03-01 20:19:41 -080052 explicit operator std::string() const;
David Tolnay7db73692019-10-20 14:51:12 -040053
54 // Note: no null terminator.
55 const char *data() const noexcept;
56 size_t size() const noexcept;
57 size_t length() const noexcept;
58
59 // Repr is PRIVATE; must not be used other than by our generated code.
60 //
61 // Not necessarily ABI compatible with &str. Codegen will translate to
62 // cxx::rust_str::RustStr which matches this layout.
63 struct Repr {
64 const char *ptr;
65 size_t len;
66 };
David Tolnayd9c4ac92020-03-01 20:33:58 -080067 Str(Repr) noexcept;
David Tolnaybaae4432020-03-01 20:20:10 -080068 explicit operator Repr() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -040069
70private:
71 Repr repr;
72};
73
David Tolnaye43b7372020-01-08 08:46:20 -080074#ifndef CXXBRIDGE01_RUST_BOX
75#define CXXBRIDGE01_RUST_BOX
David Tolnay324437a2020-03-01 13:02:24 -080076template <typename T> class Box final {
David Tolnay7db73692019-10-20 14:51:12 -040077public:
David Tolnayf6292372020-03-01 21:09:11 -080078 using value_type = T;
David Tolnay9f921372020-03-01 21:09:25 -080079 using const_pointer = typename std::add_pointer<
80 typename std::add_const<value_type>::type>::type;
81 using pointer = typename std::add_pointer<value_type>::type;
David Tolnayf6292372020-03-01 21:09:11 -080082
David Tolnay324437a2020-03-01 13:02:24 -080083 Box(const Box &other) : Box(*other) {}
84 Box(Box &&other) noexcept : repr(other.repr) { other.repr = 0; }
85 Box(const T &val) {
David Tolnay7db73692019-10-20 14:51:12 -040086 this->uninit();
David Tolnay90838582020-02-23 00:57:03 -080087 ::new (this->deref_mut()) T(val);
David Tolnay7db73692019-10-20 14:51:12 -040088 }
David Tolnay324437a2020-03-01 13:02:24 -080089 Box &operator=(const Box &other) {
David Tolnay7db73692019-10-20 14:51:12 -040090 if (this != &other) {
91 if (this->repr) {
92 **this = *other;
93 } else {
94 this->uninit();
David Tolnay90838582020-02-23 00:57:03 -080095 ::new (this->deref_mut()) T(*other);
David Tolnay7db73692019-10-20 14:51:12 -040096 }
97 }
98 return *this;
99 }
David Tolnay324437a2020-03-01 13:02:24 -0800100 Box &operator=(Box &&other) noexcept {
David Tolnay7db73692019-10-20 14:51:12 -0400101 if (this->repr) {
102 this->drop();
103 }
104 this->repr = other.repr;
105 other.repr = 0;
106 return *this;
107 }
David Tolnay324437a2020-03-01 13:02:24 -0800108 ~Box() noexcept {
David Tolnay7db73692019-10-20 14:51:12 -0400109 if (this->repr) {
110 this->drop();
111 }
112 }
113
114 const T *operator->() const noexcept { return this->deref(); }
115 const T &operator*() const noexcept { return *this->deref(); }
116 T *operator->() noexcept { return this->deref_mut(); }
117 T &operator*() noexcept { return *this->deref_mut(); }
118
119 // Important: requires that `raw` came from an into_raw call. Do not pass a
120 // pointer from `new` or any other source.
David Tolnay324437a2020-03-01 13:02:24 -0800121 static Box from_raw(T *raw) noexcept {
122 Box box;
David Tolnay7db73692019-10-20 14:51:12 -0400123 box.set_raw(raw);
124 return box;
125 }
126
127 T *into_raw() noexcept {
128 T *raw = this->deref_mut();
129 this->repr = 0;
130 return raw;
131 }
132
133private:
David Tolnay324437a2020-03-01 13:02:24 -0800134 Box() noexcept {}
David Tolnay7db73692019-10-20 14:51:12 -0400135 void uninit() noexcept;
David Tolnay2248c302020-03-01 21:13:32 -0800136 void set_raw(pointer) noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400137 void drop() noexcept;
David Tolnay2248c302020-03-01 21:13:32 -0800138 const_pointer deref() const noexcept;
139 pointer deref_mut() noexcept;
David Tolnay7db73692019-10-20 14:51:12 -0400140 uintptr_t repr;
141};
David Tolnaye43b7372020-01-08 08:46:20 -0800142#endif // CXXBRIDGE01_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400143
David Tolnay851677c2020-03-01 23:49:46 -0800144std::ostream &operator<<(std::ostream &, const String &);
145std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400146
David Tolnay3b0c9882020-03-01 14:08:57 -0800147// Snake case aliases for use in code that uses this style for type names.
148using string = String;
149using str = Str;
150template <class T> using box = Box<T>;
151
David Tolnayd1e2efc2020-03-03 22:25:43 -0800152struct unsafe_bitcopy_t {
153 explicit unsafe_bitcopy_t() = default;
154};
155constexpr unsafe_bitcopy_t unsafe_bitcopy{};
156
David Tolnay69fe4c22020-03-01 13:57:24 -0800157} // namespace cxxbridge01
David Tolnay750755e2020-03-01 13:04:08 -0800158} // namespace rust