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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use crate::{Bytes, Cursor, BytesRead, BytesWrite, BytesSeek};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BytesOwned {
inner: Cursor<Vec<u8>>
}
impl BytesOwned {
pub fn new() -> Self {
Self {
inner: Cursor::new(vec![])
}
}
pub fn with_capacity(cap: usize) -> Self {
Self {
inner: Cursor::new(Vec::with_capacity(cap))
}
}
pub fn new_raw(position: usize, inner: Vec<u8>) -> Self {
let mut cursor = Cursor::new(inner);
cursor.seek(position);
Self { inner: cursor }
}
#[inline]
pub fn as_mut_vec(&mut self) -> &mut Vec<u8> {
self.inner.inner_mut()
}
#[inline]
pub fn into_vec(self) -> Vec<u8> {
self.inner.into_inner()
}
}
impl BytesRead for BytesOwned {
#[inline]
fn as_slice(&self) -> &[u8] {
self.inner.as_slice()
}
#[inline]
fn len(&self) -> usize {
self.inner.len()
}
#[inline]
fn remaining(&self) -> &[u8] {
self.inner.remaining()
}
#[inline]
fn read(&mut self, len: usize) -> &[u8] {
self.inner.read(len)
}
#[inline]
fn peek(&self, len: usize) -> Option<&[u8]> {
self.inner.peek(len)
}
}
impl BytesWrite for BytesOwned {
#[inline]
fn as_mut(&mut self) -> &mut [u8] {
self.inner.as_mut()
}
#[inline]
fn as_bytes(&self) -> Bytes<'_> {
self.inner.as_bytes()
}
#[inline]
fn remaining_mut(&mut self) -> &mut [u8] {
self.inner.remaining_mut()
}
#[inline]
fn write(&mut self, slice: impl AsRef<[u8]>) {
self.inner.write(slice)
}
}
impl BytesSeek for BytesOwned {
fn position(&self) -> usize {
self.inner.position()
}
fn seek(&mut self, pos: usize) {
self.inner.seek(pos)
}
}
impl From<Vec<u8>> for BytesOwned {
fn from(b: Vec<u8>) -> Self {
Self::new_raw(0, b)
}
}
impl From<BytesOwned> for Vec<u8> {
fn from(b: BytesOwned) -> Self {
b.into_vec()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn write() {
let mut bytes = BytesOwned::new();
assert_eq!(bytes.len(), 0);
let to_write: Vec<u8> = (0..10).collect();
bytes.write(&to_write);
bytes.write(&to_write);
assert_eq!(bytes.len(), 20);
assert_eq!(&bytes.as_mut()[..10], to_write.as_slice());
assert_eq!(&bytes.as_mut()[10..20], to_write.as_slice());
bytes.write_u8(5u8);
bytes.write_u16(20u16);
assert_eq!(bytes.len(), 23);
bytes.seek(20);
assert_eq!(bytes.len(), 23);
bytes.seek(99);
assert_eq!(bytes.len(), 99);
bytes.write_u8(5u8);
assert_eq!(bytes.as_mut()[99], 5u8);
assert_eq!(bytes.len(), 100);
}
#[test]
fn read() {
use crate::BytesRead;
let bytes: Vec<u8> = (1..=255).collect();
let mut bytes: BytesOwned = bytes.into();
assert_eq!(bytes.as_slice(), bytes.as_slice());
assert_eq!(bytes.len(), 255);
assert_eq!(bytes.remaining().len(), 255);
let to_read: Vec<u8> = (1..=10).collect();
assert_eq!(to_read.as_slice(), bytes.read(10));
assert_eq!(bytes.remaining().len(), 255 - 10);
assert_eq!(11u8, bytes.read_u8());
let to_peek: Vec<u8> = (12..=20).collect();
assert_eq!(to_peek.as_slice(), bytes.peek(to_peek.len()).unwrap());
bytes.seek(255);
assert_eq!(bytes.remaining().len(), 0);
bytes.seek(254);
assert_eq!(255u8, bytes.read_u8());
bytes.seek(256);
bytes.seek(255);
assert_eq!(0u8, bytes.read_u8());
}
#[test]
fn test_empty() {
let mut bytes = BytesOwned::new();
assert_eq!(bytes.as_slice(), &[]);
assert_eq!(bytes.len(), 0);
bytes.seek(0);
assert_eq!(bytes.len(), 0);
}
}