diff --git a/src/libstd/io/buffered.rs b/src/libstd/io/buffered.rs index 9450f7798edcf..ca72b68c7b9f8 100644 --- a/src/libstd/io/buffered.rs +++ b/src/libstd/io/buffered.rs @@ -74,11 +74,16 @@ impl BufferedReader { } /// Gets a reference to the underlying reader. - /// - /// This type does not expose the ability to get a mutable reference to the - /// underlying reader because that could possibly corrupt the buffer. pub fn get_ref<'a>(&'a self) -> &'a R { &self.inner } + /// Gets a mutable reference to the underlying reader. + /// + /// Note that reading data directly from the contained object is not + /// recommended, and could corrupt the data coming out of this reader. This + /// method is provided to allow invoking other `&mut self` methods on the + /// contained object without having to unwrap the buffered reader. + pub fn get_mut<'a>(&'a mut self) -> &'a mut R { &mut self.inner } + /// Unwraps this `BufferedReader`, returning the underlying reader. /// /// Note that any leftover data in the internal buffer is lost. @@ -174,6 +179,14 @@ impl BufferedWriter { /// underlying reader because that could possibly corrupt the buffer. pub fn get_ref<'a>(&'a self) -> &'a W { self.inner.get_ref() } + /// Gets a mutable reference to the underlying writer. + /// + /// Note that writing data directly from the contained object is not + /// recommended, and could corrupt the data going out of this writer. This + /// method is provided to allow invoking other `&mut self` methods on the + /// contained object without having to unwrap the buffered writer. + pub fn get_mut<'a>(&'a mut self) -> &'a mut W { self.inner.get_mut_ref() } + /// Unwraps this `BufferedWriter`, returning the underlying writer. /// /// The buffer is flushed before returning the writer. @@ -238,6 +251,14 @@ impl LineBufferedWriter { /// underlying reader because that could possibly corrupt the buffer. pub fn get_ref<'a>(&'a self) -> &'a W { self.inner.get_ref() } + /// Gets a mutable reference to the underlying writer. + /// + /// Note that writing data directly from the contained object is not + /// recommended, and could corrupt the data going out of this writer. This + /// method is provided to allow invoking other `&mut self` methods on the + /// contained object without having to unwrap the buffered writer. + pub fn get_mut<'a>(&'a mut self) -> &'a mut W { self.inner.get_mut() } + /// Unwraps this `LineBufferedWriter`, returning the underlying writer. /// /// The internal buffer is flushed before returning the writer. @@ -334,6 +355,17 @@ impl BufferedStream { w.get_ref() } + /// Gets a mutable reference to the underlying stream. + /// + /// Note that writing or reading data directly from or to the contained + /// object is not recommended, and could corrupt the data of this stream. + /// This method is provided to allow invoking other `&mut self` methods on + /// the contained object without having to unwrap the buffered stream. + pub fn get_mut<'a>(&'a mut self) -> &'a mut S { + let InternalBufferedWriter(ref mut w) = self.inner.inner; + w.get_mut() + } + /// Unwraps this `BufferedStream`, returning the underlying stream. /// /// The internal buffer is flushed before returning the stream. Any leftover