blob: a2bd72bbfa73995438f1dd494cfd3505ef48ec08 [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 Tolnay3384c142020-09-14 00:26:47 -04004use core::fmt::{self, Debug, Display};
David Tolnay95dab1d2020-11-15 14:32:37 -08005use core::marker::{PhantomData, PhantomPinned};
David Tolnaybb3ff5d2020-11-15 19:45:11 -08006use core::mem::MaybeUninit;
David Tolnaya7525d32020-11-15 19:13:26 -08007use core::pin::Pin;
David Tolnay3384c142020-09-14 00:26:47 -04008use core::slice;
9use core::str::{self, Utf8Error};
David Tolnay7db73692019-10-20 14:51:12 -040010
11extern "C" {
David Tolnay0f0162f2020-11-16 23:43:37 -080012 #[link_name = "cxxbridge1$cxx_string$init"]
David Tolnaybb3ff5d2020-11-15 19:45:11 -080013 fn string_init(this: &mut MaybeUninit<CxxString>, ptr: *const u8, len: usize);
David Tolnay0f0162f2020-11-16 23:43:37 -080014 #[link_name = "cxxbridge1$cxx_string$destroy"]
David Tolnaybb3ff5d2020-11-15 19:45:11 -080015 fn string_destroy(this: &mut MaybeUninit<CxxString>);
David Tolnay0f0162f2020-11-16 23:43:37 -080016 #[link_name = "cxxbridge1$cxx_string$data"]
David Tolnay90691f42020-11-14 20:01:46 -080017 fn string_data(this: &CxxString) -> *const u8;
David Tolnay0f0162f2020-11-16 23:43:37 -080018 #[link_name = "cxxbridge1$cxx_string$length"]
David Tolnay90691f42020-11-14 20:01:46 -080019 fn string_length(this: &CxxString) -> usize;
David Tolnay0f0162f2020-11-16 23:43:37 -080020 #[link_name = "cxxbridge1$cxx_string$push"]
David Tolnayde1335f2020-11-15 19:47:02 -080021 fn string_push(this: Pin<&mut CxxString>, ptr: *const u8, len: usize);
David Tolnay7db73692019-10-20 14:51:12 -040022}
23
24/// Binding to C++ `std::string`.
25///
26/// # Invariants
27///
28/// As an invariant of this API and the static analysis of the cxx::bridge
29/// macro, in Rust code we can never obtain a `CxxString` by value. C++'s string
30/// requires a move constructor and may hold internal pointers, which is not
31/// compatible with Rust's move behavior. Instead in Rust code we will only ever
32/// look at a CxxString through a reference or smart pointer, as in `&CxxString`
33/// or `UniquePtr<CxxString>`.
34#[repr(C)]
35pub struct CxxString {
36 _private: [u8; 0],
David Tolnay95dab1d2020-11-15 14:32:37 -080037 _pinned: PhantomData<PhantomPinned>,
David Tolnay7db73692019-10-20 14:51:12 -040038}
39
David Tolnaybb3ff5d2020-11-15 19:45:11 -080040/// Construct a C++ std::string on the Rust stack.
41///
42/// # Syntax
43///
44/// In statement position:
45///
46/// ```
47/// # use cxx::let_cxx_string;
48/// # let expression = "";
49/// let_cxx_string!(var = expression);
50/// ```
51///
52/// The `expression` may have any type that implements `AsRef<[u8]>`. Commonly
53/// it will be a string literal, but for example `&[u8]` and `String` would work
54/// as well.
55///
56/// The macro expands to something resembling `let $var: Pin<&mut CxxString> =
57/// /*???*/;`. The resulting [`Pin`] can be deref'd to `&CxxString` as needed.
58///
59/// # Example
60///
61/// ```
62/// use cxx::{let_cxx_string, CxxString};
63///
64/// fn f(s: &CxxString) {/* ... */}
65///
66/// fn main() {
67/// let_cxx_string!(s = "example");
68/// f(&s);
69/// }
70/// ```
71#[macro_export]
72macro_rules! let_cxx_string {
73 ($var:ident = $value:expr $(,)?) => {
74 let mut $var = $crate::private::StackString::new();
75 #[allow(unused_mut, unused_unsafe)]
76 let mut $var = unsafe { $var.init($value) };
77 };
78}
79
David Tolnay7db73692019-10-20 14:51:12 -040080impl CxxString {
David Tolnaybb3ff5d2020-11-15 19:45:11 -080081 /// `CxxString` is not constructible via `new`. Instead, use the
82 /// [`let_cxx_string!`] macro.
83 pub fn new<T: Private>() -> Self {
84 unreachable!()
85 }
86
David Tolnay7db73692019-10-20 14:51:12 -040087 /// Returns the length of the string in bytes.
88 ///
89 /// Matches the behavior of C++ [std::string::size][size].
90 ///
91 /// [size]: https://en.cppreference.com/w/cpp/string/basic_string/size
92 pub fn len(&self) -> usize {
93 unsafe { string_length(self) }
94 }
95
96 /// Returns true if `self` has a length of zero bytes.
David Tolnayd7b8a6e2020-04-24 16:22:55 -070097 ///
98 /// Matches the behavior of C++ [std::string::empty][empty].
99 ///
100 /// [empty]: https://en.cppreference.com/w/cpp/string/basic_string/empty
David Tolnay7db73692019-10-20 14:51:12 -0400101 pub fn is_empty(&self) -> bool {
102 self.len() == 0
103 }
104
105 /// Returns a byte slice of this string's contents.
106 pub fn as_bytes(&self) -> &[u8] {
107 let data = self.as_ptr();
108 let len = self.len();
109 unsafe { slice::from_raw_parts(data, len) }
110 }
111
112 /// Produces a pointer to the first character of the string.
113 ///
114 /// Matches the behavior of C++ [std::string::data][data].
115 ///
116 /// Note that the return type may look like `const char *` but is not a
117 /// `const char *` in the typical C sense, as C++ strings may contain
118 /// internal null bytes. As such, the returned pointer only makes sense as a
David Tolnay3cd990f2020-04-24 16:24:26 -0700119 /// string in combination with the length returned by [`len()`][len].
David Tolnay7db73692019-10-20 14:51:12 -0400120 ///
121 /// [data]: https://en.cppreference.com/w/cpp/string/basic_string/data
David Tolnay3cd990f2020-04-24 16:24:26 -0700122 /// [len]: #method.len
David Tolnay7db73692019-10-20 14:51:12 -0400123 pub fn as_ptr(&self) -> *const u8 {
124 unsafe { string_data(self) }
125 }
126
127 /// Validates that the C++ string contains UTF-8 data and produces a view of
128 /// it as a Rust &amp;str, otherwise an error.
129 pub fn to_str(&self) -> Result<&str, Utf8Error> {
130 str::from_utf8(self.as_bytes())
131 }
132
133 /// If the contents of the C++ string are valid UTF-8, this function returns
134 /// a view as a Cow::Borrowed &amp;str. Otherwise replaces any invalid UTF-8
135 /// sequences with the U+FFFD [replacement character] and returns a
136 /// Cow::Owned String.
137 ///
138 /// [replacement character]: https://doc.rust-lang.org/std/char/constant.REPLACEMENT_CHARACTER.html
139 pub fn to_string_lossy(&self) -> Cow<str> {
140 String::from_utf8_lossy(self.as_bytes())
141 }
David Tolnay90691f42020-11-14 20:01:46 -0800142
143 /// Appends a given string slice onto the end of this C++ string.
David Tolnaya7525d32020-11-15 19:13:26 -0800144 pub fn push_str(self: Pin<&mut Self>, s: &str) {
David Tolnay95e74b32020-11-14 20:16:22 -0800145 self.push_bytes(s.as_bytes());
146 }
147
148 /// Appends arbitrary bytes onto the end of this C++ string.
David Tolnaya7525d32020-11-15 19:13:26 -0800149 pub fn push_bytes(self: Pin<&mut Self>, bytes: &[u8]) {
David Tolnayde1335f2020-11-15 19:47:02 -0800150 unsafe { string_push(self, bytes.as_ptr(), bytes.len()) }
David Tolnay90691f42020-11-14 20:01:46 -0800151 }
David Tolnay7db73692019-10-20 14:51:12 -0400152}
153
154impl Display for CxxString {
155 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
David Tolnayd930a792020-03-25 12:24:40 -0700156 Display::fmt(self.to_string_lossy().as_ref(), f)
David Tolnay7db73692019-10-20 14:51:12 -0400157 }
158}
159
160impl Debug for CxxString {
161 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
David Tolnayd930a792020-03-25 12:24:40 -0700162 Debug::fmt(self.to_string_lossy().as_ref(), f)
David Tolnay7db73692019-10-20 14:51:12 -0400163 }
164}
David Tolnay42ebfa22020-03-25 12:26:22 -0700165
166impl PartialEq for CxxString {
167 fn eq(&self, other: &CxxString) -> bool {
168 self.as_bytes() == other.as_bytes()
169 }
170}
171
172impl PartialEq<CxxString> for str {
173 fn eq(&self, other: &CxxString) -> bool {
174 self.as_bytes() == other.as_bytes()
175 }
176}
177
178impl PartialEq<str> for CxxString {
179 fn eq(&self, other: &str) -> bool {
180 self.as_bytes() == other.as_bytes()
181 }
182}
David Tolnaybb3ff5d2020-11-15 19:45:11 -0800183
184#[doc(hidden)]
185#[repr(C)]
186pub struct StackString {
187 // Static assertions in cxx.cc validate that this is large enough and
188 // aligned enough.
189 space: MaybeUninit<[*const (); 8]>,
190}
191
192impl StackString {
193 pub fn new() -> Self {
194 StackString {
195 space: MaybeUninit::uninit(),
196 }
197 }
198
199 pub unsafe fn init(&mut self, value: impl AsRef<[u8]>) -> Pin<&mut CxxString> {
200 let value = value.as_ref();
201 let this = &mut *self.space.as_mut_ptr().cast::<MaybeUninit<CxxString>>();
202 string_init(this, value.as_ptr(), value.len());
203 Pin::new_unchecked(&mut *this.as_mut_ptr())
204 }
205}
206
207impl Drop for StackString {
208 fn drop(&mut self) {
209 unsafe {
210 let this = &mut *self.space.as_mut_ptr().cast::<MaybeUninit<CxxString>>();
211 string_destroy(this);
212 }
213 }
214}