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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use serde::Deserialize;
use serde::Serialize;
use std::ops::Deref;
use std::ops::DerefMut;
use std::str::from_utf8;
#[derive(PartialOrd, PartialEq, Eq, Clone)]
pub struct ByteString {
pub bytes: Vec<u8>,
pub length: usize,
}
impl ByteString {
pub fn new(bytes: Vec<u8>) -> Self {
let length = bytes.len();
ByteString { bytes, length }
}
}
impl std::fmt::Display for ByteString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for &b in &self.bytes {
match b {
0 => write!(f, "\\0")?,
b'\n' | b'\r' | b'\t' => write!(f, "{}", b.escape_ascii())?,
0x01..=0x19 | 0x7f..=0xff => write!(f, "\\x{:02x}", b)?,
_ => write!(f, "{}", b as char)?,
}
}
Ok(())
}
}
impl std::str::FromStr for ByteString {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(ByteString::new(s.as_bytes().to_vec()))
}
}
impl std::fmt::Debug for ByteString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\"")?;
for &b in &self.bytes {
match b {
0 => write!(f, "\\0")?,
b'\n' | b'\r' | b'\t' => write!(f, "{}", b.escape_ascii())?,
0x01..=0x19 | 0x7f..=0xff => write!(f, "\\x{:02x}", b)?,
_ => write!(f, "{}", b as char)?,
}
}
write!(f, "\"")?;
Ok(())
}
}
impl Serialize for ByteString {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.to_string())
}
}
impl<'de> Deserialize<'de> for ByteString {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Ok(ByteString::new(s.into_bytes()))
}
}
impl<const N: usize> PartialEq<&[u8; N]> for ByteString {
fn eq(&self, other: &&[u8; N]) -> bool {
&self.bytes == other
}
}
impl<const N: usize> PartialEq<&[u8; N]> for &ByteString {
fn eq(&self, other: &&[u8; N]) -> bool {
&self.bytes == other
}
}
impl From<Vec<u8>> for ByteString {
fn from(bytes: Vec<u8>) -> Self {
ByteString::new(bytes)
}
}
impl From<&[u8]> for ByteString {
fn from(bytes: &[u8]) -> Self {
ByteString::new(bytes.to_vec())
}
}
impl<const N: usize> From<&[u8; N]> for ByteString {
fn from(bytes: &[u8; N]) -> Self {
ByteString::new(bytes.to_vec())
}
}
impl From<&str> for ByteString {
fn from(bytes: &str) -> Self {
ByteString::new(bytes.as_bytes().to_vec())
}
}
impl From<String> for ByteString {
fn from(bytes: String) -> Self {
ByteString::new(bytes.into_bytes())
}
}
impl From<ByteString> for String {
fn from(bytes: ByteString) -> Self {
String::from(from_utf8(&bytes.bytes).unwrap())
}
}
impl Deref for ByteString {
type Target = Vec<u8>;
fn deref(&self) -> &Vec<u8> {
&self.bytes
}
}
impl DerefMut for ByteString {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.bytes
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_byte_string_debug() {
assert_eq!(format!("{:?}", ByteString::from("abc")), r#""abc""#);
assert_eq!(
format!("{:?}", ByteString::from("\0\n\r\t")),
r#""\0\n\r\t""#
);
assert_eq!(
format!("{:?}", ByteString::from(b"\x01\x10\x7f\xff")),
r#""\x01\x10\x7f\xff""#
);
}
}