fix some warnings (unneeded parentheses, missing dyn keyword)
[nihav.git] / nihav-core / src / io / byteio.rs
index e7bb6c396a817e9c4cc50f94c32a6616832880d5..adbb6369264ae3e3bd02fd6e96b9a14264945665 100644 (file)
@@ -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<T: Read+Seek> {
+    file:     Box<T>,
     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<usize> {
@@ -538,15 +539,15 @@ impl<'a> ByteIO for MemoryReader<'a> {
     }
 }
 
-impl<'a> FileReader<'a> {
+impl<T: Read+Seek> FileReader<T> {
 
     /// 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<T: Read+Seek> ByteIO for FileReader<T> {
     fn read_byte(&mut self) -> ByteIOResult<u8> {
         let mut byte : [u8; 1] = [0];
         let ret = self.file.read(&mut byte);
@@ -638,7 +639,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,14 +648,24 @@ 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<T: Write+Seek> {
+    file:     Box<T>,
+}
+
+/// Bytestream writer to memory.
+///
+/// Unlike [`MemoryWriter`] which writes to an array of fixed size, `GrowableMemoryWriter` grows output size when output size exceeds capacity.
+///
+/// [`MemoryWriter`]: ./struct.MemoryWriter.html
+pub struct GrowableMemoryWriter<'a> {
+    buf:    &'a mut Vec<u8>,
+    pos:    usize,
 }
 
 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<()> {
@@ -797,9 +808,85 @@ 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(())
+    }
+
+    fn tell(&mut self) -> u64 {
+        self.pos as u64
+    }
+
+    fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
+        let cur_pos  = self.pos       as i64;
+        let cur_size = self.buf.len() as i64;
+        match pos {
+            SeekFrom::Start(x)   => self.real_seek(x as i64),
+            SeekFrom::Current(x) => self.real_seek(cur_pos + x),
+            SeekFrom::End(x)     => self.real_seek(cur_size + x),
+        }
+    }
+
+    fn is_eof(&self) -> bool {
+        self.pos >= self.buf.len()
+    }
+
+    fn is_seekable(&mut self) -> bool {
+        true
+    }
+
+    fn size(&mut self) -> i64 {
+        self.buf.len() as i64
+    }
+}
+
+impl<'a> GrowableMemoryWriter<'a> {
+
+    /// Constructs a new instance of `GrowableMemoryWriter`.
+    pub fn new_write(buf: &'a mut Vec<u8>) -> Self {
+        GrowableMemoryWriter { buf, pos: 0 }
+    }
+
+    fn real_seek(&mut self, pos: i64) -> ByteIOResult<u64> {
+        if pos < 0 || (pos as usize) > self.buf.len() {
+            return Err(ByteIOError::WrongRange)
+        }
+        self.pos = pos as usize;
+        Ok(pos as u64)
+    }
+}
+
+impl<'a> ByteIO for GrowableMemoryWriter<'a> {
+    #[allow(unused_variables)]
+    fn read_byte(&mut self) -> ByteIOResult<u8> {
+        Err(ByteIOError::NotImplemented)
+    }
+
+    #[allow(unused_variables)]
+    fn peek_byte(&mut self) -> ByteIOResult<u8> {
+        Err(ByteIOError::NotImplemented)
+    }
+
+    #[allow(unused_variables)]
+    fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
+        Err(ByteIOError::NotImplemented)
+    }
+
+    #[allow(unused_variables)]
+    fn read_buf_some(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
+        Err(ByteIOError::NotImplemented)
+    }
+
+    #[allow(unused_variables)]
+    fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
+        Err(ByteIOError::NotImplemented)
+    }
+
+    fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
+        if self.pos + buf.len() > self.buf.len() {
+            self.buf.resize(self.pos + buf.len(), 0);
         }
+        self.buf[self.pos..][..buf.len()].copy_from_slice(buf);
         self.pos += buf.len();
         Ok(())
     }
@@ -831,14 +918,14 @@ impl<'a> ByteIO for MemoryWriter<'a> {
     }
 }
 
-impl FileWriter {
+impl<T: Write+Seek> FileWriter<T> {
     /// 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<T: Write+Seek> ByteIO for FileWriter<T> {
     #[allow(unused_variables)]
     fn read_byte(&mut self) -> ByteIOResult<u8> {
         Err(ByteIOError::NotImplemented)