blob: 10abbcef9285be49904fac78c222ce77c1917942 [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)]
David Tolnayc4a3ede2020-12-27 21:45:33 -080076 let mut $var = match $value {
77 let_cxx_string => unsafe { $var.init(let_cxx_string) },
78 };
David Tolnaybb3ff5d2020-11-15 19:45:11 -080079 };
80}
81
David Tolnay7db73692019-10-20 14:51:12 -040082impl CxxString {
David Tolnaybb3ff5d2020-11-15 19:45:11 -080083 /// `CxxString` is not constructible via `new`. Instead, use the
84 /// [`let_cxx_string!`] macro.
85 pub fn new<T: Private>() -> Self {
86 unreachable!()
87 }
88
David Tolnay7db73692019-10-20 14:51:12 -040089 /// Returns the length of the string in bytes.
90 ///
91 /// Matches the behavior of C++ [std::string::size][size].
92 ///
93 /// [size]: https://en.cppreference.com/w/cpp/string/basic_string/size
94 pub fn len(&self) -> usize {
95 unsafe { string_length(self) }
96 }
97
98 /// Returns true if `self` has a length of zero bytes.
David Tolnayd7b8a6e2020-04-24 16:22:55 -070099 ///
100 /// Matches the behavior of C++ [std::string::empty][empty].
101 ///
102 /// [empty]: https://en.cppreference.com/w/cpp/string/basic_string/empty
David Tolnay7db73692019-10-20 14:51:12 -0400103 pub fn is_empty(&self) -> bool {
104 self.len() == 0
105 }
106
107 /// Returns a byte slice of this string's contents.
108 pub fn as_bytes(&self) -> &[u8] {
109 let data = self.as_ptr();
110 let len = self.len();
111 unsafe { slice::from_raw_parts(data, len) }
112 }
113
114 /// Produces a pointer to the first character of the string.
115 ///
116 /// Matches the behavior of C++ [std::string::data][data].
117 ///
118 /// Note that the return type may look like `const char *` but is not a
119 /// `const char *` in the typical C sense, as C++ strings may contain
120 /// internal null bytes. As such, the returned pointer only makes sense as a
David Tolnay3cd990f2020-04-24 16:24:26 -0700121 /// string in combination with the length returned by [`len()`][len].
David Tolnay7db73692019-10-20 14:51:12 -0400122 ///
123 /// [data]: https://en.cppreference.com/w/cpp/string/basic_string/data
David Tolnay3cd990f2020-04-24 16:24:26 -0700124 /// [len]: #method.len
David Tolnay7db73692019-10-20 14:51:12 -0400125 pub fn as_ptr(&self) -> *const u8 {
126 unsafe { string_data(self) }
127 }
128
129 /// Validates that the C++ string contains UTF-8 data and produces a view of
130 /// it as a Rust &amp;str, otherwise an error.
131 pub fn to_str(&self) -> Result<&str, Utf8Error> {
132 str::from_utf8(self.as_bytes())
133 }
134
135 /// If the contents of the C++ string are valid UTF-8, this function returns
136 /// a view as a Cow::Borrowed &amp;str. Otherwise replaces any invalid UTF-8
137 /// sequences with the U+FFFD [replacement character] and returns a
138 /// Cow::Owned String.
139 ///
140 /// [replacement character]: https://doc.rust-lang.org/std/char/constant.REPLACEMENT_CHARACTER.html
141 pub fn to_string_lossy(&self) -> Cow<str> {
142 String::from_utf8_lossy(self.as_bytes())
143 }
David Tolnay90691f42020-11-14 20:01:46 -0800144
145 /// Appends a given string slice onto the end of this C++ string.
David Tolnaya7525d32020-11-15 19:13:26 -0800146 pub fn push_str(self: Pin<&mut Self>, s: &str) {
David Tolnay95e74b32020-11-14 20:16:22 -0800147 self.push_bytes(s.as_bytes());
148 }
149
150 /// Appends arbitrary bytes onto the end of this C++ string.
David Tolnaya7525d32020-11-15 19:13:26 -0800151 pub fn push_bytes(self: Pin<&mut Self>, bytes: &[u8]) {
David Tolnayde1335f2020-11-15 19:47:02 -0800152 unsafe { string_push(self, bytes.as_ptr(), bytes.len()) }
David Tolnay90691f42020-11-14 20:01:46 -0800153 }
David Tolnay7db73692019-10-20 14:51:12 -0400154}
155
156impl Display for CxxString {
157 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
David Tolnayd930a792020-03-25 12:24:40 -0700158 Display::fmt(self.to_string_lossy().as_ref(), f)
David Tolnay7db73692019-10-20 14:51:12 -0400159 }
160}
161
162impl Debug for CxxString {
163 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
David Tolnayd930a792020-03-25 12:24:40 -0700164 Debug::fmt(self.to_string_lossy().as_ref(), f)
David Tolnay7db73692019-10-20 14:51:12 -0400165 }
166}
David Tolnay42ebfa22020-03-25 12:26:22 -0700167
168impl PartialEq for CxxString {
169 fn eq(&self, other: &CxxString) -> bool {
170 self.as_bytes() == other.as_bytes()
171 }
172}
173
174impl PartialEq<CxxString> for str {
175 fn eq(&self, other: &CxxString) -> bool {
176 self.as_bytes() == other.as_bytes()
177 }
178}
179
180impl PartialEq<str> for CxxString {
181 fn eq(&self, other: &str) -> bool {
182 self.as_bytes() == other.as_bytes()
183 }
184}
David Tolnaybb3ff5d2020-11-15 19:45:11 -0800185
186#[doc(hidden)]
187#[repr(C)]
188pub struct StackString {
189 // Static assertions in cxx.cc validate that this is large enough and
190 // aligned enough.
191 space: MaybeUninit<[*const (); 8]>,
192}
193
194impl StackString {
195 pub fn new() -> Self {
196 StackString {
197 space: MaybeUninit::uninit(),
198 }
199 }
200
201 pub unsafe fn init(&mut self, value: impl AsRef<[u8]>) -> Pin<&mut CxxString> {
202 let value = value.as_ref();
203 let this = &mut *self.space.as_mut_ptr().cast::<MaybeUninit<CxxString>>();
204 string_init(this, value.as_ptr(), value.len());
205 Pin::new_unchecked(&mut *this.as_mut_ptr())
206 }
207}
208
209impl Drop for StackString {
210 fn drop(&mut self) {
211 unsafe {
212 let this = &mut *self.space.as_mut_ptr().cast::<MaybeUninit<CxxString>>();
213 string_destroy(this);
214 }
215 }
216}