blob: 7ee89874e2c428800f681c900fb455080f5a3e48 [file] [log] [blame]
David Tolnay3384c142020-09-14 00:26:47 -04001use alloc::borrow::Cow;
David Tolnayc5a52f92020-09-14 00:43:29 -04002use alloc::string::String;
David Tolnay3384c142020-09-14 00:26:47 -04003use core::fmt::{self, Debug, Display};
David Tolnay95dab1d2020-11-15 14:32:37 -08004use core::marker::{PhantomData, PhantomPinned};
David Tolnay3384c142020-09-14 00:26:47 -04005use core::slice;
6use core::str::{self, Utf8Error};
David Tolnay7db73692019-10-20 14:51:12 -04007
8extern "C" {
David Tolnay8f16ae72020-10-08 18:21:13 -07009 #[link_name = "cxxbridge05$cxx_string$data"]
David Tolnay90691f42020-11-14 20:01:46 -080010 fn string_data(this: &CxxString) -> *const u8;
David Tolnay8f16ae72020-10-08 18:21:13 -070011 #[link_name = "cxxbridge05$cxx_string$length"]
David Tolnay90691f42020-11-14 20:01:46 -080012 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 Tolnay7db73692019-10-20 14:51:12 -040015}
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)]
28pub struct CxxString {
29 _private: [u8; 0],
David Tolnay95dab1d2020-11-15 14:32:37 -080030 _pinned: PhantomData<PhantomPinned>,
David Tolnay7db73692019-10-20 14:51:12 -040031}
32
33impl 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 Tolnayd7b8a6e2020-04-24 16:22:55 -070044 ///
45 /// Matches the behavior of C++ [std::string::empty][empty].
46 ///
47 /// [empty]: https://en.cppreference.com/w/cpp/string/basic_string/empty
David Tolnay7db73692019-10-20 14:51:12 -040048 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 Tolnay3cd990f2020-04-24 16:24:26 -070066 /// string in combination with the length returned by [`len()`][len].
David Tolnay7db73692019-10-20 14:51:12 -040067 ///
68 /// [data]: https://en.cppreference.com/w/cpp/string/basic_string/data
David Tolnay3cd990f2020-04-24 16:24:26 -070069 /// [len]: #method.len
David Tolnay7db73692019-10-20 14:51:12 -040070 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 &amp;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 &amp;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 Tolnay90691f42020-11-14 20:01:46 -080089
90 /// Appends a given string slice onto the end of this C++ string.
91 pub fn push_str(&mut self, s: &str) {
David Tolnay95e74b32020-11-14 20:16:22 -080092 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 Tolnay90691f42020-11-14 20:01:46 -080098 }
David Tolnay7db73692019-10-20 14:51:12 -040099}
100
101impl Display for CxxString {
102 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
David Tolnayd930a792020-03-25 12:24:40 -0700103 Display::fmt(self.to_string_lossy().as_ref(), f)
David Tolnay7db73692019-10-20 14:51:12 -0400104 }
105}
106
107impl Debug for CxxString {
108 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
David Tolnayd930a792020-03-25 12:24:40 -0700109 Debug::fmt(self.to_string_lossy().as_ref(), f)
David Tolnay7db73692019-10-20 14:51:12 -0400110 }
111}
David Tolnay42ebfa22020-03-25 12:26:22 -0700112
113impl PartialEq for CxxString {
114 fn eq(&self, other: &CxxString) -> bool {
115 self.as_bytes() == other.as_bytes()
116 }
117}
118
119impl PartialEq<CxxString> for str {
120 fn eq(&self, other: &CxxString) -> bool {
121 self.as_bytes() == other.as_bytes()
122 }
123}
124
125impl PartialEq<str> for CxxString {
126 fn eq(&self, other: &str) -> bool {
127 self.as_bytes() == other.as_bytes()
128 }
129}