use core::{fmt, mem};
#[derive(Eq, PartialEq, Hash)]
#[repr(transparent)]
pub struct Arg([u8]);
impl Arg {
pub const fn new(bytes: &[u8]) -> &Self {
unsafe { mem::transmute(bytes) }
}
pub const fn bytes(&self) -> &[u8] {
&self.0
}
pub fn as_str(&self) -> &str {
let questions: &str = "????????????????";
let bytes = self.bytes();
match str::from_utf8(bytes) {
Ok(s) => s,
Err(_) => match questions.get(..bytes.len()) {
Some(questions) => questions,
None => questions,
},
}
}
}
impl PartialEq<[u8]> for Arg {
fn eq(&self, other: &[u8]) -> bool {
self.0 == *other
}
}
impl PartialEq<&[u8]> for Arg {
fn eq(&self, other: &&[u8]) -> bool {
self.0 == **other
}
}
impl PartialEq<str> for Arg {
fn eq(&self, other: &str) -> bool {
self.0 == *other.as_bytes()
}
}
impl fmt::Debug for Arg {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match str::from_utf8(self.bytes()) {
Ok(s) => write!(f, "{:?}", s),
Err(_) => write!(f, "{:x?}", &self.0),
}
}
}