From: Kostya Shishkov Date: Sun, 4 Oct 2020 10:43:15 +0000 (+0200) Subject: core/io: introduce flush() call for ByteIO X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=cf095d5693cc631dbf4fd6fa1dc229f859978017;p=nihav.git core/io: introduce flush() call for ByteIO --- 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 FileReader { @@ -625,6 +629,8 @@ impl ByteIO for FileReader { 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 FileWriter { @@ -993,6 +1008,13 @@ impl ByteIO for FileWriter { fn size(&mut self) -> i64 { -1 } + + fn flush(&mut self) -> ByteIOResult<()> { + match self.file.flush() { + Ok(()) => Ok(()), + Err(_) => Err(ByteIOError::WriteError), + } + } } #[cfg(test)]