Remove RefCell from IoConverter

Seek is removed, so dynamic borrow checking is no longer needed
This commit is contained in:
Mathijs van de Nes 2015-02-24 18:50:33 +01:00
parent 518b40aabc
commit 51c55d30b3

View file

@ -1,24 +1,23 @@
use std::io;
use std::io::prelude::*;
use std::old_io;
use std::cell::RefCell;
pub struct IoConverter<T> {
inner: RefCell<T>,
inner: T,
}
impl<T> IoConverter<T> {
pub fn new(inner: T) -> IoConverter<T> {
IoConverter { inner: RefCell::new(inner), }
IoConverter { inner: inner, }
}
pub fn into_inner(self) -> T {
self.inner.into_inner()
self.inner
}
}
impl<W: Write> Writer for IoConverter<W> {
fn write_all(&mut self, buf: &[u8]) -> old_io::IoResult<()> {
match self.inner.borrow_mut().write_all(buf) {
match self.inner.write_all(buf) {
Ok(()) => Ok(()),
Err(..) => Err(old_io::standard_error(old_io::OtherIoError)),
}
@ -27,13 +26,13 @@ impl<W: Write> Writer for IoConverter<W> {
impl<W: Writer> Write for IoConverter<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
match self.inner.borrow_mut().write_all(buf) {
match self.inner.write_all(buf) {
Ok(()) => Ok(buf.len()),
Err(..) => Err(io::Error::new(io::ErrorKind::Other, "Some writing error", None)),
}
}
fn flush(&mut self) -> io::Result<()> {
match self.inner.borrow_mut().flush() {
match self.inner.flush() {
Ok(()) => Ok(()),
Err(..) => Err(io::Error::new(io::ErrorKind::Other, "Some flushing error", None)),
}
@ -42,7 +41,7 @@ impl<W: Writer> Write for IoConverter<W> {
impl<R: Reader> Read for IoConverter<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self.inner.borrow_mut().read(buf) {
match self.inner.read(buf) {
Ok(v) => Ok(v),
Err(ref e) if e.kind == old_io::EndOfFile => Ok(0),
Err(..) => Err(io::Error::new(io::ErrorKind::Other, "Some reading error", None)),
@ -52,7 +51,7 @@ impl<R: Reader> Read for IoConverter<R> {
impl<R: Read> Reader for IoConverter<R> {
fn read(&mut self, buf: &mut [u8]) -> old_io::IoResult<usize> {
match self.inner.borrow_mut().read(buf) {
match self.inner.read(buf) {
Ok(0) if buf.len() > 0 => Err(old_io::standard_error(old_io::EndOfFile)),
Ok(v) => Ok(v),
Err(..) => Err(old_io::standard_error(old_io::OtherIoError)),