Skip to content

std: Update the std::io adaptors to proxy all methods #22428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/libstd/io/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,47 @@ use core::prelude::*;

use boxed::Box;
use cmp;
use io::{self, SeekFrom, Read, Write, Seek, BufRead};
use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind};
use fmt;
use mem;
use slice;
use string::String;
use vec::Vec;

// =============================================================================
// Forwarding implementations

impl<'a, R: Read + ?Sized> Read for &'a mut R {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { (**self).read(buf) }

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<()> { (**self).read_to_end(buf) }

fn read_to_string(&mut self, buf: &mut String) -> io::Result<()> {
(**self).read_to_string(buf)
}
}
impl<'a, W: Write + ?Sized> Write for &'a mut W {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { (**self).write(buf) }

fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { (**self).write_all(buf) }

fn write_fmt(&mut self, fmt: fmt::Arguments) -> io::Result<()> { (**self).write_fmt(fmt) }

fn flush(&mut self) -> io::Result<()> { (**self).flush() }
}
impl<'a, S: Seek + ?Sized> Seek for &'a mut S {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { (**self).seek(pos) }
}
impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B {
fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() }

fn consume(&mut self, amt: usize) { (**self).consume(amt) }

fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<()> {
(**self).read_until(byte, buf)
}

fn read_line(&mut self, buf: &mut String) -> io::Result<()> { (**self).read_line(buf) }
}

impl<R: Read + ?Sized> Read for Box<R> {
Expand Down Expand Up @@ -76,6 +96,15 @@ impl<'a> Write for &'a mut [u8] {
*self = b;
Ok(amt)
}

fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
if try!(self.write(data)) == data.len() {
Ok(())
} else {
Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer", None))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WriteZero seems like the wrong error for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sfackler: yeah, I agree. I kept the WriteZero because that's the error the Read::write_all supermethod returns. The fact that I'm not doing the loop is more of an implementation detail. Is it worth making a PartialWrite error kind?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is indeed what WriteZero is intended for, but I think PartialWrite is actually a better name for it! cc @alexcrichton

}
}

fn flush(&mut self) -> io::Result<()> { Ok(()) }
}

Expand All @@ -84,5 +113,11 @@ impl Write for Vec<u8> {
self.push_all(buf);
Ok(buf.len())
}

fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
try!(self.write(buf));
Ok(())
}

fn flush(&mut self) -> io::Result<()> { Ok(()) }
}