1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::fmt;
use std::str::{self, FromStr, Utf8Error};
use std::string::FromUtf8Error;
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct ResRef(pub(crate) Vec<u8>);
impl ResRef {
#[inline]
pub fn as_str(&self) -> Result<&str, Utf8Error> {
str::from_utf8(&self.0)
}
#[inline]
pub fn as_string(self) -> Result<String, FromUtf8Error> {
String::from_utf8(self.0)
}
}
impl fmt::Debug for ResRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Ok(value) = str::from_utf8(&self.0) {
return write!(f, "{}", value);
}
self.0.fmt(f)
}
}
impl fmt::Display for ResRef {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let value = self.as_str().map_err(|_| fmt::Error)?;
write!(f, "{}", value)
}
}
impl Into<String> for ResRef {
#[inline]
fn into(self) -> String {
String::from_utf8(self.0).expect("ResRef contains non UTF-8 string")
}
}
impl<'a> Into<&'a str> for &'a ResRef {
#[inline]
fn into(self) -> &'a str {
str::from_utf8(&self.0).expect("ResRef contains non UTF-8 string")
}
}
impl<'a> From<&'a str> for ResRef {
#[inline]
fn from(str: &'a str) -> Self { ResRef(str.as_bytes().to_owned()) }
}
impl FromStr for ResRef {
type Err = ();
#[inline]
fn from_str(str: &str) -> Result<Self, Self::Err> { Ok(str.into()) }
}