diff options
author | Kostya Shishkov <kostya.shishkov@gmail.com> | 2020-10-04 12:43:15 +0200 |
---|---|---|
committer | Kostya Shishkov <kostya.shishkov@gmail.com> | 2020-10-04 12:43:15 +0200 |
commit | cf095d5693cc631dbf4fd6fa1dc229f859978017 (patch) | |
tree | 89ecb7e3e1846b3139597caf94d48549180f8222 | |
parent | fc74462bb783c7b1ca9220fd3e901b9510efecb0 (diff) | |
download | nihav-cf095d5693cc631dbf4fd6fa1dc229f859978017.tar.gz |
core/io: introduce flush() call for ByteIO
-rw-r--r-- | nihav-core/src/io/byteio.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/nihav-core/src/io/byteio.rs b/nihav-core/src/io/byteio.rs index 7b5f220..44b2684 100644 --- a/nihav-core/src/io/byteio.rs +++ b/nihav-core/src/io/byteio.rs @@ -49,6 +49,8 @@ pub trait ByteIO { fn is_seekable(&mut self) -> bool; /// Returns stream size or -1 if it is not known. fn size(&mut self) -> i64; + /// Flushes output if possible. + fn flush(&mut self) -> ByteIOResult<()>; } /// High-level bytestream reader. @@ -537,6 +539,8 @@ impl<'a> ByteIO for MemoryReader<'a> { fn size(&mut self) -> i64 { self.buf.len() as i64 } + + fn flush(&mut self) -> ByteIOResult<()> { Ok(()) } } impl<T: Read+Seek> FileReader<T> { @@ -625,6 +629,8 @@ impl<T: Read+Seek> ByteIO for FileReader<T> { fn size(&mut self) -> i64 { -1 } + + fn flush(&mut self) -> ByteIOResult<()> { Ok(()) } } /// High-level bytestream writer. @@ -774,6 +780,11 @@ impl<'a> ByteWriter<'a> { if sz == -1 { return -1; } sz - (self.tell() as i64) } + + /// Flushes output stream if possible. + pub fn flush(&mut self) -> ByteIOResult<()> { + self.io.flush() + } } impl<'a> MemoryWriter<'a> { @@ -850,6 +861,8 @@ impl<'a> ByteIO for MemoryWriter<'a> { fn size(&mut self) -> i64 { self.buf.len() as i64 } + + fn flush(&mut self) -> ByteIOResult<()> { Ok(()) } } impl<'a> GrowableMemoryWriter<'a> { @@ -928,6 +941,8 @@ impl<'a> ByteIO for GrowableMemoryWriter<'a> { fn size(&mut self) -> i64 { self.buf.len() as i64 } + + fn flush(&mut self) -> ByteIOResult<()> { Ok(()) } } impl<T: Write+Seek> FileWriter<T> { @@ -993,6 +1008,13 @@ impl<T: Write+Seek> ByteIO for FileWriter<T> { fn size(&mut self) -> i64 { -1 } + + fn flush(&mut self) -> ByteIOResult<()> { + match self.file.flush() { + Ok(()) => Ok(()), + Err(_) => Err(ByteIOError::WriteError), + } + } } #[cfg(test)] |