use Read;
use Write;
use ExtendFromReader;
use ExtendFromReaderSlow;
use error::ExtendError;
use ReadOverwrite;
use void::Void;
use std::vec::Vec;
use std::io::{Sink, Empty};
use std::io;
impl Write for Vec<u8> {
type WriteError = Void;
type FlushError = Void;
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::WriteError> {
self.extend_from_slice(buf);
Ok(buf.len())
}
fn flush(&mut self) -> Result<(), Self::FlushError> {
Ok(())
}
}
impl ExtendFromReaderSlow for Vec<u8> {
type ExtendError = Void;
fn extend_from_reader_slow<R: Read + ?Sized>(&mut self, reader: &mut R) -> Result<usize, ExtendError<R::ReadError, Self::ExtendError>> {
let begin = self.len();
self.resize(begin + 1024, 0);
match reader.read(&mut self[begin..]) {
Ok(bytes) => {
assert!(bytes <= self.capacity() - begin);
self.resize(begin + bytes, 0);
Ok(bytes)
},
Err(e) => {
self.resize(begin, 0);
Err(ExtendError::ReadErr(e))
},
}
}
}
impl ExtendFromReader for Vec<u8> {
fn extend_from_reader<R: Read + ReadOverwrite + ?Sized>(&mut self, reader: &mut R) -> Result<usize, ExtendError<R::ReadError, Self::ExtendError>> {
self.reserve(1024);
unsafe {
if self.capacity() > self.len() {
let begin = self.len();
let capacity = self.capacity();
self.set_len(capacity);
match reader.read(&mut self[begin..]) {
Ok(bytes) => {
assert!(bytes <= capacity - begin);
self.set_len(begin + bytes);
Ok(bytes)
},
Err(e) => {
self.set_len(begin);
Err(ExtendError::ReadErr(e))
},
}
} else {
self.extend_from_reader_slow(reader)
}
}
}
}
impl Write for Sink {
type WriteError = Void;
type FlushError = Void;
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::WriteError> {
Ok(buf.len())
}
fn flush(&mut self) -> Result<(), Self::FlushError> {
Ok(())
}
}
impl Read for Empty {
type ReadError = Void;
fn read(&mut self, _buf: &mut [u8]) -> Result<usize, Self::ReadError> {
Ok(0)
}
}
pub struct StdRead<R> (R);
impl<R: Read> StdRead<R> {
pub fn new(reader: R) -> Self {
StdRead(reader)
}
pub fn into_inner(self) -> R {
self.0
}
}
impl<E: Into<io::Error>, R: Read<ReadError=E>> io::Read for StdRead<R> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
self.0.read(buf).map_err(Into::into)
}
}
pub struct StdWrite<W> (W);
impl<W: Write> StdWrite<W> {
pub fn new(writer: W) -> Self {
StdWrite(writer)
}
pub fn into_inner(self) -> W {
self.0
}
}
impl<WE: Into<io::Error>, FE: Into<io::Error>, W: Write<WriteError=WE, FlushError=FE>> io::Write for StdWrite<W> {
fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
self.0.write(buf).map_err(Into::into)
}
fn flush(&mut self) -> Result<(), io::Error> {
self.0.flush().map_err(Into::into)
}
}
pub struct StdIo<T> (T);
impl<RE: Into<io::Error>, WE: Into<io::Error>, FE: Into<io::Error>, T: Read<ReadError=RE> + Write<WriteError=WE, FlushError=FE>> StdIo<T> {
pub fn new(io: T) -> Self {
StdIo(io)
}
pub fn into_inner(self) -> T {
self.0
}
}
impl<E: Into<io::Error>, T: Read<ReadError=E>> io::Read for StdIo<T> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
self.0.read(buf).map_err(Into::into)
}
}
impl<WE: Into<io::Error>, FE: Into<io::Error>, T: Write<WriteError=WE, FlushError=FE>> io::Write for StdIo<T> {
fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
self.0.write(buf).map_err(Into::into)
}
fn flush(&mut self) -> Result<(), io::Error> {
self.0.flush().map_err(Into::into)
}
}
pub struct GenioRead<R> (R);
impl<R: io::Read> GenioRead<R> {
pub fn new(reader: R) -> Self {
GenioRead(reader)
}
pub fn into_inner(self) -> R {
self.0
}
}
impl<R: io::Read> Read for GenioRead<R> {
type ReadError = io::Error;
fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
self.0.read(buf)
}
}
pub struct GenioWrite<W> (W);
impl<W: io::Write> GenioWrite<W> {
pub fn new(writer: W) -> Self {
GenioWrite(writer)
}
pub fn into_inner(self) -> W {
self.0
}
}
impl<W: io::Write> Write for GenioWrite<W> {
type WriteError = io::Error;
type FlushError = io::Error;
fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
self.0.write(buf)
}
fn flush(&mut self) -> Result<(), io::Error> {
self.0.flush()
}
}
pub struct GenioIo<T> (T);
impl<T: io::Read + io::Write> GenioIo<T> {
pub fn new(io: T) -> Self {
GenioIo(io)
}
pub fn into_inner(self) -> T {
self.0
}
}
impl<T: io::Read> Read for GenioIo<T> {
type ReadError = io::Error;
fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
self.0.read(buf)
}
}
impl<T: io::Write> Write for GenioIo<T> {
type WriteError = io::Error;
type FlushError = io::Error;
fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
self.0.write(buf)
}
fn flush(&mut self) -> Result<(), io::Error> {
self.0.flush()
}
}