Struct CxxString
#[repr(C)]
pub struct CxxString { /* private fields */ }
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) -> usizeReturns the length of the string in bytes.
Matches the behavior of C++ std::string::size.
fn is_empty(&self) -> boolReturns true if
selfhas a length of zero bytes.Matches the behavior of C++ std::string::empty.
fn as_bytes(&self) -> &[u8]Returns a byte slice of this string's contents.
fn as_ptr(&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().Modifying the string data through this pointer has undefined behavior.
fn as_c_str(&self) -> &CStrProduces a nul-terminated string view of this string's contents.
Matches the behavior of C++ std::string::c_str.
If this string contains no internal '\0' bytes, then
self.as_c_str().count_bytes() == self.len(). But if it does, the CStr only refers to the part of the string up to the first nul byte.fn to_str(&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) -> 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 the implementation 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.
Trait Implementations
impl Debug for CxxString
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Display for CxxString
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Eq for CxxString
impl ExternType for CxxString
type Kind = Opaque;
impl Hash for CxxString
fn hash<H: Hasher>(&self, state: &mut H)
impl Ord for CxxString
fn cmp(&self, other: &Self) -> Ordering
impl PartialEq for CxxString
fn eq(&self, other: &Self) -> bool
impl PartialEq<str> for CxxString
fn eq(&self, other: &str) -> bool
impl PartialOrd for CxxString
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
impl SharedPtrTarget for CxxString
impl UniquePtrTarget for CxxString
impl VectorElement for CxxString
impl WeakPtrTarget for CxxString
Auto Trait Implementations
impl !Unpin for CxxString
impl Freeze for CxxString
impl RefUnwindSafe for CxxString
impl Send for CxxString
impl Sync for CxxString
impl UnsafeUnpin for CxxString
impl UnwindSafe for CxxString
Blanket Implementations
impl<T> Any for CxxString
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for CxxString
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for CxxString
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for CxxString
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToString for CxxString
where
T: Display + ?Sized,
fn to_string(&self) -> String
impl<T, U> Into<U> for CxxString
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for CxxString
where
U: Into<T>,
type Error = Infallible;fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto<U> for CxxString
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>