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

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

/// A mutable slice wrapper that implements BytesWrite
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct BytesMut<'a> {
	inner: Cursor<&'a mut [u8]>
}

impl<'a> BytesMut<'a> {

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

}

impl BytesRead for BytesMut<'_> {

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

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

	fn read(&mut self, len: usize) -> &[u8] {
		self.inner.read(len)
	}

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

}

impl BytesWrite for BytesMut<'_> {

	#[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: &[u8]) {
		self.inner.write(slice)
	}

}

impl BytesSeek for BytesMut<'_> {
	/// 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 mut [u8]> for BytesMut<'a> {
	fn from(s: &'a mut [u8]) -> Self {
		Self::new(0, s)
	}
}


#[cfg(test)]
mod tests {

	use super::*;
	use crate::BytesRead;

	#[test]
	fn write() {

		let mut bytes = [0u8; 100];
		let mut bytes = BytesMut::from(&mut bytes[..]);
		assert_eq!(bytes.len(), 100);

		let to_write: Vec<u8> = (0..10).collect();
		bytes.write(&to_write);
		bytes.write(&to_write);

		assert_eq!(bytes.remaining().len(), 100 - 20);
		assert_eq!(bytes.remaining().len(), bytes.remaining_mut().len());

		assert_eq!(&bytes.as_mut()[..10], to_write.as_slice());
		assert_eq!(&bytes.as_bytes().peek(20).unwrap()[10..], to_write.as_slice());

		bytes.write_u8(5u8);
		bytes.write_u16(20u16);

		assert_eq!(bytes.remaining_mut().len(), 100 - 23);

		// seek
		bytes.seek(99);
		// should now write to the 99 byte // this is the last byte
		bytes.write_u8(5u8);
		assert_eq!(bytes.remaining_mut().len(), 0);
		assert_eq!(bytes.as_mut()[99], 5u8);

	}

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

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

		// seek
		bytes.seek(100);

		bytes.write_u8(5u8);
	}

}