blob: e02145bb66d1b0b0590e4944d85158c0d3efa056 [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 {
David Tolnay8c730492020-03-13 01:29:06 -07009inline namespace cxxbridge02 {
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 Tolnay8c730492020-03-13 01:29:06 -070074#ifndef CXXBRIDGE02_RUST_BOX
75#define CXXBRIDGE02_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) {}
David Tolnay33169bd2020-03-06 13:02:08 -080084 Box(Box &&other) noexcept : ptr(other.ptr) { other.ptr = nullptr; }
David Tolnay324437a2020-03-01 13:02:24 -080085 Box(const T &val) {
David Tolnay7db73692019-10-20 14:51:12 -040086 this->uninit();
David Tolnay33169bd2020-03-06 13:02:08 -080087 ::new (this->ptr) 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) {
David Tolnay33169bd2020-03-06 13:02:08 -080091 if (this->ptr) {
David Tolnay7db73692019-10-20 14:51:12 -040092 **this = *other;
93 } else {
94 this->uninit();
David Tolnay33169bd2020-03-06 13:02:08 -080095 ::new (this->ptr) 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 Tolnay33169bd2020-03-06 13:02:08 -0800101 if (this->ptr) {
David Tolnay7db73692019-10-20 14:51:12 -0400102 this->drop();
103 }
David Tolnay33169bd2020-03-06 13:02:08 -0800104 this->ptr = other.ptr;
105 other.ptr = nullptr;
David Tolnay7db73692019-10-20 14:51:12 -0400106 return *this;
107 }
David Tolnay324437a2020-03-01 13:02:24 -0800108 ~Box() noexcept {
David Tolnay33169bd2020-03-06 13:02:08 -0800109 if (this->ptr) {
David Tolnay7db73692019-10-20 14:51:12 -0400110 this->drop();
111 }
112 }
113
David Tolnay33169bd2020-03-06 13:02:08 -0800114 const T *operator->() const noexcept { return this->ptr; }
115 const T &operator*() const noexcept { return *this->ptr; }
116 T *operator->() noexcept { return this->ptr; }
117 T &operator*() noexcept { return *this->ptr; }
David Tolnay7db73692019-10-20 14:51:12 -0400118
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 Tolnay33169bd2020-03-06 13:02:08 -0800123 box.ptr = raw;
David Tolnay7db73692019-10-20 14:51:12 -0400124 return box;
125 }
126
127 T *into_raw() noexcept {
David Tolnay33169bd2020-03-06 13:02:08 -0800128 T *raw = this->ptr;
129 this->ptr = nullptr;
David Tolnay7db73692019-10-20 14:51:12 -0400130 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 Tolnay7db73692019-10-20 14:51:12 -0400136 void drop() noexcept;
David Tolnay33169bd2020-03-06 13:02:08 -0800137 T *ptr;
David Tolnay7db73692019-10-20 14:51:12 -0400138};
David Tolnay8c730492020-03-13 01:29:06 -0700139#endif // CXXBRIDGE02_RUST_BOX
David Tolnay7db73692019-10-20 14:51:12 -0400140
David Tolnay851677c2020-03-01 23:49:46 -0800141std::ostream &operator<<(std::ostream &, const String &);
142std::ostream &operator<<(std::ostream &, const Str &);
David Tolnay7db73692019-10-20 14:51:12 -0400143
David Tolnay3b0c9882020-03-01 14:08:57 -0800144// Snake case aliases for use in code that uses this style for type names.
145using string = String;
146using str = Str;
147template <class T> using box = Box<T>;
148
David Tolnayd1e2efc2020-03-03 22:25:43 -0800149struct unsafe_bitcopy_t {
150 explicit unsafe_bitcopy_t() = default;
151};
152constexpr unsafe_bitcopy_t unsafe_bitcopy{};
153
David Tolnay8c730492020-03-13 01:29:06 -0700154} // namespace cxxbridge02
David Tolnay750755e2020-03-01 13:04:08 -0800155} // namespace rust