Struct CxxString
struct CxxString { ... }
Binding to C++ std::string.
Invariants
As an invariant of this API and the static analysis of the cxx::bridge
macro, in Rust code we can never obtain a CxxString by value. C++'s string
requires a move constructor and may hold internal pointers, which is not
compatible with Rust's move behavior. Instead in Rust code we will only ever
look at a CxxString through a reference or smart pointer, as in &CxxString
or UniquePtr<CxxString>.
Implementations
impl CxxString
fn new<T: Private>() -> SelfCxxStringis not constructible vianew. Instead, use the [let_cxx_string!] macro.fn len(self: &Self) -> usizeReturns the length of the string in bytes.
Matches the behavior of C++ std::string::size.
fn is_empty(self: &Self) -> boolReturns true if
selfhas a length of zero bytes.Matches the behavior of C++ std::string::empty.
fn as_bytes(self: &Self) -> &[u8]Returns a byte slice of this string's contents.
fn as_ptr(self: &Self) -> *const u8Produces a pointer to the first character of the string.
Matches the behavior of C++ std::string::data.
Note that the return type may look like
const char *but is not aconst char *in the typical C sense, as C++ strings may contain internal null bytes. As such, the returned pointer only makes sense as a string in combination with the length returned bylen().fn to_str(self: &Self) -> Result<&str, Utf8Error>Validates that the C++ string contains UTF-8 data and produces a view of it as a Rust &str, otherwise an error.
fn to_string_lossy(self: &Self) -> Cow<'_, str>If the contents of the C++ string are valid UTF-8, this function returns a view as a Cow::Borrowed &str. Otherwise replaces any invalid UTF-8 sequences with the U+FFFD replacement character and returns a Cow::Owned String.
fn clear(self: Pin<&mut Self>)Removes all characters from the string.
Matches the behavior of C++ std::string::clear.
Note: unlike the guarantee of Rust's
std::string::String::clear, the C++ standard does not require that capacity is unchanged by this operation. In practice existing implementations do not change the capacity but all pointers, references, and iterators into the string contents are nevertheless invalidated.fn reserve(self: Pin<&mut Self>, additional: usize)Ensures that this string's capacity is at least
additionalbytes larger than its length.The capacity may be increased by more than
additionalbytes if it chooses, to amortize the cost of frequent reallocations.The meaning of the argument is not the same as std::string::reserve in C++. The C++ standard library and Rust standard library both have a
reservemethod on strings, but in C++ code the argument always refers to total capacity, whereas in Rust code it always refers to additional capacity. This API onCxxStringfollows the Rust convention, the same way that for the length accessor we use the Rust conventionallen()naming and not C++size()orlength().Panics
Panics if the new capacity overflows usize.
fn push_str(self: Pin<&mut Self>, s: &str)Appends a given string slice onto the end of this C++ string.
fn push_bytes(self: Pin<&mut Self>, bytes: &[u8])Appends arbitrary bytes onto the end of this C++ string.
impl Debug for CxxString
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl Display for CxxString
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl Eq for CxxString
impl ExternType for CxxString
impl Freeze for CxxString
impl Hash for CxxString
fn hash<H: Hasher>(self: &Self, state: &mut H)
impl Ord for CxxString
fn cmp(self: &Self, other: &Self) -> Ordering
impl PartialEq for CxxString
fn eq(self: &Self, other: &Self) -> bool
impl PartialEq for CxxString
fn eq(self: &Self, other: &str) -> bool
impl PartialOrd for CxxString
fn partial_cmp(self: &Self, other: &Self) -> Option<Ordering>
impl RefUnwindSafe for CxxString
impl Send for CxxString
impl SharedPtrTarget for CxxString
impl Sync for CxxString
impl UniquePtrTarget for CxxString
impl Unpin for CxxString
impl UnsafeUnpin for CxxString
impl UnwindSafe for CxxString
impl VectorElement for CxxString
impl WeakPtrTarget for CxxString
impl<T> Any for CxxString
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for CxxString
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for CxxString
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> From for CxxString
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToString for CxxString
fn to_string(self: &Self) -> String
impl<T, U> Into for CxxString
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for CxxString
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for CxxString
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>