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

use crate::{BytesRead, ReadError, BytesReadRef, BytesSeek, SeekError, Cursor};

use std::io;

/// A slice wrapper that implements BytesRead.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Bytes<'a> {
	inner: Cursor<&'a [u8]>
}

impl<'a> Bytes<'a> {
	/// You should probably use:
	///
	/// ```
	/// # use simple_bytes::Bytes;
	/// # let slice = &[1u8, 2u8][..];
	/// let bytes: Bytes = slice.into();
	/// // or
	/// let bytes = Bytes::from(slice);
	/// ```
	///
	pub fn new(position: usize, inner: &'a [u8]) -> Self {
		let mut cursor = Cursor::new(inner);
		cursor.seek(position);
		Self { inner: cursor }
	}

	/// Returns the inner slice with the original reference.
	pub fn inner(&self) -> &'a [u8] {
		self.as_slice_ref()
	}
}

impl BytesRead for Bytes<'_> {
	// returns the full slice
	#[inline]
	fn as_slice(&self) -> &[u8] {
		self.inner.as_slice()
	}

	#[inline]
	fn remaining(&self) -> &[u8] {
		self.inner.remaining()
	}

	#[inline]
	fn try_read(&mut self, len: usize) -> Result<&[u8], ReadError> {
		self.inner.try_read(len)
	}

	#[inline]
	fn peek(&self, len: usize) -> Option<&[u8]> {
		self.inner.peek(len)
	}
}

impl io::Read for Bytes<'_> {
	fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
		io::Read::read(&mut self.inner, buf)
	}
}

impl<'a> BytesReadRef<'a> for Bytes<'a> {

	// returns the full slice
	#[inline]
	fn as_slice_ref(&self) -> &'a [u8] {
		*self.inner.inner()
	}

	#[inline]
	fn remaining_ref(&self) -> &'a [u8] {
		&self.as_slice_ref()[self.position()..]
	}

	#[inline]
	fn try_read_ref(&mut self, len: usize) -> Result<&'a [u8], ReadError> {
		let slice = &self.as_slice_ref()[self.position()..].get(..len)
			.ok_or(ReadError)?;
		// the previous line did not panic
		// so let's increase our position
		self.seek(self.position() + len);

		Ok(slice)
	}

	#[inline]
	fn peek_ref(&self, len: usize) -> Option<&'a [u8]> {
		self.remaining_ref().get(..len)
	}

}

impl BytesSeek for Bytes<'_> {
	/// Returns the internal position.
	fn position(&self) -> usize {
		self.inner.position()
	}

	/// Sets the internal position.
	/// 
	/// ## Fails
	/// If the position exceeds the slice.
	fn try_seek(&mut self, pos: usize) -> Result<(), SeekError> {
		self.inner.try_seek(pos)
	}
}

impl io::Seek for Bytes<'_> {
	fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
		io::Seek::seek(&mut self.inner, pos)
	}
}

impl<'a> From<&'a [u8]> for Bytes<'a> {
	fn from(s: &'a [u8]) -> Self {
		Self::new(0, s)
	}
}

#[cfg(test)]
mod tests {

	use super::*;

	#[test]
	fn read() {

		let bytes: Vec<u8> = (0..=255).collect();
		let mut bytes = Bytes::from(bytes.as_slice());
		assert_eq!(bytes.as_slice(), bytes.as_slice());
		assert_eq!(bytes.len(), 256);
		assert_eq!(bytes.remaining().len(), 256);

		let to_read: Vec<u8> = (0..10).collect();
		assert_eq!(to_read.as_slice(), bytes.read(10));
		assert_eq!(bytes.remaining().len(), 256 - 10);

		assert_eq!(10u8, bytes.read_u8());

		// peek
		let to_peek: Vec<u8> = (11..=20).collect();
		assert_eq!(to_peek.as_slice(), bytes.peek(10).unwrap());

		bytes.seek(255);
		assert_eq!(255u8, bytes.read_u8());

		assert_eq!(bytes.position(), 256);
		bytes.seek(256);
	}

	#[test]
	fn read_le() {

		let b = u16::MAX - 20;
		println!("be: {:?}", b.to_be_bytes());
		let bytes = b.to_le_bytes();
		println!("le: {:?}", bytes);
		let mut bytes = Bytes::from(&bytes[..]);
		assert_ne!(bytes.read_u16(), b);
		bytes.seek(0);
		println!("buffer: {:?}", bytes.as_slice());
		assert_eq!(bytes.read_le_u16(), b);

	}

	#[test]
	fn test_empty() {
		let mut bytes = Bytes::from(&[][..]);
		assert_eq!(bytes.as_slice(), &[]);
		assert_eq!(bytes.len(), 0);
		bytes.seek(0);
	}

	#[test]
	#[should_panic]
	fn test_seek_empty() {
		let mut bytes = Bytes::from(&[][..]);
		bytes.seek(1);
	}

	#[test]
	#[should_panic]
	fn read_out_of_bound() {

		let bytes = [0u8; 100];
		let mut bytes = Bytes::from(&bytes[..]);

		bytes.seek(100);

		let _ = bytes.read_u8();
	}

	#[test]
	#[should_panic]
	fn seek_out_of_bound() {

		let bytes = [0u8; 100];
		let mut bytes = Bytes::from(&bytes[..]);

		bytes.seek(101);
	}

}