1 use crate::actually_private::Private; 2 use alloc::borrow::Cow; 3 use alloc::string::String; 4 use core::cmp::Ordering; 5 use core::fmt::{self, Debug, Display}; 6 use core::hash::{Hash, Hasher}; 7 use core::marker::{PhantomData, PhantomPinned}; 8 use core::mem::MaybeUninit; 9 use core::pin::Pin; 10 use core::slice; 11 use core::str::{self, Utf8Error}; 12 13 extern "C" { 14 #[link_name = "cxxbridge1$cxx_string$init"] string_init(this: &mut MaybeUninit<CxxString>, ptr: *const u8, len: usize)15 fn string_init(this: &mut MaybeUninit<CxxString>, ptr: *const u8, len: usize); 16 #[link_name = "cxxbridge1$cxx_string$destroy"] string_destroy(this: &mut MaybeUninit<CxxString>)17 fn string_destroy(this: &mut MaybeUninit<CxxString>); 18 #[link_name = "cxxbridge1$cxx_string$data"] string_data(this: &CxxString) -> *const u819 fn string_data(this: &CxxString) -> *const u8; 20 #[link_name = "cxxbridge1$cxx_string$length"] string_length(this: &CxxString) -> usize21 fn string_length(this: &CxxString) -> usize; 22 #[link_name = "cxxbridge1$cxx_string$clear"] string_clear(this: Pin<&mut CxxString>)23 fn string_clear(this: Pin<&mut CxxString>); 24 #[link_name = "cxxbridge1$cxx_string$reserve_total"] string_reserve_total(this: Pin<&mut CxxString>, new_cap: usize)25 fn string_reserve_total(this: Pin<&mut CxxString>, new_cap: usize); 26 #[link_name = "cxxbridge1$cxx_string$push"] string_push(this: Pin<&mut CxxString>, ptr: *const u8, len: usize)27 fn string_push(this: Pin<&mut CxxString>, ptr: *const u8, len: usize); 28 } 29 30 /// Binding to C++ `std::string`. 31 /// 32 /// # Invariants 33 /// 34 /// As an invariant of this API and the static analysis of the cxx::bridge 35 /// macro, in Rust code we can never obtain a `CxxString` by value. C++'s string 36 /// requires a move constructor and may hold internal pointers, which is not 37 /// compatible with Rust's move behavior. Instead in Rust code we will only ever 38 /// look at a CxxString through a reference or smart pointer, as in `&CxxString` 39 /// or `UniquePtr<CxxString>`. 40 #[repr(C)] 41 pub struct CxxString { 42 _private: [u8; 0], 43 _pinned: PhantomData<PhantomPinned>, 44 } 45 46 /// Construct a C++ std::string on the Rust stack. 47 /// 48 /// # Syntax 49 /// 50 /// In statement position: 51 /// 52 /// ``` 53 /// # use cxx::let_cxx_string; 54 /// # let expression = ""; 55 /// let_cxx_string!(var = expression); 56 /// ``` 57 /// 58 /// The `expression` may have any type that implements `AsRef<[u8]>`. Commonly 59 /// it will be a string literal, but for example `&[u8]` and `String` would work 60 /// as well. 61 /// 62 /// The macro expands to something resembling `let $var: Pin<&mut CxxString> = 63 /// /*???*/;`. The resulting [`Pin`] can be deref'd to `&CxxString` as needed. 64 /// 65 /// # Example 66 /// 67 /// ``` 68 /// use cxx::{let_cxx_string, CxxString}; 69 /// 70 /// fn f(s: &CxxString) {/* ... */} 71 /// 72 /// fn main() { 73 /// let_cxx_string!(s = "example"); 74 /// f(&s); 75 /// } 76 /// ``` 77 #[macro_export] 78 macro_rules! let_cxx_string { 79 ($var:ident = $value:expr $(,)?) => { 80 let mut cxx_stack_string = $crate::private::StackString::new(); 81 #[allow(unused_mut, unused_unsafe)] 82 let mut $var = match $value { 83 let_cxx_string => unsafe { cxx_stack_string.init(let_cxx_string) }, 84 }; 85 }; 86 } 87 88 impl CxxString { 89 /// `CxxString` is not constructible via `new`. Instead, use the 90 /// [`let_cxx_string!`] macro. new<T: Private>() -> Self91 pub fn new<T: Private>() -> Self { 92 unreachable!() 93 } 94 95 /// Returns the length of the string in bytes. 96 /// 97 /// Matches the behavior of C++ [std::string::size][size]. 98 /// 99 /// [size]: https://en.cppreference.com/w/cpp/string/basic_string/size len(&self) -> usize100 pub fn len(&self) -> usize { 101 unsafe { string_length(self) } 102 } 103 104 /// Returns true if `self` has a length of zero bytes. 105 /// 106 /// Matches the behavior of C++ [std::string::empty][empty]. 107 /// 108 /// [empty]: https://en.cppreference.com/w/cpp/string/basic_string/empty is_empty(&self) -> bool109 pub fn is_empty(&self) -> bool { 110 self.len() == 0 111 } 112 113 /// Returns a byte slice of this string's contents. as_bytes(&self) -> &[u8]114 pub fn as_bytes(&self) -> &[u8] { 115 let data = self.as_ptr(); 116 let len = self.len(); 117 unsafe { slice::from_raw_parts(data, len) } 118 } 119 120 /// Produces a pointer to the first character of the string. 121 /// 122 /// Matches the behavior of C++ [std::string::data][data]. 123 /// 124 /// Note that the return type may look like `const char *` but is not a 125 /// `const char *` in the typical C sense, as C++ strings may contain 126 /// internal null bytes. As such, the returned pointer only makes sense as a 127 /// string in combination with the length returned by [`len()`][len]. 128 /// 129 /// [data]: https://en.cppreference.com/w/cpp/string/basic_string/data 130 /// [len]: #method.len as_ptr(&self) -> *const u8131 pub fn as_ptr(&self) -> *const u8 { 132 unsafe { string_data(self) } 133 } 134 135 /// Validates that the C++ string contains UTF-8 data and produces a view of 136 /// it as a Rust &str, otherwise an error. to_str(&self) -> Result<&str, Utf8Error>137 pub fn to_str(&self) -> Result<&str, Utf8Error> { 138 str::from_utf8(self.as_bytes()) 139 } 140 141 /// If the contents of the C++ string are valid UTF-8, this function returns 142 /// a view as a Cow::Borrowed &str. Otherwise replaces any invalid UTF-8 143 /// sequences with the U+FFFD [replacement character] and returns a 144 /// Cow::Owned String. 145 /// 146 /// [replacement character]: https://doc.rust-lang.org/std/char/constant.REPLACEMENT_CHARACTER.html to_string_lossy(&self) -> Cow<str>147 pub fn to_string_lossy(&self) -> Cow<str> { 148 String::from_utf8_lossy(self.as_bytes()) 149 } 150 151 /// Removes all characters from the string. 152 /// 153 /// Matches the behavior of C++ [std::string::clear][clear]. 154 /// 155 /// Note: **unlike** the guarantee of Rust's `std::string::String::clear`, 156 /// the C++ standard does not require that capacity is unchanged by this 157 /// operation. In practice existing implementations do not change the 158 /// capacity but all pointers, references, and iterators into the string 159 /// contents are nevertheless invalidated. 160 /// 161 /// [clear]: https://en.cppreference.com/w/cpp/string/basic_string/clear clear(self: Pin<&mut Self>)162 pub fn clear(self: Pin<&mut Self>) { 163 unsafe { string_clear(self) } 164 } 165 166 /// Ensures that this string's capacity is at least `additional` bytes 167 /// larger than its length. 168 /// 169 /// The capacity may be increased by more than `additional` bytes if it 170 /// chooses, to amortize the cost of frequent reallocations. 171 /// 172 /// **The meaning of the argument is not the same as 173 /// [std::string::reserve][reserve] in C++.** The C++ standard library and 174 /// Rust standard library both have a `reserve` method on strings, but in 175 /// C++ code the argument always refers to total capacity, whereas in Rust 176 /// code it always refers to additional capacity. This API on `CxxString` 177 /// follows the Rust convention, the same way that for the length accessor 178 /// we use the Rust conventional `len()` naming and not C++ `size()` or 179 /// `length()`. 180 /// 181 /// # Panics 182 /// 183 /// Panics if the new capacity overflows usize. 184 /// 185 /// [reserve]: https://en.cppreference.com/w/cpp/string/basic_string/reserve reserve(self: Pin<&mut Self>, additional: usize)186 pub fn reserve(self: Pin<&mut Self>, additional: usize) { 187 let new_cap = self 188 .len() 189 .checked_add(additional) 190 .expect("CxxString capacity overflow"); 191 unsafe { string_reserve_total(self, new_cap) } 192 } 193 194 /// Appends a given string slice onto the end of this C++ string. push_str(self: Pin<&mut Self>, s: &str)195 pub fn push_str(self: Pin<&mut Self>, s: &str) { 196 self.push_bytes(s.as_bytes()); 197 } 198 199 /// Appends arbitrary bytes onto the end of this C++ string. push_bytes(self: Pin<&mut Self>, bytes: &[u8])200 pub fn push_bytes(self: Pin<&mut Self>, bytes: &[u8]) { 201 unsafe { string_push(self, bytes.as_ptr(), bytes.len()) } 202 } 203 } 204 205 impl Display for CxxString { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result206 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 207 Display::fmt(self.to_string_lossy().as_ref(), f) 208 } 209 } 210 211 impl Debug for CxxString { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result212 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 213 Debug::fmt(self.to_string_lossy().as_ref(), f) 214 } 215 } 216 217 impl PartialEq for CxxString { eq(&self, other: &Self) -> bool218 fn eq(&self, other: &Self) -> bool { 219 self.as_bytes() == other.as_bytes() 220 } 221 } 222 223 impl PartialEq<CxxString> for str { eq(&self, other: &CxxString) -> bool224 fn eq(&self, other: &CxxString) -> bool { 225 self.as_bytes() == other.as_bytes() 226 } 227 } 228 229 impl PartialEq<str> for CxxString { eq(&self, other: &str) -> bool230 fn eq(&self, other: &str) -> bool { 231 self.as_bytes() == other.as_bytes() 232 } 233 } 234 235 impl Eq for CxxString {} 236 237 impl PartialOrd for CxxString { partial_cmp(&self, other: &Self) -> Option<Ordering>238 fn partial_cmp(&self, other: &Self) -> Option<Ordering> { 239 self.as_bytes().partial_cmp(other.as_bytes()) 240 } 241 } 242 243 impl Ord for CxxString { cmp(&self, other: &Self) -> Ordering244 fn cmp(&self, other: &Self) -> Ordering { 245 self.as_bytes().cmp(other.as_bytes()) 246 } 247 } 248 249 impl Hash for CxxString { hash<H: Hasher>(&self, state: &mut H)250 fn hash<H: Hasher>(&self, state: &mut H) { 251 self.as_bytes().hash(state); 252 } 253 } 254 255 #[doc(hidden)] 256 #[repr(C)] 257 pub struct StackString { 258 // Static assertions in cxx.cc validate that this is large enough and 259 // aligned enough. 260 space: MaybeUninit<[usize; 8]>, 261 } 262 263 #[allow(missing_docs)] 264 impl StackString { new() -> Self265 pub fn new() -> Self { 266 StackString { 267 space: MaybeUninit::uninit(), 268 } 269 } 270 init(&mut self, value: impl AsRef<[u8]>) -> Pin<&mut CxxString>271 pub unsafe fn init(&mut self, value: impl AsRef<[u8]>) -> Pin<&mut CxxString> { 272 let value = value.as_ref(); 273 unsafe { 274 let this = &mut *self.space.as_mut_ptr().cast::<MaybeUninit<CxxString>>(); 275 string_init(this, value.as_ptr(), value.len()); 276 Pin::new_unchecked(&mut *this.as_mut_ptr()) 277 } 278 } 279 } 280 281 impl Drop for StackString { drop(&mut self)282 fn drop(&mut self) { 283 unsafe { 284 let this = &mut *self.space.as_mut_ptr().cast::<MaybeUninit<CxxString>>(); 285 string_destroy(this); 286 } 287 } 288 } 289