pub struct MemoryReader<'a> {
buf: &'a [u8],
- size: usize,
pos: usize,
}
impl<'a> MemoryReader<'a> {
pub fn new_read(buf: &'a [u8]) -> Self {
- MemoryReader { buf, size: buf.len(), pos: 0 }
+ MemoryReader { buf, pos: 0 }
}
fn real_seek(&mut self, pos: i64) -> ByteIOResult<u64> {
- if pos < 0 || (pos as usize) > self.size {
+ if pos < 0 || (pos as usize) > self.buf.len() {
return Err(ByteIOError::WrongRange);
}
self.pos = pos as usize;
}
fn peek_buf(&mut self, buf: &mut [u8]) -> ByteIOResult<usize> {
- let copy_size = if self.size - self.pos < buf.len() { self.size } else { buf.len() };
+ let copy_size = if self.buf.len() - self.pos < buf.len() { self.buf.len() } else { buf.len() };
if copy_size == 0 { return Err(ByteIOError::EOF); }
let dst = &mut buf[0..copy_size];
dst.copy_from_slice(&self.buf[self.pos..][..copy_size]);
}
fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
- let cur_pos = self.pos as i64;
- let cur_size = self.size as i64;
+ 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),
}
fn is_eof(&self) -> bool {
- self.pos >= self.size
+ self.pos >= self.buf.len()
}
fn is_seekable(&mut self) -> bool {
pub struct MemoryWriter<'a> {
buf: &'a mut [u8],
- size: usize,
pos: usize,
}
impl<'a> MemoryWriter<'a> {
pub fn new_write(buf: &'a mut [u8]) -> Self {
- let len = buf.len();
- MemoryWriter { buf, size: len, pos: 0 }
+ MemoryWriter { buf, pos: 0 }
}
fn real_seek(&mut self, pos: i64) -> ByteIOResult<u64> {
- if pos < 0 || (pos as usize) > self.size {
+ if pos < 0 || (pos as usize) > self.buf.len() {
return Err(ByteIOError::WrongRange)
}
self.pos = pos as usize;
}
fn write_buf(&mut self, buf: &[u8]) -> ByteIOResult<()> {
- if self.pos + buf.len() > self.size { return Err(ByteIOError::WriteError); }
+ 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];
}
}
fn seek(&mut self, pos: SeekFrom) -> ByteIOResult<u64> {
- let cur_pos = self.pos as i64;
- let cur_size = self.size as i64;
+ 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),
}
fn is_eof(&self) -> bool {
- self.pos >= self.size
+ self.pos >= self.buf.len()
}
fn is_seekable(&mut self) -> bool {