Basic PoC for read_bytes

Mostly just needed to reassure myself that the lifetimes work.
This commit is contained in:
Bradlee Speice 2019-04-23 15:34:00 -04:00
parent cf92d98671
commit 1cec9a0fe6

View File

@ -141,6 +141,10 @@ impl<'a> BytesReader<'a> {
bits_left: 0, bits_left: 0,
} }
} }
fn remaining(&self) -> usize {
self.bytes.len().checked_sub(self.pos).unwrap_or(0)
}
} }
impl<'a> From<&'a [u8]> for BytesReader<'a> { impl<'a> From<&'a [u8]> for BytesReader<'a> {
fn from(b: &'a [u8]) -> Self { fn from(b: &'a [u8]) -> Self {
@ -244,12 +248,23 @@ impl<'a> KStream for BytesReader<'a> {
unimplemented!() unimplemented!()
} }
fn read_bytes(&mut self, _len: usize) -> io::Result<&[u8]> { fn read_bytes(&mut self, len: usize) -> io::Result<&[u8]> {
unimplemented!() if len > self.remaining() {
return Err(io::Error::from(io::ErrorKind::UnexpectedEof).into())
}
let slice = &self.bytes[self.pos..self.pos+len];
self.pos += len;
Ok(slice)
} }
fn read_bytes_full(&mut self) -> io::Result<&[u8]> { fn read_bytes_full(&mut self) -> io::Result<&[u8]> {
unimplemented!() if self.remaining() > 0 {
self.pos = self.bytes.len();
Ok(&self.bytes[self.pos..])
} else {
Err(io::Error::from(io::ErrorKind::UnexpectedEof).into())
}
} }
fn read_bytes_term( fn read_bytes_term(
@ -274,4 +289,27 @@ mod tests {
assert_eq!([1, 2, 3, 4], c); assert_eq!([1, 2, 3, 4], c);
} }
#[test]
fn basic_read_bytes() {
let b = vec![1, 2, 3, 4, 5, 6, 7, 8];
let mut reader = BytesReader::from(b.as_slice());
assert_eq!(
reader.read_bytes(4).unwrap(),
&[1, 2, 3, 4]
);
assert_eq!(
reader.read_bytes(3).unwrap(),
&[5, 6, 7]
);
assert_eq!(
reader.read_bytes(4).unwrap_err().kind(),
io::ErrorKind::UnexpectedEof
);
assert_eq!(
reader.read_bytes(1).unwrap(),
&[8]
);
}
} }