pub struct StrValue<'d> { /* private fields */ }
Expand description
Represents a string value.
The string might or might not be zero terminated, and might or might not be valid UTF-8. The accessor functions will check these properties if needed.
Implementations
sourceimpl<'d> StrValue<'d>
impl<'d> StrValue<'d>
sourcepub fn from_bytes_without_nul(bytes: &'d [u8]) -> StrValue<'d>
pub fn from_bytes_without_nul(bytes: &'d [u8]) -> StrValue<'d>
Create a StrValue referring to raw bytes, which do not include a nul-terminator.
sourcepub fn from_bytes_with_nul(bytes: &'d [u8]) -> StrValue<'d>
pub fn from_bytes_with_nul(bytes: &'d [u8]) -> StrValue<'d>
Create a StrValue referring to raw bytes, which include a nul-terminator.
Panics if the last byte is not a nul-terminator.
sourcepub fn from_str(s: &'d str) -> StrValue<'d>
pub fn from_str(s: &'d str) -> StrValue<'d>
Create a StrValue referring to a non-zero terminated UTF-8 str
.
sourcepub fn from_cstr(s: &'d CStr) -> StrValue<'d>
pub fn from_cstr(s: &'d CStr) -> StrValue<'d>
Create a StrValue referring to a zero-terminated C string.
sourcepub fn as_bytes(&self) -> &'d [u8] ⓘ
pub fn as_bytes(&self) -> &'d [u8] ⓘ
Get the raw bytes of the string, without zero terminmator.
Always works, regardless of what type of string the value refers to.
sourcepub fn as_str(&self) -> Result<&'d str, Utf8Error>
pub fn as_str(&self) -> Result<&'d str, Utf8Error>
Get the value as &str
.
Unless the StrValue refers to a &str
, it is checked for valid UTF-8.
sourcepub fn as_cstr(&self) -> Result<&'d CStr, FromBytesWithNulError>
pub fn as_cstr(&self) -> Result<&'d CStr, FromBytesWithNulError>
Get the value as (nul-terminated) C string.
This only works when the StrValue:
- refers to a
&Cstr
, - refers to raw bytes including a nul terminator, but not containing any other nul bytes (this is checked), or
- refers to an empty string.
Otherwise, it fails.
Note that this should always work for values resulting from properly encoded argdata, as long as the value doesn’t contain any embedded nul bytes.