about summary refs log tree commit diff
path: root/src/libstd/io/impls.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-08-30 05:59:49 +0000
committerbors <bors@rust-lang.org>2015-08-30 05:59:49 +0000
commit4bb90232daab451cf58359e0c5874bc905d2f101 (patch)
tree3c58214650cd1c8c3cee6575d6ac2cb59cbdcf2e /src/libstd/io/impls.rs
parent2f749254cafd4940243548730afc02c5410611bf (diff)
parent73e7a7269553f661501d54c1fe8c57aa12b126fa (diff)
downloadrust-4bb90232daab451cf58359e0c5874bc905d2f101.tar.gz
rust-4bb90232daab451cf58359e0c5874bc905d2f101.zip
Auto merge of #27588 - cesarb:read_all, r=alexcrichton
This implements the proposed "read_exact" RFC (https://github.com/rust-lang/rfcs/pull/980).

Tracking issue: https://github.com/rust-lang/rust/issues/27585
Diffstat (limited to 'src/libstd/io/impls.rs')
-rw-r--r--src/libstd/io/impls.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/libstd/io/impls.rs b/src/libstd/io/impls.rs
index a5c8ba335c1..79013000fe3 100644
--- a/src/libstd/io/impls.rs
+++ b/src/libstd/io/impls.rs
@@ -36,6 +36,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 {
@@ -95,6 +100,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> {
@@ -151,6 +161,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")]