Struct cxx::CxxString

source ·
#[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

source

pub fn new<T: Private>() -> Self

CxxString is not constructible via new. Instead, use the let_cxx_string! macro.

source

pub fn len(&self) -> usize

Returns the length of the string in bytes.

Matches the behavior of C++ std::string::size.

source

pub fn is_empty(&self) -> bool

Returns true if self has a length of zero bytes.

Matches the behavior of C++ std::string::empty.

source

pub fn as_bytes(&self) -> &[u8]

Returns a byte slice of this string’s contents.

source

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().

source

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.

source

pub fn to_string_lossy(&self) -> Cow<'_, str>

Available on crate feature alloc only.

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.

source

pub 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.

source

pub fn reserve(self: Pin<&mut Self>, 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.

source

pub fn push_str(self: Pin<&mut Self>, s: &str)

Appends a given string slice onto the end of this C++ string.

source

pub fn push_bytes(self: Pin<&mut Self>, bytes: &[u8])

Appends arbitrary bytes onto the end of this C++ string.

Trait Implementations§

source§

impl Debug for CxxString

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for CxxString

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl ExternType for CxxString

§

type Kind = Opaque

§

type Id

A type-level representation of the type’s C++ namespace and type name. Read more
source§

impl Hash for CxxString

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for CxxString

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<CxxString> for str

source§

fn eq(&self, other: &CxxString) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<str> for CxxString

source§

fn eq(&self, other: &str) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq for CxxString

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for CxxString

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Write for Pin<&mut CxxString>

source§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Write a buffer into this writer, returning how many bytes were written. Read more
source§

fn flush(&mut self) -> Result<()>

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · source§

fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more
source§

impl Write for Pin<&mut CxxString>

source§

fn write_str(&mut self, s: &str) -> Result

Writes a string slice into this writer, returning whether the write succeeded. Read more
1.1.0 · source§

fn write_char(&mut self, c: char) -> Result<(), Error>

Writes a char into this writer, returning whether the write succeeded. Read more
1.0.0 · source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Glue for usage of the write! macro with implementors of this trait. Read more
source§

impl Eq for CxxString

source§

impl SharedPtrTarget for CxxString

source§

impl UniquePtrTarget for CxxString

source§

impl VectorElement for CxxString

source§

impl WeakPtrTarget for CxxString

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.