#[repr(C)]pub struct CxxString { /* private fields */ }
Expand description
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§
Source§impl CxxString
impl CxxString
Sourcepub fn new<T>() -> CxxStringwhere
T: Private,
pub fn new<T>() -> CxxStringwhere
T: Private,
CxxString
is not constructible via new
. Instead, use the
let_cxx_string!
macro.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the string in bytes.
Matches the behavior of C++ std::string::size.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if self
has a length of zero bytes.
Matches the behavior of C++ std::string::empty.
Sourcepub fn as_ptr(&self) -> *const u8
pub fn as_ptr(&self) -> *const u8
Produces 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 a
const 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 by len()
.
Sourcepub fn to_str(&self) -> Result<&str, Utf8Error>
pub 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.
Sourcepub fn to_string_lossy(&self) -> Cow<'_, str>
pub 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.
Sourcepub fn clear(self: Pin<&mut CxxString>)
pub fn clear(self: Pin<&mut CxxString>)
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.
Sourcepub fn reserve(self: Pin<&mut CxxString>, additional: usize)
pub fn reserve(self: Pin<&mut CxxString>, additional: usize)
Ensures that this string’s capacity is at least additional
bytes
larger than its length.
The capacity may be increased by more than additional
bytes 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 reserve
method 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 on CxxString
follows the Rust convention, the same way that for the length accessor
we use the Rust conventional len()
naming and not C++ size()
or
length()
.
§Panics
Panics if the new capacity overflows usize.
Sourcepub fn push_str(self: Pin<&mut CxxString>, s: &str)
pub fn push_str(self: Pin<&mut CxxString>, s: &str)
Appends a given string slice onto the end of this C++ string.
Sourcepub fn push_bytes(self: Pin<&mut CxxString>, bytes: &[u8])
pub fn push_bytes(self: Pin<&mut CxxString>, bytes: &[u8])
Appends arbitrary bytes onto the end of this C++ string.
Trait Implementations§
Source§impl ExternType for CxxString
impl ExternType for CxxString
Source§impl Ord for CxxString
impl Ord for CxxString
Source§impl PartialOrd for CxxString
impl PartialOrd for CxxString
Source§impl Write for Pin<&mut CxxString>
impl Write for Pin<&mut CxxString>
Source§impl Write for Pin<&mut CxxString>
impl Write for Pin<&mut CxxString>
Source§fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
Source§fn flush(&mut self) -> Result<(), Error>
fn flush(&mut self) -> Result<(), Error>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)impl Eq for CxxString
impl UniquePtrTarget for CxxString
impl VectorElement for CxxString
impl WeakPtrTarget for CxxString
Auto Trait Implementations§
impl Freeze for CxxString
impl RefUnwindSafe for CxxString
impl Send for CxxString
impl Sync for CxxString
impl !Unpin for CxxString
impl UnwindSafe for CxxString
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more