Skip to content

Commit

Permalink
Swap CxxString::as_c_str and CxxString::to_str in documentation
Browse files Browse the repository at this point in the history
This order makes more sense to me: as_bytes, as_ptr, as_c_str, to_str, to_str_lossy
  • Loading branch information
dtolnay committed Feb 5, 2025
1 parent 1d208e2 commit 94be9e5
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/cxx_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,18 @@ impl CxxString {
unsafe { string_data(self) }
}

/// Validates that the C++ string contains UTF-8 data and produces a view of
/// it as a Rust &str, otherwise an error.
pub fn to_str(&self) -> Result<&str, Utf8Error> {
str::from_utf8(self.as_bytes())
}

/// Produces a `&CStr` view of the string without additional allocations.
pub fn as_c_str(&self) -> &CStr {
// Since C++11, string[string.size()] is guaranteed to be \0.
unsafe { CStr::from_ptr(self.as_ptr().cast::<c_char>()) }
}

/// Validates that the C++ string contains UTF-8 data and produces a view of
/// it as a Rust &amp;str, otherwise an error.
pub fn to_str(&self) -> Result<&str, Utf8Error> {
str::from_utf8(self.as_bytes())
}

/// If the contents of the C++ string are valid UTF-8, this function returns
/// a view as a Cow::Borrowed &amp;str. Otherwise replaces any invalid UTF-8
/// sequences with the U+FFFD [replacement character] and returns a
Expand Down

0 comments on commit 94be9e5

Please sign in to comment.