Skip to content

Commit cfab9cc

Browse files
committed
Rollup merge of rust-lang#22428 - erickt:io-wrappers, r=aturon
Also includes some minor optimizations to the Vec and slice writers to remove the unnecessary loop.
2 parents 5676f60 + ded93b1 commit cfab9cc

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/libstd/io/impls.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,47 @@ use core::prelude::*;
1212

1313
use boxed::Box;
1414
use cmp;
15-
use io::{self, SeekFrom, Read, Write, Seek, BufRead};
15+
use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind};
16+
use fmt;
1617
use mem;
1718
use slice;
19+
use string::String;
1820
use vec::Vec;
1921

2022
// =============================================================================
2123
// Forwarding implementations
2224

2325
impl<'a, R: Read + ?Sized> Read for &'a mut R {
2426
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { (**self).read(buf) }
27+
28+
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<()> { (**self).read_to_end(buf) }
29+
30+
fn read_to_string(&mut self, buf: &mut String) -> io::Result<()> {
31+
(**self).read_to_string(buf)
32+
}
2533
}
2634
impl<'a, W: Write + ?Sized> Write for &'a mut W {
2735
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { (**self).write(buf) }
36+
37+
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { (**self).write_all(buf) }
38+
39+
fn write_fmt(&mut self, fmt: fmt::Arguments) -> io::Result<()> { (**self).write_fmt(fmt) }
40+
2841
fn flush(&mut self) -> io::Result<()> { (**self).flush() }
2942
}
3043
impl<'a, S: Seek + ?Sized> Seek for &'a mut S {
3144
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { (**self).seek(pos) }
3245
}
3346
impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B {
3447
fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() }
48+
3549
fn consume(&mut self, amt: usize) { (**self).consume(amt) }
50+
51+
fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<()> {
52+
(**self).read_until(byte, buf)
53+
}
54+
55+
fn read_line(&mut self, buf: &mut String) -> io::Result<()> { (**self).read_line(buf) }
3656
}
3757

3858
impl<R: Read + ?Sized> Read for Box<R> {
@@ -76,6 +96,15 @@ impl<'a> Write for &'a mut [u8] {
7696
*self = b;
7797
Ok(amt)
7898
}
99+
100+
fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
101+
if try!(self.write(data)) == data.len() {
102+
Ok(())
103+
} else {
104+
Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer", None))
105+
}
106+
}
107+
79108
fn flush(&mut self) -> io::Result<()> { Ok(()) }
80109
}
81110

@@ -84,5 +113,11 @@ impl Write for Vec<u8> {
84113
self.push_all(buf);
85114
Ok(buf.len())
86115
}
116+
117+
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
118+
try!(self.write(buf));
119+
Ok(())
120+
}
121+
87122
fn flush(&mut self) -> io::Result<()> { Ok(()) }
88123
}

0 commit comments

Comments
 (0)