blob: 54213c00f0eab1dd27ca9b571618b621fe2330f7 [file] [log] [blame]
David Tolnaybb3ff5d2020-11-15 19:45:11 -08001use crate::actually_private::Private;
David Tolnay3384c142020-09-14 00:26:47 -04002use alloc::borrow::Cow;
David Tolnayc5a52f92020-09-14 00:43:29 -04003use alloc::string::String;
David Tolnay05db2fb2021-03-18 13:33:27 -07004use core::cmp::Ordering;
David Tolnay3384c142020-09-14 00:26:47 -04005use core::fmt::{self, Debug, Display};
David Tolnay05db2fb2021-03-18 13:33:27 -07006use core::hash::{Hash, Hasher};
David Tolnay95dab1d2020-11-15 14:32:37 -08007use core::marker::{PhantomData, PhantomPinned};
David Tolnaybb3ff5d2020-11-15 19:45:11 -08008use core::mem::MaybeUninit;
David Tolnaya7525d32020-11-15 19:13:26 -08009use core::pin::Pin;
David Tolnay3384c142020-09-14 00:26:47 -040010use core::slice;
11use core::str::{self, Utf8Error};
David Tolnay7db73692019-10-20 14:51:12 -040012
13extern "C" {
David Tolnay0f0162f2020-11-16 23:43:37 -080014 #[link_name = "cxxbridge1$cxx_string$init"]
David Tolnaybb3ff5d2020-11-15 19:45:11 -080015 fn string_init(this: &mut MaybeUninit<CxxString>, ptr: *const u8, len: usize);
David Tolnay0f0162f2020-11-16 23:43:37 -080016 #[link_name = "cxxbridge1$cxx_string$destroy"]
David Tolnaybb3ff5d2020-11-15 19:45:11 -080017 fn string_destroy(this: &mut MaybeUninit<CxxString>);
David Tolnay0f0162f2020-11-16 23:43:37 -080018 #[link_name = "cxxbridge1$cxx_string$data"]
David Tolnay90691f42020-11-14 20:01:46 -080019 fn string_data(this: &CxxString) -> *const u8;
David Tolnay0f0162f2020-11-16 23:43:37 -080020 #[link_name = "cxxbridge1$cxx_string$length"]
David Tolnay90691f42020-11-14 20:01:46 -080021 fn string_length(this: &CxxString) -> usize;
David Tolnayf2a14662021-08-12 13:47:46 -070022 #[link_name = "cxxbridge1$cxx_string$clear"]
23 fn string_clear(this: Pin<&mut CxxString>);
David Tolnay0f0162f2020-11-16 23:43:37 -080024 #[link_name = "cxxbridge1$cxx_string$push"]
David Tolnayde1335f2020-11-15 19:47:02 -080025 fn string_push(this: Pin<&mut CxxString>, ptr: *const u8, len: usize);
David Tolnay7db73692019-10-20 14:51:12 -040026}
27
28/// Binding to C++ `std::string`.
29///
30/// # Invariants
31///
32/// As an invariant of this API and the static analysis of the cxx::bridge
33/// macro, in Rust code we can never obtain a `CxxString` by value. C++'s string
34/// requires a move constructor and may hold internal pointers, which is not
35/// compatible with Rust's move behavior. Instead in Rust code we will only ever
36/// look at a CxxString through a reference or smart pointer, as in `&CxxString`
37/// or `UniquePtr<CxxString>`.
38#[repr(C)]
39pub struct CxxString {
40 _private: [u8; 0],
David Tolnay95dab1d2020-11-15 14:32:37 -080041 _pinned: PhantomData<PhantomPinned>,
David Tolnay7db73692019-10-20 14:51:12 -040042}
43
David Tolnaybb3ff5d2020-11-15 19:45:11 -080044/// Construct a C++ std::string on the Rust stack.
45///
46/// # Syntax
47///
48/// In statement position:
49///
50/// ```
51/// # use cxx::let_cxx_string;
52/// # let expression = "";
53/// let_cxx_string!(var = expression);
54/// ```
55///
56/// The `expression` may have any type that implements `AsRef<[u8]>`. Commonly
57/// it will be a string literal, but for example `&[u8]` and `String` would work
58/// as well.
59///
60/// The macro expands to something resembling `let $var: Pin<&mut CxxString> =
61/// /*???*/;`. The resulting [`Pin`] can be deref'd to `&CxxString` as needed.
62///
63/// # Example
64///
65/// ```
66/// use cxx::{let_cxx_string, CxxString};
67///
68/// fn f(s: &CxxString) {/* ... */}
69///
70/// fn main() {
71/// let_cxx_string!(s = "example");
72/// f(&s);
73/// }
74/// ```
75#[macro_export]
76macro_rules! let_cxx_string {
77 ($var:ident = $value:expr $(,)?) => {
David Tolnayaa153ee2021-02-03 21:15:05 -080078 let mut cxx_stack_string = $crate::private::StackString::new();
David Tolnaybb3ff5d2020-11-15 19:45:11 -080079 #[allow(unused_mut, unused_unsafe)]
David Tolnayc4a3ede2020-12-27 21:45:33 -080080 let mut $var = match $value {
David Tolnayaa153ee2021-02-03 21:15:05 -080081 let_cxx_string => unsafe { cxx_stack_string.init(let_cxx_string) },
David Tolnayc4a3ede2020-12-27 21:45:33 -080082 };
David Tolnaybb3ff5d2020-11-15 19:45:11 -080083 };
84}
85
David Tolnay7db73692019-10-20 14:51:12 -040086impl CxxString {
David Tolnaybb3ff5d2020-11-15 19:45:11 -080087 /// `CxxString` is not constructible via `new`. Instead, use the
88 /// [`let_cxx_string!`] macro.
89 pub fn new<T: Private>() -> Self {
90 unreachable!()
91 }
92
David Tolnay7db73692019-10-20 14:51:12 -040093 /// Returns the length of the string in bytes.
94 ///
95 /// Matches the behavior of C++ [std::string::size][size].
96 ///
97 /// [size]: https://en.cppreference.com/w/cpp/string/basic_string/size
98 pub fn len(&self) -> usize {
99 unsafe { string_length(self) }
100 }
101
102 /// Returns true if `self` has a length of zero bytes.
David Tolnayd7b8a6e2020-04-24 16:22:55 -0700103 ///
104 /// Matches the behavior of C++ [std::string::empty][empty].
105 ///
106 /// [empty]: https://en.cppreference.com/w/cpp/string/basic_string/empty
David Tolnay7db73692019-10-20 14:51:12 -0400107 pub fn is_empty(&self) -> bool {
108 self.len() == 0
109 }
110
111 /// Returns a byte slice of this string's contents.
112 pub fn as_bytes(&self) -> &[u8] {
113 let data = self.as_ptr();
114 let len = self.len();
115 unsafe { slice::from_raw_parts(data, len) }
116 }
117
118 /// Produces a pointer to the first character of the string.
119 ///
120 /// Matches the behavior of C++ [std::string::data][data].
121 ///
122 /// Note that the return type may look like `const char *` but is not a
123 /// `const char *` in the typical C sense, as C++ strings may contain
124 /// internal null bytes. As such, the returned pointer only makes sense as a
David Tolnay3cd990f2020-04-24 16:24:26 -0700125 /// string in combination with the length returned by [`len()`][len].
David Tolnay7db73692019-10-20 14:51:12 -0400126 ///
127 /// [data]: https://en.cppreference.com/w/cpp/string/basic_string/data
David Tolnay3cd990f2020-04-24 16:24:26 -0700128 /// [len]: #method.len
David Tolnay7db73692019-10-20 14:51:12 -0400129 pub fn as_ptr(&self) -> *const u8 {
130 unsafe { string_data(self) }
131 }
132
133 /// Validates that the C++ string contains UTF-8 data and produces a view of
134 /// it as a Rust &amp;str, otherwise an error.
135 pub fn to_str(&self) -> Result<&str, Utf8Error> {
136 str::from_utf8(self.as_bytes())
137 }
138
139 /// If the contents of the C++ string are valid UTF-8, this function returns
140 /// a view as a Cow::Borrowed &amp;str. Otherwise replaces any invalid UTF-8
141 /// sequences with the U+FFFD [replacement character] and returns a
142 /// Cow::Owned String.
143 ///
144 /// [replacement character]: https://doc.rust-lang.org/std/char/constant.REPLACEMENT_CHARACTER.html
145 pub fn to_string_lossy(&self) -> Cow<str> {
146 String::from_utf8_lossy(self.as_bytes())
147 }
David Tolnay90691f42020-11-14 20:01:46 -0800148
David Tolnayf2a14662021-08-12 13:47:46 -0700149 /// Removes all characters from the string.
150 ///
151 /// Matches the behavior of C++ [std::string::clear][clear].
152 ///
153 /// Note: **unlike** the guarantee of Rust's `std::string::String::clear`,
154 /// the C++ standard does not require that capacity is unchanged by this
155 /// operation. In practice existing implementations do not change the
156 /// capacity but all pointers, references, and iterators into the string
157 /// contents are nevertheless invalidated.
158 ///
159 /// [clear]: https://en.cppreference.com/w/cpp/string/basic_string/clear
160 pub fn clear(self: Pin<&mut Self>) {
161 unsafe { string_clear(self) }
162 }
163
David Tolnay90691f42020-11-14 20:01:46 -0800164 /// Appends a given string slice onto the end of this C++ string.
David Tolnaya7525d32020-11-15 19:13:26 -0800165 pub fn push_str(self: Pin<&mut Self>, s: &str) {
David Tolnay95e74b32020-11-14 20:16:22 -0800166 self.push_bytes(s.as_bytes());
167 }
168
169 /// Appends arbitrary bytes onto the end of this C++ string.
David Tolnaya7525d32020-11-15 19:13:26 -0800170 pub fn push_bytes(self: Pin<&mut Self>, bytes: &[u8]) {
David Tolnayde1335f2020-11-15 19:47:02 -0800171 unsafe { string_push(self, bytes.as_ptr(), bytes.len()) }
David Tolnay90691f42020-11-14 20:01:46 -0800172 }
David Tolnay7db73692019-10-20 14:51:12 -0400173}
174
175impl Display for CxxString {
176 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
David Tolnayd930a792020-03-25 12:24:40 -0700177 Display::fmt(self.to_string_lossy().as_ref(), f)
David Tolnay7db73692019-10-20 14:51:12 -0400178 }
179}
180
181impl Debug for CxxString {
182 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
David Tolnayd930a792020-03-25 12:24:40 -0700183 Debug::fmt(self.to_string_lossy().as_ref(), f)
David Tolnay7db73692019-10-20 14:51:12 -0400184 }
185}
David Tolnay42ebfa22020-03-25 12:26:22 -0700186
187impl PartialEq for CxxString {
David Tolnay2e43b512021-03-18 13:35:38 -0700188 fn eq(&self, other: &Self) -> bool {
David Tolnay42ebfa22020-03-25 12:26:22 -0700189 self.as_bytes() == other.as_bytes()
190 }
191}
192
193impl PartialEq<CxxString> for str {
194 fn eq(&self, other: &CxxString) -> bool {
195 self.as_bytes() == other.as_bytes()
196 }
197}
198
199impl PartialEq<str> for CxxString {
200 fn eq(&self, other: &str) -> bool {
201 self.as_bytes() == other.as_bytes()
202 }
203}
David Tolnaybb3ff5d2020-11-15 19:45:11 -0800204
b059021327b783f92021-03-19 01:48:36 +0800205impl Eq for CxxString {}
206
207impl PartialOrd for CxxString {
David Tolnay05db2fb2021-03-18 13:33:27 -0700208 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
b059021327b783f92021-03-19 01:48:36 +0800209 self.as_bytes().partial_cmp(other.as_bytes())
210 }
211}
212
b059021327b783f92021-03-19 01:48:36 +0800213impl Ord for CxxString {
David Tolnay05db2fb2021-03-18 13:33:27 -0700214 fn cmp(&self, other: &Self) -> Ordering {
b059021327b783f92021-03-19 01:48:36 +0800215 self.as_bytes().cmp(other.as_bytes())
216 }
217}
218
David Tolnay05db2fb2021-03-18 13:33:27 -0700219impl Hash for CxxString {
220 fn hash<H: Hasher>(&self, state: &mut H) {
221 self.as_bytes().hash(state);
b059021327b783f92021-03-19 01:48:36 +0800222 }
223}
224
David Tolnaybb3ff5d2020-11-15 19:45:11 -0800225#[doc(hidden)]
226#[repr(C)]
227pub struct StackString {
228 // Static assertions in cxx.cc validate that this is large enough and
229 // aligned enough.
David Tolnay0df1fdb2021-01-27 11:26:36 -0800230 space: MaybeUninit<[usize; 8]>,
David Tolnaybb3ff5d2020-11-15 19:45:11 -0800231}
232
David Tolnaycb07a842021-04-16 16:08:52 -0700233#[allow(missing_docs)]
David Tolnaybb3ff5d2020-11-15 19:45:11 -0800234impl StackString {
235 pub fn new() -> Self {
236 StackString {
237 space: MaybeUninit::uninit(),
238 }
239 }
240
241 pub unsafe fn init(&mut self, value: impl AsRef<[u8]>) -> Pin<&mut CxxString> {
242 let value = value.as_ref();
243 let this = &mut *self.space.as_mut_ptr().cast::<MaybeUninit<CxxString>>();
244 string_init(this, value.as_ptr(), value.len());
245 Pin::new_unchecked(&mut *this.as_mut_ptr())
246 }
247}
248
249impl Drop for StackString {
250 fn drop(&mut self) {
251 unsafe {
252 let this = &mut *self.space.as_mut_ptr().cast::<MaybeUninit<CxxString>>();
253 string_destroy(this);
254 }
255 }
256}