X-Git-Url: https://git.nihav.org/?a=blobdiff_plain;f=src%2Fio%2Fbyteio.rs;h=966169cdefa00756767c79a500cd64ef70499064;hb=b74ff9fac35d41737d71d97227fad233aa4a4b49;hp=9fee3bda0e6c7f101349d689b833737885c02f08;hpb=d532000c8949263f00e6755c6390b3a65eb81b5c;p=nihav.git diff --git a/src/io/byteio.rs b/src/io/byteio.rs index 9fee3bd..966169c 100644 --- a/src/io/byteio.rs +++ b/src/io/byteio.rs @@ -13,7 +13,7 @@ pub enum ByteIOError { SeekError, } -type ByteIOResult = Result; +pub type ByteIOResult = Result; pub trait ByteIO { fn read_buf(&mut self, buf: &mut [u8]) -> ByteIOResult; @@ -48,7 +48,7 @@ pub struct FileReader<'a> { macro_rules! read_int { ($s: ident, $inttype: ty, $size: expr, $which: ident) => ({ let mut buf = [0; $size]; - try!($s.read_buf(&mut buf)); + $s.read_buf(&mut buf)?; unsafe { Ok((*(buf.as_ptr() as *const $inttype)).$which()) } @@ -58,7 +58,7 @@ macro_rules! read_int { macro_rules! peek_int { ($s: ident, $inttype: ty, $size: expr, $which: ident) => ({ let mut buf = [0; $size]; - try!($s.peek_buf(&mut buf)); + $s.peek_buf(&mut buf)?; unsafe { Ok((*(buf.as_ptr() as *const $inttype)).$which()) } @@ -166,7 +166,7 @@ impl<'a> ByteReader<'a> { } else { let mut ssize = len; let mut buf : [u8; 16] = [0; 16]; - let mut bref = &mut buf; + let bref = &mut buf; while ssize > bref.len() { self.io.read_buf(bref)?; ssize -= bref.len(); @@ -190,6 +190,16 @@ impl<'a> ByteReader<'a> { pub fn is_eof(&mut self) -> bool { self.io.is_eof() } + + pub fn size(&mut self) -> i64 { + self.io.size() + } + + pub fn left(&mut self) -> i64 { + let size = self.io.size(); + if size == -1 { return -1; } + return size - (self.io.tell() as i64) + } } impl<'a> MemoryReader<'a> { @@ -360,8 +370,8 @@ pub struct MemoryWriter<'a> { pos: usize, } -pub struct FileWriter<'a> { - file: &'a File, +pub struct FileWriter { + file: File, } impl<'a> ByteWriter<'a> { @@ -509,13 +519,13 @@ impl<'a> ByteIO for MemoryWriter<'a> { } } -impl<'a> FileWriter<'a> { - pub fn new_write(file: &'a mut File) -> Self { +impl FileWriter { + pub fn new_write(file: File) -> Self { FileWriter { file: file } } } -impl<'a> ByteIO for FileWriter<'a> { +impl ByteIO for FileWriter { #[allow(unused_variables)] fn read_byte(&mut self) -> ByteIOResult { Err(ByteIOError::NotImplemented)