Skip to content

std: Expose get_mut() on I/O wrappers #14785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions src/libstd/io/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ impl<R: Reader> BufferedReader<R> {
}

/// 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.
Expand Down Expand Up @@ -174,6 +179,14 @@ impl<W: Writer> BufferedWriter<W> {
/// 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.
Expand Down Expand Up @@ -238,6 +251,14 @@ impl<W: Writer> LineBufferedWriter<W> {
/// 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.
Expand Down Expand Up @@ -334,6 +355,17 @@ impl<S: Stream> BufferedStream<S> {
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
Expand Down