pub trait PyStringMethods<'py>: Sealed {
// Required methods
fn to_cow(&self) -> PyResult<Cow<'_, str>>;
fn to_string_lossy(&self) -> Cow<'_, str>;
fn encode_utf8(&self) -> PyResult<Bound<'py, PyBytes>>;
}
Expand description
Implementation of functionality for PyString
.
These methods are defined for the Bound<'py, PyString>
smart pointer, so to use method call
syntax these methods are separated into a trait, because stable Rust does not yet support
arbitrary_self_types
.
Required Methods§
Sourcefn to_cow(&self) -> PyResult<Cow<'_, str>>
fn to_cow(&self) -> PyResult<Cow<'_, str>>
Converts the PyString
into a Rust string, avoiding copying when possible.
Returns a UnicodeEncodeError
if the input is not valid unicode
(containing unpaired surrogates).
Sourcefn to_string_lossy(&self) -> Cow<'_, str>
fn to_string_lossy(&self) -> Cow<'_, str>
Converts the PyString
into a Rust string.
Unpaired surrogates invalid UTF-8 sequences are
replaced with U+FFFD REPLACEMENT CHARACTER
.
Sourcefn encode_utf8(&self) -> PyResult<Bound<'py, PyBytes>>
fn encode_utf8(&self) -> PyResult<Bound<'py, PyBytes>>
Encodes this string as a Python bytes
object, using UTF-8 encoding.