Some more cleanup

This commit is contained in:
Mathijs van de Nes 2015-02-24 18:46:01 +01:00
parent 6d2a63e406
commit f382cf85b9
3 changed files with 4 additions and 45 deletions

View file

@ -5,12 +5,11 @@ use std::cell::RefCell;
pub struct IoConverter<T> { pub struct IoConverter<T> {
inner: RefCell<T>, inner: RefCell<T>,
eofs: usize,
} }
impl<T> IoConverter<T> { impl<T> IoConverter<T> {
pub fn new(inner: T) -> IoConverter<T> { pub fn new(inner: T) -> IoConverter<T> {
IoConverter { inner: RefCell::new(inner), eofs: 0, } IoConverter { inner: RefCell::new(inner), }
} }
pub fn into_inner(self) -> T { pub fn into_inner(self) -> T {
self.inner.into_inner() self.inner.into_inner()
@ -41,27 +40,7 @@ impl<W: Writer> Write for IoConverter<W> {
} }
} }
impl<W: io::Seek> old_io::Seek for IoConverter<W> { impl<R: Reader> Read for IoConverter<R> {
fn tell(&self) -> old_io::IoResult<u64> {
match self.inner.borrow_mut().seek(io::SeekFrom::Current(0)) {
Ok(v) => Ok(v),
Err(..) => Err(old_io::standard_error(old_io::OtherIoError)),
}
}
fn seek(&mut self, pos: i64, style: old_io::SeekStyle) -> old_io::IoResult<()> {
let new_pos = match style {
old_io::SeekSet => io::SeekFrom::Start(pos as u64),
old_io::SeekEnd => io::SeekFrom::End(pos),
old_io::SeekCur => io::SeekFrom::Current(pos),
};
match self.inner.borrow_mut().seek(new_pos) {
Ok(..) => Ok(()),
Err(..) => Err(old_io::standard_error(old_io::OtherIoError)),
}
}
}
impl<R: old_io::Reader> Read for IoConverter<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self.inner.borrow_mut().read(buf) { match self.inner.borrow_mut().read(buf) {
Ok(v) => Ok(v), Ok(v) => Ok(v),
@ -74,14 +53,7 @@ impl<R: old_io::Reader> Read for IoConverter<R> {
impl<R: Read> Reader for IoConverter<R> { impl<R: Read> Reader for IoConverter<R> {
fn read(&mut self, buf: &mut [u8]) -> old_io::IoResult<usize> { fn read(&mut self, buf: &mut [u8]) -> old_io::IoResult<usize> {
match self.inner.borrow_mut().read(buf) { match self.inner.borrow_mut().read(buf) {
Ok(0) => { Ok(0) if buf.len() > 0 => Err(old_io::standard_error(old_io::EndOfFile)),
if self.eofs >= 2 {
Err(old_io::standard_error(old_io::EndOfFile))
} else {
self.eofs += 1;
Ok(0)
}
},
Ok(v) => Ok(v), Ok(v) => Ok(v),
Err(..) => Err(old_io::standard_error(old_io::OtherIoError)), Err(..) => Err(old_io::standard_error(old_io::OtherIoError)),
} }

View file

@ -63,14 +63,6 @@ impl<'a, R: Read> Read for RefMutReader<'a, R>
} }
} }
impl<'a, R: Reader> Reader for RefMutReader<'a, R>
{
fn read(&mut self, buf: &mut [u8]) -> ::std::old_io::IoResult<usize>
{
self.inner.read(buf)
}
}
/// Additional integer write methods for a io::Read /// Additional integer write methods for a io::Read
pub trait WriteIntExt { pub trait WriteIntExt {
/// Write a u32 in little-endian mode /// Write a u32 in little-endian mode
@ -110,16 +102,12 @@ pub trait ReadIntExt {
fn fill_exact<R: Read>(reader: &mut R, buf: &mut [u8]) -> io::Result<()> { fn fill_exact<R: Read>(reader: &mut R, buf: &mut [u8]) -> io::Result<()> {
let mut idx = 0; let mut idx = 0;
let mut tries = 0;
while idx < buf.len() { while idx < buf.len() {
match reader.read(&mut buf[idx..]) { match reader.read(&mut buf[idx..]) {
Err(v) => return Err(v), Err(v) => return Err(v),
Ok(0) => return Err(io::Error::new(io::ErrorKind::ResourceUnavailable, "Could not fill the buffer", None)),
Ok(i) => idx += i, Ok(i) => idx += i,
} }
tries += 1;
if tries > 2*buf.len() {
return Err(io::Error::new(io::ErrorKind::ResourceUnavailable, "Could not fill the buffer", None));
}
} }
Ok(()) Ok(())
} }

View file

@ -7,7 +7,6 @@ use result::{ZipResult, ZipError};
use std::default::Default; use std::default::Default;
use std::io; use std::io;
use std::io::prelude::*; use std::io::prelude::*;
use std::old_io::Writer;
use std::mem; use std::mem;
use time; use time;
use flate2; use flate2;