diff options
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/cursor.rs | 60 | ||||
| -rw-r--r-- | src/utils/mod.rs | 5 | ||||
| -rw-r--r-- | src/utils/read.rs | 23 |
3 files changed, 88 insertions, 0 deletions
diff --git a/src/utils/cursor.rs b/src/utils/cursor.rs new file mode 100644 index 0000000..c41270a --- /dev/null +++ b/src/utils/cursor.rs | |||
| @@ -0,0 +1,60 @@ | |||
| 1 | use std::io::{Error, ErrorKind, Read, Result, Seek, SeekFrom, Write}; | ||
| 2 | |||
| 3 | pub struct IoCursor<Io> { | ||
| 4 | io: Io, | ||
| 5 | cursor: u64, | ||
| 6 | bounds: (u64, u64), | ||
| 7 | } | ||
| 8 | |||
| 9 | impl<Io: Seek> IoCursor<Io> { | ||
| 10 | pub fn new(mut io: Io, start: u64, end: u64) -> Result<Self> { | ||
| 11 | let cursor = io.seek(SeekFrom::Start(start))?; | ||
| 12 | Ok(Self { | ||
| 13 | io, | ||
| 14 | cursor, | ||
| 15 | bounds: (cursor, end), | ||
| 16 | }) | ||
| 17 | } | ||
| 18 | } | ||
| 19 | |||
| 20 | impl<Io: Read> Read for IoCursor<Io> { | ||
| 21 | fn read(&mut self, buf: &mut [u8]) -> Result<usize> { | ||
| 22 | let upper = buf.len().min((self.bounds.1 - self.cursor) as usize); | ||
| 23 | let bytes = self.io.read(&mut buf[..upper])?; | ||
| 24 | self.cursor += bytes as u64; | ||
| 25 | Ok(bytes) | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | impl<Io: Write> Write for IoCursor<Io> { | ||
| 30 | fn write(&mut self, buf: &[u8]) -> Result<usize> { | ||
| 31 | let upper = buf.len().min((self.bounds.1 - self.cursor) as usize); | ||
| 32 | let bytes = self.io.write(&buf[..upper])?; | ||
| 33 | self.cursor += bytes as u64; | ||
| 34 | Ok(bytes) | ||
| 35 | } | ||
| 36 | |||
| 37 | #[inline] | ||
| 38 | fn flush(&mut self) -> Result<()> { | ||
| 39 | self.io.flush() | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | impl<Io: Seek> Seek for IoCursor<Io> { | ||
| 44 | fn seek(&mut self, pos: SeekFrom) -> Result<u64> { | ||
| 45 | self.cursor = match pos { | ||
| 46 | SeekFrom::Start(0) => return Ok(self.cursor - self.bounds.0), | ||
| 47 | SeekFrom::Start(offset) => self.bounds.0.checked_add(offset), | ||
| 48 | SeekFrom::End(offset) => self.bounds.1.checked_add_signed(offset), | ||
| 49 | SeekFrom::Current(offset) => self.cursor.checked_add_signed(offset), | ||
| 50 | } | ||
| 51 | .filter(|v| *v >= self.bounds.0) | ||
| 52 | .ok_or(Error::new( | ||
| 53 | ErrorKind::InvalidInput, | ||
| 54 | "Invalid seek to a negative or overflowing position", | ||
| 55 | ))? | ||
| 56 | .min(self.bounds.1); | ||
| 57 | |||
| 58 | Ok(self.io.seek(SeekFrom::Start(self.cursor))? - self.bounds.0) | ||
| 59 | } | ||
| 60 | } | ||
diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 0000000..99a4e13 --- /dev/null +++ b/src/utils/mod.rs | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | mod cursor; | ||
| 2 | mod read; | ||
| 3 | |||
| 4 | pub use cursor::IoCursor; | ||
| 5 | pub use read::ReadUtils; | ||
diff --git a/src/utils/read.rs b/src/utils/read.rs new file mode 100644 index 0000000..185758a --- /dev/null +++ b/src/utils/read.rs | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | use std::io::{Read, Result as IOResult}; | ||
| 2 | |||
| 3 | pub trait ReadUtils { | ||
| 4 | fn read_arr<const S: usize>(&mut self) -> IOResult<[u8; S]>; | ||
| 5 | |||
| 6 | fn read_vec(&mut self, size: usize) -> IOResult<Vec<u8>>; | ||
| 7 | } | ||
| 8 | |||
| 9 | impl<R: Read> ReadUtils for R { | ||
| 10 | #[inline] | ||
| 11 | fn read_arr<const S: usize>(&mut self) -> Result<[u8; S], std::io::Error> { | ||
| 12 | let mut arr = [0; S]; | ||
| 13 | self.read(&mut arr)?; | ||
| 14 | Ok(arr) | ||
| 15 | } | ||
| 16 | |||
| 17 | #[inline] | ||
| 18 | fn read_vec(&mut self, size: usize) -> Result<Vec<u8>, std::io::Error> { | ||
| 19 | let mut vec = vec![0; size]; | ||
| 20 | self.read(&mut vec)?; | ||
| 21 | Ok(vec) | ||
| 22 | } | ||
| 23 | } | ||
