X-Git-Url: https://git.nihav.org/?a=blobdiff_plain;f=nihav-core%2Fsrc%2Fio%2Fbyteio.rs;h=7b5f220727e3ef21ceff699e7c40b53ebeb65ccf;hb=6c0356da10eef6e02cee247d334f8515dd2952cb;hp=ff899fc8a0d95b0b0e457813340bfb5c87c42eaa;hpb=422e87e4397fc168e568e629b8a0f2b6a9f01ad8;p=nihav.git diff --git a/nihav-core/src/io/byteio.rs b/nihav-core/src/io/byteio.rs index ff899fc..7b5f220 100644 --- a/nihav-core/src/io/byteio.rs +++ b/nihav-core/src/io/byteio.rs @@ -1,6 +1,5 @@ //! Bytestream reading/writing functionality. pub use std::io::SeekFrom; -use std::fs::File; use std::io::prelude::*; use std::ptr; @@ -76,7 +75,7 @@ pub trait ByteIO { /// [`MemoryReader`]: ./struct.MemoryReader.html #[allow(dead_code)] pub struct ByteReader<'a> { - io: &'a mut ByteIO, + io: &'a mut dyn ByteIO, } /// Bytestream reader from memory. @@ -85,9 +84,9 @@ pub struct MemoryReader<'a> { pos: usize, } -/// Bytestream reader from file. -pub struct FileReader<'a> { - file: &'a File, +/// Bytestream reader from anything implementing `std::io::Read` and `std::io::Seek`. +pub struct FileReader { + file: Box, eof: bool, } @@ -200,6 +199,7 @@ write_int_func!(write_u64le, u64, 8, to_le); /// # Ok(()) /// # } /// ```` +#[allow(clippy::identity_op)] pub fn write_u24be(dst: &mut [u8], val: u32) -> ByteIOResult<()> { if dst.len() < 3 { return Err(ByteIOError::WriteError); } dst[0] = (val >> 16) as u8; @@ -208,6 +208,7 @@ pub fn write_u24be(dst: &mut [u8], val: u32) -> ByteIOResult<()> { Ok(()) } /// Writes 24-bit little-endian integer to the provided buffer. +#[allow(clippy::identity_op)] pub fn write_u24le(dst: &mut [u8], val: u32) -> ByteIOResult<()> { if dst.len() < 3 { return Err(ByteIOError::WriteError); } dst[0] = (val >> 0) as u8; @@ -240,7 +241,7 @@ impl<'a> ByteReader<'a> { /// # Ok(()) /// # } /// ```` - pub fn new(io: &'a mut ByteIO) -> Self { ByteReader { io } } + pub fn new(io: &'a mut dyn ByteIO) -> Self { ByteReader { io } } /// Reads data into provided buffer. Partial read is treated as success. pub fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult { @@ -538,15 +539,15 @@ impl<'a> ByteIO for MemoryReader<'a> { } } -impl<'a> FileReader<'a> { +impl FileReader { /// Constructs a new instance of `FileReader`. - pub fn new_read(file: &'a mut File) -> Self { - FileReader { file, eof : false } + pub fn new_read(file: T) -> Self { + FileReader { file: Box::new(file), eof : false } } } -impl<'a> ByteIO for FileReader<'a> { +impl ByteIO for FileReader { fn read_byte(&mut self) -> ByteIOResult { let mut byte : [u8; 1] = [0]; let ret = self.file.read(&mut byte); @@ -563,18 +564,30 @@ impl<'a> ByteIO for FileReader<'a> { } fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult { - let ret = self.file.read(buf); - if ret.is_err() { return Err(ByteIOError::ReadError); } - let sz = ret.unwrap(); - if sz < buf.len() { self.eof = true; return Err(ByteIOError::EOF); } - Ok(sz) + match self.file.read_exact(buf) { + Ok(()) => Ok(buf.len()), + Err(err) => { + if err.kind() == std::io::ErrorKind::UnexpectedEof { + self.eof = true; + Err(ByteIOError::EOF) + } else { + Err(ByteIOError::ReadError) + } + }, + } } fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult { let ret = self.file.read(buf); if ret.is_err() { return Err(ByteIOError::ReadError); } let sz = ret.unwrap(); - if sz < buf.len() { self.eof = true; } + if sz < buf.len() { + if let Err(_err) = self.file.read(&mut buf[sz..][..1]) { + self.eof = true; + } else { + return Ok(sz + 1); + } + } Ok(sz) } @@ -638,7 +651,7 @@ impl<'a> ByteIO for FileReader<'a> { /// [`MemoryWriter`]: ./struct.MemoryWriter.html #[allow(dead_code)] pub struct ByteWriter<'a> { - io: &'a mut ByteIO, + io: &'a mut dyn ByteIO, } /// Bytestream writer to memory. @@ -647,9 +660,9 @@ pub struct MemoryWriter<'a> { pos: usize, } -/// Bytestream writer to file. -pub struct FileWriter { - file: File, +/// Bytestream writer to anything implementing `std::io::Write` and `std::io::Seek`. +pub struct FileWriter { + file: Box, } /// Bytestream writer to memory. @@ -664,7 +677,7 @@ pub struct GrowableMemoryWriter<'a> { impl<'a> ByteWriter<'a> { /// Constructs a new instance of `ByteWriter`. - pub fn new(io: &'a mut ByteIO) -> Self { ByteWriter { io } } + pub fn new(io: &'a mut dyn ByteIO) -> Self { ByteWriter { io } } /// Writes byte array to the output. pub fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> { @@ -807,9 +820,7 @@ impl<'a> ByteIO for MemoryWriter<'a> { fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> { if self.pos + buf.len() > self.buf.len() { return Err(ByteIOError::WriteError); } - for i in 0..buf.len() { - self.buf[self.pos + i] = buf[i]; - } + self.buf[self.pos..][..buf.len()].copy_from_slice(buf); self.pos += buf.len(); Ok(()) } @@ -887,9 +898,7 @@ impl<'a> ByteIO for GrowableMemoryWriter<'a> { if self.pos + buf.len() > self.buf.len() { self.buf.resize(self.pos + buf.len(), 0); } - for i in 0..buf.len() { - self.buf[self.pos + i] = buf[i]; - } + self.buf[self.pos..][..buf.len()].copy_from_slice(buf); self.pos += buf.len(); Ok(()) } @@ -921,14 +930,14 @@ impl<'a> ByteIO for GrowableMemoryWriter<'a> { } } -impl FileWriter { +impl FileWriter { /// Constructs a new instance of `FileWriter`. - pub fn new_write(file: File) -> Self { - FileWriter { file } + pub fn new_write(file: T) -> Self { + FileWriter { file: Box::new(file) } } } -impl ByteIO for FileWriter { +impl ByteIO for FileWriter { #[allow(unused_variables)] fn read_byte(&mut self) -> ByteIOResult { Err(ByteIOError::NotImplemented)