#[repr(transparent)]pub struct Str { /* private fields */ }
Expand description
Type with same representation as GVariant “s”, “o” and “g” types
This is the type returned by:
gv!("s").cast(data);
We can’t use Rust’s str
type here because GVariant strings always end with
a NUL byte.
Implementations
sourceimpl Str
impl Str
sourcepub fn as_bytes_non_conformant(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn as_bytes_non_conformant(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
Convert &Str
to &[u8]
This will give the same result as s.to_str().as_bytes()
for normal
data, but unlike using to_str()
it should be 0-cost as it doesn’t
require scanning the underlying data.
The result of this function will deviate from the GLib GVariant implementation if the data contains embedded NULs or non-utf8 data. The spec says:
2.7.3 Handling Non-Normal Serialised Data
String with Embedded Nul
If a string has a nul character as its final byte, but also contains another nul character before this final terminator, the value of the string is taken to be the part of the string that precedes the embedded nul. This means that obtaining a C pointer to a string is still a constant time operation.
Instead this function will return the data with the embedded NULs intact (excluding the final NUL byte)
sourcepub fn to_str(&self) -> &str
pub fn to_str(&self) -> &str
Convert &Str
to &str
For consistency with the GLib implementation this will return a empty string if the underlying data is not utf-8 encoded or contains embedded NULs. This differs from the wording of the GVariant specification which says that “the use of UTF-8 is expected and encouraged”, but it is not guaranteed.
This function executes in linear time with the length of the data. If
you know that your data is in normal form you can use
self.to_bytes_non_conformant()
instead which executes in constant
time.
Trait Implementations
sourceimpl Cast for Str
impl Cast for Str
sourcefn default_ref() -> &'static Self
fn default_ref() -> &'static Self
Get a static reference to the default value for this type. Read more
fn try_from_aligned_slice(
slice: &AlignedSlice<Self::AlignOf>
) -> Result<&Self, WrongSize>
fn try_from_aligned_slice_mut(
slice: &mut AlignedSlice<Self::AlignOf>
) -> Result<&mut Self, WrongSize>
sourcefn from_aligned_slice(slice: &AlignedSlice<Self::AlignOf>) -> &Self
fn from_aligned_slice(slice: &AlignedSlice<Self::AlignOf>) -> &Self
Cast slice
to type Self
. Read more