| David Tolnay | 3384c14 | 2020-09-14 00:26:47 -0400 | [diff] [blame] | 1 | use alloc::borrow::Cow; |
| David Tolnay | c5a52f9 | 2020-09-14 00:43:29 -0400 | [diff] [blame] | 2 | use alloc::string::String; |
| David Tolnay | 3384c14 | 2020-09-14 00:26:47 -0400 | [diff] [blame] | 3 | use core::fmt::{self, Debug, Display}; |
| David Tolnay | 95dab1d | 2020-11-15 14:32:37 -0800 | [diff] [blame^] | 4 | use core::marker::{PhantomData, PhantomPinned}; |
| David Tolnay | 3384c14 | 2020-09-14 00:26:47 -0400 | [diff] [blame] | 5 | use core::slice; |
| 6 | use core::str::{self, Utf8Error}; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 7 | |
| 8 | extern "C" { |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 9 | #[link_name = "cxxbridge05$cxx_string$data"] |
| David Tolnay | 90691f4 | 2020-11-14 20:01:46 -0800 | [diff] [blame] | 10 | fn string_data(this: &CxxString) -> *const u8; |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 11 | #[link_name = "cxxbridge05$cxx_string$length"] |
| David Tolnay | 90691f4 | 2020-11-14 20:01:46 -0800 | [diff] [blame] | 12 | fn string_length(this: &CxxString) -> usize; |
| 13 | #[link_name = "cxxbridge05$cxx_string$push"] |
| 14 | fn string_push(this: &mut CxxString, ptr: *const u8, len: usize); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | /// Binding to C++ `std::string`. |
| 18 | /// |
| 19 | /// # Invariants |
| 20 | /// |
| 21 | /// As an invariant of this API and the static analysis of the cxx::bridge |
| 22 | /// macro, in Rust code we can never obtain a `CxxString` by value. C++'s string |
| 23 | /// requires a move constructor and may hold internal pointers, which is not |
| 24 | /// compatible with Rust's move behavior. Instead in Rust code we will only ever |
| 25 | /// look at a CxxString through a reference or smart pointer, as in `&CxxString` |
| 26 | /// or `UniquePtr<CxxString>`. |
| 27 | #[repr(C)] |
| 28 | pub struct CxxString { |
| 29 | _private: [u8; 0], |
| David Tolnay | 95dab1d | 2020-11-15 14:32:37 -0800 | [diff] [blame^] | 30 | _pinned: PhantomData<PhantomPinned>, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | impl CxxString { |
| 34 | /// Returns the length of the string in bytes. |
| 35 | /// |
| 36 | /// Matches the behavior of C++ [std::string::size][size]. |
| 37 | /// |
| 38 | /// [size]: https://en.cppreference.com/w/cpp/string/basic_string/size |
| 39 | pub fn len(&self) -> usize { |
| 40 | unsafe { string_length(self) } |
| 41 | } |
| 42 | |
| 43 | /// Returns true if `self` has a length of zero bytes. |
| David Tolnay | d7b8a6e | 2020-04-24 16:22:55 -0700 | [diff] [blame] | 44 | /// |
| 45 | /// Matches the behavior of C++ [std::string::empty][empty]. |
| 46 | /// |
| 47 | /// [empty]: https://en.cppreference.com/w/cpp/string/basic_string/empty |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 48 | pub fn is_empty(&self) -> bool { |
| 49 | self.len() == 0 |
| 50 | } |
| 51 | |
| 52 | /// Returns a byte slice of this string's contents. |
| 53 | pub fn as_bytes(&self) -> &[u8] { |
| 54 | let data = self.as_ptr(); |
| 55 | let len = self.len(); |
| 56 | unsafe { slice::from_raw_parts(data, len) } |
| 57 | } |
| 58 | |
| 59 | /// Produces a pointer to the first character of the string. |
| 60 | /// |
| 61 | /// Matches the behavior of C++ [std::string::data][data]. |
| 62 | /// |
| 63 | /// Note that the return type may look like `const char *` but is not a |
| 64 | /// `const char *` in the typical C sense, as C++ strings may contain |
| 65 | /// 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] | 66 | /// string in combination with the length returned by [`len()`][len]. |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 67 | /// |
| 68 | /// [data]: https://en.cppreference.com/w/cpp/string/basic_string/data |
| David Tolnay | 3cd990f | 2020-04-24 16:24:26 -0700 | [diff] [blame] | 69 | /// [len]: #method.len |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 70 | pub fn as_ptr(&self) -> *const u8 { |
| 71 | unsafe { string_data(self) } |
| 72 | } |
| 73 | |
| 74 | /// Validates that the C++ string contains UTF-8 data and produces a view of |
| 75 | /// it as a Rust &str, otherwise an error. |
| 76 | pub fn to_str(&self) -> Result<&str, Utf8Error> { |
| 77 | str::from_utf8(self.as_bytes()) |
| 78 | } |
| 79 | |
| 80 | /// If the contents of the C++ string are valid UTF-8, this function returns |
| 81 | /// a view as a Cow::Borrowed &str. Otherwise replaces any invalid UTF-8 |
| 82 | /// sequences with the U+FFFD [replacement character] and returns a |
| 83 | /// Cow::Owned String. |
| 84 | /// |
| 85 | /// [replacement character]: https://doc.rust-lang.org/std/char/constant.REPLACEMENT_CHARACTER.html |
| 86 | pub fn to_string_lossy(&self) -> Cow<str> { |
| 87 | String::from_utf8_lossy(self.as_bytes()) |
| 88 | } |
| David Tolnay | 90691f4 | 2020-11-14 20:01:46 -0800 | [diff] [blame] | 89 | |
| 90 | /// Appends a given string slice onto the end of this C++ string. |
| 91 | pub fn push_str(&mut self, s: &str) { |
| David Tolnay | 95e74b3 | 2020-11-14 20:16:22 -0800 | [diff] [blame] | 92 | self.push_bytes(s.as_bytes()); |
| 93 | } |
| 94 | |
| 95 | /// Appends arbitrary bytes onto the end of this C++ string. |
| 96 | pub fn push_bytes(&mut self, bytes: &[u8]) { |
| 97 | unsafe { string_push(self, bytes.as_ptr(), bytes.len()) } |
| David Tolnay | 90691f4 | 2020-11-14 20:01:46 -0800 | [diff] [blame] | 98 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | impl Display for CxxString { |
| 102 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| David Tolnay | d930a79 | 2020-03-25 12:24:40 -0700 | [diff] [blame] | 103 | Display::fmt(self.to_string_lossy().as_ref(), f) |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | impl Debug for CxxString { |
| 108 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| David Tolnay | d930a79 | 2020-03-25 12:24:40 -0700 | [diff] [blame] | 109 | Debug::fmt(self.to_string_lossy().as_ref(), f) |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 110 | } |
| 111 | } |
| David Tolnay | 42ebfa2 | 2020-03-25 12:26:22 -0700 | [diff] [blame] | 112 | |
| 113 | impl PartialEq for CxxString { |
| 114 | fn eq(&self, other: &CxxString) -> bool { |
| 115 | self.as_bytes() == other.as_bytes() |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | impl PartialEq<CxxString> for str { |
| 120 | fn eq(&self, other: &CxxString) -> bool { |
| 121 | self.as_bytes() == other.as_bytes() |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | impl PartialEq<str> for CxxString { |
| 126 | fn eq(&self, other: &str) -> bool { |
| 127 | self.as_bytes() == other.as_bytes() |
| 128 | } |
| 129 | } |