Skip to content

Commit 945b4ed

Browse files
committed
Allow mutable access to wrapped internal type in Buffered*
This is necessary to e.g. set a timeout on the underlying stream.
1 parent 0e06f71 commit 945b4ed

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

src/libstd/io/buffered.rs

+26-10
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,14 @@ impl<R: Reader> BufferedReader<R> {
7575
}
7676

7777
/// Gets a reference to the underlying reader.
78+
pub fn get_ref<'a>(&self) -> &R { &self.inner }
79+
80+
/// Gets a mutable reference to the underlying reader.
7881
///
79-
/// This type does not expose the ability to get a mutable reference to the
80-
/// underlying reader because that could possibly corrupt the buffer.
81-
pub fn get_ref<'a>(&'a self) -> &'a R { &self.inner }
82+
/// ## Warning
83+
///
84+
/// It is inadvisable to directly read from the underlying reader.
85+
pub fn get_mut(&mut self) -> &mut R { &mut self.inner }
8286

8387
/// Unwraps this `BufferedReader`, returning the underlying reader.
8488
///
@@ -176,10 +180,14 @@ impl<W: Writer> BufferedWriter<W> {
176180
}
177181

178182
/// Gets a reference to the underlying writer.
183+
pub fn get_ref(&self) -> &W { self.inner.as_ref().unwrap() }
184+
185+
/// Gets a mutable reference to the underlying write.
179186
///
180-
/// This type does not expose the ability to get a mutable reference to the
181-
/// underlying reader because that could possibly corrupt the buffer.
182-
pub fn get_ref<'a>(&'a self) -> &'a W { self.inner.as_ref().unwrap() }
187+
/// ## Warning
188+
///
189+
/// It is inadvisable to directly read from the underlying writer.
190+
pub fn get_mut(&mut self) -> &mut W { self.inner.as_mut().unwrap() }
183191

184192
/// Unwraps this `BufferedWriter`, returning the underlying writer.
185193
///
@@ -341,14 +349,22 @@ impl<S: Stream> BufferedStream<S> {
341349
}
342350

343351
/// Gets a reference to the underlying stream.
344-
///
345-
/// This type does not expose the ability to get a mutable reference to the
346-
/// underlying reader because that could possibly corrupt the buffer.
347-
pub fn get_ref<'a>(&'a self) -> &'a S {
352+
pub fn get_ref(&self) -> &S {
348353
let InternalBufferedWriter(ref w) = self.inner.inner;
349354
w.get_ref()
350355
}
351356

357+
/// Gets a mutable reference to the underlying stream.
358+
///
359+
/// ## Warning
360+
///
361+
/// It is inadvisable to read directly from or write directly to the
362+
/// underlying stream.
363+
pub fn get_mut(&mut self) -> &mut S {
364+
let InternalBufferedWriter(ref mut w) = self.inner.inner;
365+
w.get_mut()
366+
}
367+
352368
/// Unwraps this `BufferedStream`, returning the underlying stream.
353369
///
354370
/// The internal buffer is flushed before returning the stream. Any leftover

0 commit comments

Comments
 (0)