Skip to content

Commit 7fab9cb

Browse files
committed
bufwriter::WriterPanicked: Provide panicking example
Signed-off-by: Ian Jackson <[email protected]>
1 parent 3817631 commit 7fab9cb

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Diff for: library/std/src/io/buffered/bufwriter.rs

+24
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,30 @@ impl<W: Write> BufWriter<W> {
324324
#[unstable(feature = "bufwriter_into_raw_parts", issue = "none")]
325325
/// Error returned for the buffered data from `BufWriter::into_raw_parts`, when the underlying
326326
/// writer has previously panicked. Contains the (possibly partly written) buffered data.
327+
///
328+
/// # Example
329+
///
330+
/// ```
331+
/// #![feature(bufwriter_into_raw_parts)]
332+
/// use std::io::{self, BufWriter, Write};
333+
/// use std::panic::{catch_unwind, AssertUnwindSafe};
334+
///
335+
/// struct PanickingWriter;
336+
/// impl Write for PanickingWriter {
337+
/// fn write(&mut self, buf: &[u8]) -> io::Result<usize> { panic!() }
338+
/// fn flush(&mut self) -> io::Result<()> { panic!() }
339+
/// }
340+
///
341+
/// let mut stream = BufWriter::new(PanickingWriter);
342+
/// write!(stream, "some data").unwrap();
343+
/// let result = catch_unwind(AssertUnwindSafe(|| {
344+
/// stream.flush().unwrap()
345+
/// }));
346+
/// assert!(result.is_err());
347+
/// let (recovered_writer, buffered_data) = stream.into_raw_parts();
348+
/// assert!(matches!(recovered_writer, PanickingWriter));
349+
/// assert_eq!(buffered_data.unwrap_err().into_inner(), b"some data");
350+
/// ```
327351
pub struct WriterPanicked {
328352
buf: Vec<u8>,
329353
}

0 commit comments

Comments
 (0)