| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame^] | 1 | use crate::actually_private::Private; |
| David Tolnay | 3384c14 | 2020-09-14 00:26:47 -0400 | [diff] [blame] | 2 | use alloc::borrow::Cow; |
| David Tolnay | c5a52f9 | 2020-09-14 00:43:29 -0400 | [diff] [blame] | 3 | use alloc::string::String; |
| David Tolnay | 3384c14 | 2020-09-14 00:26:47 -0400 | [diff] [blame] | 4 | use core::fmt::{self, Debug, Display}; |
| David Tolnay | 95dab1d | 2020-11-15 14:32:37 -0800 | [diff] [blame] | 5 | use core::marker::{PhantomData, PhantomPinned}; |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame^] | 6 | use core::mem::MaybeUninit; |
| David Tolnay | a7525d3 | 2020-11-15 19:13:26 -0800 | [diff] [blame] | 7 | use core::pin::Pin; |
| David Tolnay | 3384c14 | 2020-09-14 00:26:47 -0400 | [diff] [blame] | 8 | use core::slice; |
| 9 | use core::str::{self, Utf8Error}; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 10 | |
| 11 | extern "C" { |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame^] | 12 | #[link_name = "cxxbridge05$cxx_string$init"] |
| 13 | fn string_init(this: &mut MaybeUninit<CxxString>, ptr: *const u8, len: usize); |
| 14 | #[link_name = "cxxbridge05$cxx_string$destroy"] |
| 15 | fn string_destroy(this: &mut MaybeUninit<CxxString>); |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 16 | #[link_name = "cxxbridge05$cxx_string$data"] |
| David Tolnay | 90691f4 | 2020-11-14 20:01:46 -0800 | [diff] [blame] | 17 | fn string_data(this: &CxxString) -> *const u8; |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 18 | #[link_name = "cxxbridge05$cxx_string$length"] |
| David Tolnay | 90691f4 | 2020-11-14 20:01:46 -0800 | [diff] [blame] | 19 | fn string_length(this: &CxxString) -> usize; |
| 20 | #[link_name = "cxxbridge05$cxx_string$push"] |
| David Tolnay | de1335f | 2020-11-15 19:47:02 -0800 | [diff] [blame] | 21 | fn string_push(this: Pin<&mut CxxString>, ptr: *const u8, len: usize); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 22 | } |
| 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)] |
| 35 | pub struct CxxString { |
| 36 | _private: [u8; 0], |
| David Tolnay | 95dab1d | 2020-11-15 14:32:37 -0800 | [diff] [blame] | 37 | _pinned: PhantomData<PhantomPinned>, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 38 | } |
| 39 | |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame^] | 40 | /// 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] |
| 72 | macro_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 Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 80 | impl CxxString { |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame^] | 81 | /// `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 Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 87 | /// 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 Tolnay | d7b8a6e | 2020-04-24 16:22:55 -0700 | [diff] [blame] | 97 | /// |
| 98 | /// Matches the behavior of C++ [std::string::empty][empty]. |
| 99 | /// |
| 100 | /// [empty]: https://en.cppreference.com/w/cpp/string/basic_string/empty |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 101 | 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 Tolnay | 3cd990f | 2020-04-24 16:24:26 -0700 | [diff] [blame] | 119 | /// string in combination with the length returned by [`len()`][len]. |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 120 | /// |
| 121 | /// [data]: https://en.cppreference.com/w/cpp/string/basic_string/data |
| David Tolnay | 3cd990f | 2020-04-24 16:24:26 -0700 | [diff] [blame] | 122 | /// [len]: #method.len |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 123 | 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 &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 &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 Tolnay | 90691f4 | 2020-11-14 20:01:46 -0800 | [diff] [blame] | 142 | |
| 143 | /// Appends a given string slice onto the end of this C++ string. |
| David Tolnay | a7525d3 | 2020-11-15 19:13:26 -0800 | [diff] [blame] | 144 | pub fn push_str(self: Pin<&mut Self>, s: &str) { |
| David Tolnay | 95e74b3 | 2020-11-14 20:16:22 -0800 | [diff] [blame] | 145 | self.push_bytes(s.as_bytes()); |
| 146 | } |
| 147 | |
| 148 | /// Appends arbitrary bytes onto the end of this C++ string. |
| David Tolnay | a7525d3 | 2020-11-15 19:13:26 -0800 | [diff] [blame] | 149 | pub fn push_bytes(self: Pin<&mut Self>, bytes: &[u8]) { |
| David Tolnay | de1335f | 2020-11-15 19:47:02 -0800 | [diff] [blame] | 150 | unsafe { string_push(self, bytes.as_ptr(), bytes.len()) } |
| David Tolnay | 90691f4 | 2020-11-14 20:01:46 -0800 | [diff] [blame] | 151 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | impl Display for CxxString { |
| 155 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| David Tolnay | d930a79 | 2020-03-25 12:24:40 -0700 | [diff] [blame] | 156 | Display::fmt(self.to_string_lossy().as_ref(), f) |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | impl Debug for CxxString { |
| 161 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| David Tolnay | d930a79 | 2020-03-25 12:24:40 -0700 | [diff] [blame] | 162 | Debug::fmt(self.to_string_lossy().as_ref(), f) |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 163 | } |
| 164 | } |
| David Tolnay | 42ebfa2 | 2020-03-25 12:26:22 -0700 | [diff] [blame] | 165 | |
| 166 | impl PartialEq for CxxString { |
| 167 | fn eq(&self, other: &CxxString) -> bool { |
| 168 | self.as_bytes() == other.as_bytes() |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | impl PartialEq<CxxString> for str { |
| 173 | fn eq(&self, other: &CxxString) -> bool { |
| 174 | self.as_bytes() == other.as_bytes() |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | impl PartialEq<str> for CxxString { |
| 179 | fn eq(&self, other: &str) -> bool { |
| 180 | self.as_bytes() == other.as_bytes() |
| 181 | } |
| 182 | } |
| David Tolnay | bb3ff5d | 2020-11-15 19:45:11 -0800 | [diff] [blame^] | 183 | |
| 184 | #[doc(hidden)] |
| 185 | #[repr(C)] |
| 186 | pub 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 | |
| 192 | impl 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 | |
| 207 | impl 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 | } |