Implement read_exact for the Read trait

This implements the proposed "read_exact" RFC
(https://github.com/rust-lang/rfcs/pull/980).
This commit is contained in:
Cesar Eduardo Barros
2015-07-20 00:23:37 -03:00
parent ef04b07239
commit ff81920f03
4 changed files with 140 additions and 0 deletions

View File

@@ -38,6 +38,11 @@ impl<'a, R: Read + ?Sized> Read for &'a mut R {
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
(**self).read_to_string(buf)
}
#[inline]
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
(**self).read_exact(buf)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, W: Write + ?Sized> Write for &'a mut W {
@@ -97,6 +102,11 @@ impl<R: Read + ?Sized> Read for Box<R> {
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
(**self).read_to_string(buf)
}
#[inline]
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
(**self).read_exact(buf)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<W: Write + ?Sized> Write for Box<W> {
@@ -153,6 +163,17 @@ impl<'a> Read for &'a [u8] {
*self = b;
Ok(amt)
}
#[inline]
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
if buf.len() > self.len() {
return Err(Error::new(ErrorKind::UnexpectedEOF, "failed to fill whole buffer"));
}
let (a, b) = self.split_at(buf.len());
slice::bytes::copy_memory(a, buf);
*self = b;
Ok(())
}
}
#[stable(feature = "rust1", since = "1.0.0")]