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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use std::fmt;
use std::cmp::min;
use std::ops::{Deref, DerefMut};
pub struct PrettySlice<'a> (&'a [u8]);
impl<'a> fmt::Debug for PrettySlice<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for i in 0..self.0.len() {
match i > 0 {
true => { write!(f, "·{:02x}", self.0[i])?; },
false => { write!(f, "{:02x}", self.0[i])?; },
}
}
Ok(())
}
}
impl<'a> fmt::Display for PrettySlice<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for i in 0..self.0.len() {
write!(f, "{:02x}", self.0[i])?;
}
Ok(())
}
}
pub trait ToPretty {
fn pretty(&self) -> PrettySlice;
fn to_hex(&self) -> String {
format!("{}", self.pretty())
}
}
impl<T: AsRef<[u8]>> ToPretty for T {
fn pretty(&self) -> PrettySlice {
PrettySlice(self.as_ref())
}
}
pub enum BytesRef<'a> {
Flexible(&'a mut Bytes),
Fixed(&'a mut [u8])
}
impl<'a> BytesRef<'a> {
pub fn write(&mut self, offset: usize, input: &[u8]) -> usize {
match *self {
BytesRef::Flexible(ref mut data) => {
let data_len = data.len();
let wrote = input.len() + if data_len > offset { 0 } else { offset - data_len };
data.resize(offset, 0);
data.extend_from_slice(input);
wrote
},
BytesRef::Fixed(ref mut data) if offset < data.len() => {
let max = min(data.len() - offset, input.len());
for i in 0..max {
data[offset + i] = input[i];
}
max
},
_ => 0
}
}
}
impl<'a> Deref for BytesRef<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] {
match *self {
BytesRef::Flexible(ref bytes) => bytes,
BytesRef::Fixed(ref bytes) => bytes,
}
}
}
impl <'a> DerefMut for BytesRef<'a> {
fn deref_mut(&mut self) -> &mut [u8] {
match *self {
BytesRef::Flexible(ref mut bytes) => bytes,
BytesRef::Fixed(ref mut bytes) => bytes,
}
}
}
pub type Bytes = Vec<u8>;
#[cfg(test)]
mod tests {
use super::BytesRef;
#[test]
fn should_write_bytes_to_fixed_bytesref() {
let mut data1 = vec![0, 0, 0];
let mut data2 = vec![0, 0, 0];
let (res1, res2) = {
let mut bytes1 = BytesRef::Fixed(&mut data1[..]);
let mut bytes2 = BytesRef::Fixed(&mut data2[1..2]);
let res1 = bytes1.write(1, &[1, 1, 1]);
let res2 = bytes2.write(3, &[1, 1, 1]);
(res1, res2)
};
assert_eq!(&data1, &[0, 1, 1]);
assert_eq!(res1, 2);
assert_eq!(&data2, &[0, 0, 0]);
assert_eq!(res2, 0);
}
#[test]
fn should_write_bytes_to_flexible_bytesref() {
let mut data1 = vec![0, 0, 0];
let mut data2 = vec![0, 0, 0];
let mut data3 = vec![0, 0, 0];
let (res1, res2, res3) = {
let mut bytes1 = BytesRef::Flexible(&mut data1);
let mut bytes2 = BytesRef::Flexible(&mut data2);
let mut bytes3 = BytesRef::Flexible(&mut data3);
let res1 = bytes1.write(1, &[1, 1, 1]);
let res2 = bytes2.write(3, &[1, 1, 1]);
let res3 = bytes3.write(5, &[1, 1, 1]);
(res1, res2, res3)
};
assert_eq!(&data1, &[0, 1, 1, 1]);
assert_eq!(res1, 3);
assert_eq!(&data2, &[0, 0, 0, 1, 1, 1]);
assert_eq!(res2, 3);
assert_eq!(&data3, &[0, 0, 0, 0, 0, 1, 1, 1]);
assert_eq!(res3, 5);
}
}