add annotation support

This commit is contained in:
Dustin J. Mitchell 2022-02-11 23:54:52 +00:00
parent b01285d780
commit 7996a98908
6 changed files with 308 additions and 3 deletions

View file

@ -69,6 +69,18 @@ impl<'a> TCString<'a> {
}
}
/// Consume this TCString and return an equivalent String, or an error if not
/// valid UTF-8. In the error condition, the original data is lost.
pub(crate) fn into_string(self) -> Result<String, std::str::Utf8Error> {
match self {
TCString::CString(cstring) => cstring.into_string().map_err(|e| e.utf8_error()),
TCString::CStr(cstr) => cstr.to_str().map(|s| s.to_string()),
TCString::String(string) => Ok(string),
TCString::InvalidUtf8(e, _) => Err(e),
TCString::None => unreachable!(),
}
}
fn as_bytes(&self) -> &[u8] {
match self {
TCString::CString(cstring) => cstring.as_bytes(),