From fba00d2f2637955e430e8a8f77329dbfefcbb882 Mon Sep 17 00:00:00 2001 From: Christoph Burgdorf Date: Fri, 13 Mar 2015 13:10:13 +0100 Subject: [PATCH] implements read_into_string/read_into_vec for Read trait This implements RFC0841 --- src/libstd/io/mod.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 9137068076b46..9749d549fd43e 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -213,6 +213,15 @@ pub trait Read { read_to_end(self, buf) } + /// Read all bytes until EOF in this source, returning them as a new `Vec`. + /// + /// See `read_to_end` for other semantics. + fn read_into_vec(&mut self) -> Result> { + let mut buf = Vec::new(); + let res = self.read_to_end(&mut buf); + res.map(|_| buf) + } + /// Read all bytes until EOF in this source, placing them into `buf`. /// /// # Errors @@ -233,6 +242,15 @@ pub trait Read { // know is guaranteed to only read data into the end of the buffer. append_to_string(buf, |b| read_to_end(self, b)) } + + /// Read all bytes until EOF in this source, returning them as a new buffer. + /// + /// See `read_to_string` for other semantics. + fn read_into_string(&mut self, buf: &mut String) -> Result { + let mut buf = String::new(); + let res = self.read_to_string(&mut buf); + res.map(|_| buf) + } } /// Extension methods for all instances of `Read`, typically imported through