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

use crate::{BytesRead, BytesSeek, Cursor};

/// 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.inner.inner()
	}

}

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 read(&mut self, len: usize) -> &[u8] {
		self.inner.read(len)
	}

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

}

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

	/// Sets the internal position.
	/// 
	/// ## Panics
	/// If the position exceeds the slice.
	fn seek(&mut self, pos: usize) {
		self.inner.seek(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 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);
	}

}