simple_bytes/
bytes_array.rs

1//! # Note
2//!	Internally there exists only one position
3//! So if you read and write you should keep this in mind
4
5use crate::{
6	Bytes, Cursor,
7	BytesRead, ReadError,
8	BytesWrite, WriteError,
9	BytesSeek, SeekError
10};
11
12use std::io;
13
14
15/// A array wrapper that implements BytesWrite and BytesRead
16#[derive(Debug, Clone, PartialEq, Eq, Hash)]
17pub struct BytesArray<const N: usize> {
18	inner: Cursor<[u8; N]>
19}
20
21impl<const N: usize> BytesArray<N> {
22	/// You should probably use:
23	///
24	/// ```
25	/// # use simple_bytes::BytesArray;
26	/// # let arr = [1u8, 2u8];
27	/// let bytes: BytesArray<2> = arr.into();
28	/// // or
29	/// let bytes = BytesArray::from(arr);
30	/// ```
31	///
32	pub fn new(position: usize, inner: [u8; N]) -> Self {
33		let mut cursor = Cursor::new(inner);
34		cursor.seek(position);
35		Self { inner: cursor }
36	}
37
38	/// Returns the underlying array mutably.
39	#[inline]
40	pub fn as_mut_array(&mut self) -> &mut [u8; N] {
41		self.inner.inner_mut()
42	}
43
44	/// Returns the underlying Array.
45	#[inline]
46	pub fn into_array(self) -> [u8; N] {
47		self.inner.into_inner()
48	}
49
50}
51
52impl<const N: usize> BytesRead for BytesArray<N> {
53	#[inline]
54	fn as_slice(&self) -> &[u8] {
55		self.inner.as_slice()
56	}
57
58	#[inline]
59	fn len(&self) -> usize {
60		self.inner.len()
61	}
62
63	#[inline]
64	fn remaining(&self) -> &[u8] {
65		self.inner.remaining()
66	}
67
68	#[inline]
69	fn try_read(&mut self, len: usize) -> Result<&[u8], ReadError> {
70		self.inner.try_read(len)
71	}
72
73	#[inline]
74	fn peek(&self, len: usize) -> Option<&[u8]> {
75		self.inner.peek(len)
76	}
77}
78
79impl<const N: usize> io::Read for BytesArray<N> {
80	fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
81		io::Read::read(&mut self.inner, buf)
82	}
83}
84
85impl<const N: usize> BytesWrite for BytesArray<N> {
86	#[inline]
87	fn as_mut(&mut self) -> &mut [u8] {
88		self.inner.as_mut()
89	}
90
91	#[inline]
92	fn as_bytes(&self) -> Bytes<'_> {
93		self.inner.as_bytes()
94	}
95
96	/// Returns the remaining mutable slice.
97	/// 
98	/// If an empty slice is returned, this does not mean
99	/// you can't write anymore.
100	#[inline]
101	fn remaining_mut(&mut self) -> &mut [u8] {
102		self.inner.remaining_mut()
103	}
104
105	/// Writes a slice. Allocates more space if the slice is
106	/// bigger than the `Vec`.
107	#[inline]
108	fn try_write(&mut self, slice: impl AsRef<[u8]>) -> Result<(), WriteError> {
109		self.inner.try_write(slice)
110	}
111}
112
113impl<const N: usize> io::Write for BytesArray<N> {
114	fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
115		io::Write::write(&mut self.inner, buf)
116	}
117
118	fn flush(&mut self) -> io::Result<()> {
119		Ok(())
120	}
121}
122
123impl<const N: usize> BytesSeek for BytesArray<N> {
124	/// Returns the internal position.
125	fn position(&self) -> usize {
126		self.inner.position()
127	}
128
129	/// Sets the internal position, allocating more space
130	/// if the position is bigger than the `Vec`.
131	fn try_seek(&mut self, pos: usize) -> Result<(), SeekError> {
132		self.inner.try_seek(pos)
133	}
134}
135
136impl<const N: usize> io::Seek for BytesArray<N> {
137	fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
138		io::Seek::seek(&mut self.inner, pos)
139	}
140}
141
142impl<const N: usize> From<[u8; N]> for BytesArray<N> {
143	fn from(b: [u8; N]) -> Self {
144		Self::new(0, b)
145	}
146}
147
148impl<const N: usize> From<BytesArray<N>> for [u8; N] {
149	fn from(b: BytesArray<N>) -> Self {
150		b.into_array()
151	}
152}
153
154
155#[cfg(test)]
156mod tests {
157	use super::*;
158	use crate::BytesRead;
159
160	#[test]
161	fn write() {
162		let mut bytes = BytesArray::from([0u8; 100]);
163		assert_eq!(bytes.len(), 100);
164
165		let to_write: Vec<u8> = (0..10).collect();
166		bytes.write(&to_write);
167		bytes.write(&to_write);
168
169		assert_eq!(bytes.remaining().len(), 100 - 20);
170		assert_eq!(bytes.remaining().len(), bytes.remaining_mut().len());
171
172		assert_eq!(&bytes.as_mut()[..10], to_write.as_slice());
173		assert_eq!(&bytes.as_bytes().peek(20).unwrap()[10..], to_write.as_slice());
174
175		bytes.write_u8(5u8);
176		bytes.write_u16(20u16);
177
178		assert_eq!(bytes.remaining_mut().len(), 100 - 23);
179
180		// seek
181		bytes.seek(99);
182		// should now write to the 99 byte // this is the last byte
183		bytes.write_u8(5u8);
184		assert_eq!(bytes.remaining_mut().len(), 0);
185		assert_eq!(bytes.as_mut()[99], 5u8);
186
187	}
188
189	#[test]
190	fn write_le() {
191		let b = u16::MAX - 20;
192		let le = b.to_le_bytes();
193		let mut bytes = BytesArray::from([0u8; 2]);
194		bytes.write_le_u16(b);
195		assert_eq!(bytes.as_slice(), le);
196	}
197
198	#[test]
199	fn test_empty() {
200		let mut bytes = BytesArray::from([0u8; 0]);
201		assert_eq!(bytes.as_slice(), &[]);
202		assert_eq!(bytes.len(), 0);
203		bytes.seek(0);
204	}
205
206	#[test]
207	#[should_panic]
208	fn write_overflow() {
209		let mut bytes = BytesArray::from([0u8; 100]);
210
211		// seek
212		bytes.seek(100);
213
214		bytes.write_u8(5u8);
215	}
216}