pub struct Lat1Str { /* private fields */ }
Expand description
A slice to a Latin-1 (aka. ISO 8859-1) string.
It is usually seen in its borrowed form, &Lat1Str
.
Lat1Str contains a slice of bytes and is by definition always
valid Latin-1.
This type is useful for XCB because strings in the X protocol are expected to be Latin-1 encoded. Although the X strings are Latin-1, in reality ASCII can be expected without too much risk, hence all the ASCII related functions.
This does not account for strings passed as raw bytes
to x::ChangeProperty (e.g. to set a window title).
These strings are passed as-is by the X server to the window compositor and
encoding is implied by the property itself
(e.g. UTF-8 for _NET_WM_NAME
aka. window title).
Implementations§
source§impl Lat1Str
impl Lat1Str
sourcepub fn from_bytes(bytes: &[u8]) -> &Self
pub fn from_bytes(bytes: &[u8]) -> &Self
Returns a reference to a Lat1Str that borrows the passed bytes
sourcepub fn try_from_ascii(str: &str) -> Result<&Self, Lat1Error>
pub fn try_from_ascii(str: &str) -> Result<&Self, Lat1Error>
Returns a reference to a Lat1Str
that borrows the passed string bytes
only if str
is pure ASCII.
Otherwise, a Lat1Error::NonAscii
is returned.
sourcepub fn from_ascii(str: &str) -> &Self
pub fn from_ascii(str: &str) -> &Self
Returns a reference to a Lat1Str
that borrows the passed string bytes
only if str
is pure ASCII.
§Panics
This function panics if str
contains non-ASCII chars.
sourcepub unsafe fn from_ascii_unchecked(str: &str) -> &Self
pub unsafe fn from_ascii_unchecked(str: &str) -> &Self
Returns a reference to a Lat1Str
that borrows the passed string bytes.
§Safety
If str
contains non-ASCII characters, the returned string will not correspond
to the passed string (the latin-1 will contain utf-8 encoding).
sourcepub fn from_utf8(str: &str) -> Cow<'_, Lat1Str>
pub fn from_utf8(str: &str) -> Cow<'_, Lat1Str>
Returns a Latin-1 string built from a UTF-8 string
Cow::Borrowed
is returned if str
contains only ASCII,
otherwise, a conversion from UTF-8 is performed and Cow::Owned
is returned.
sourcepub fn try_as_ascii(&self) -> Result<&str, Lat1Error>
pub fn try_as_ascii(&self) -> Result<&str, Lat1Error>
Returns the string in UTF-8 encoding, only if the string is pure ASCII.
Otherwise, a Lat1Error::NonAscii
is returned.
sourcepub fn as_ascii(&self) -> &str
pub fn as_ascii(&self) -> &str
Returns the string in UTF-8 encoding, only if the string is pure ASCII.
§Panics
This function panics if the string contains non-ASCII chars.
sourcepub unsafe fn as_ascii_unchecked(&self) -> &str
pub unsafe fn as_ascii_unchecked(&self) -> &str
Returns the string in UTF-8 encoding.
§Safety
If the string contains non-ASCII characters, the returned string will be invalid UTF-8.