Skip to content

Commit 5e3cfa3

Browse files
committed
Rollup merge of rust-lang#27170 - steveklabnik:doc_std_io_intoinnererror, r=alexcrichton
Mostly adding examples. r? @alexcrichton
2 parents 192523c + d2aee95 commit 5e3cfa3

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

src/libstd/io/buffered.rs

+73-1
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,27 @@ pub struct BufWriter<W: Write> {
304304
/// An error returned by `into_inner` which combines an error that
305305
/// happened while writing out the buffer, and the buffered writer object
306306
/// which may be used to recover from the condition.
307+
///
308+
/// # Examples
309+
///
310+
/// ```no_run
311+
/// use std::io::BufWriter;
312+
/// use std::net::TcpStream;
313+
///
314+
/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
315+
///
316+
/// // do stuff with the stream
317+
///
318+
/// // we want to get our `TcpStream` back, so let's try:
319+
///
320+
/// let stream = match stream.into_inner() {
321+
/// Ok(s) => s,
322+
/// Err(e) => {
323+
/// // Here, e is an IntoInnerError
324+
/// panic!("An error occurred");
325+
/// }
326+
/// };
327+
/// ```
307328
#[derive(Debug)]
308329
#[stable(feature = "rust1", since = "1.0.0")]
309330
pub struct IntoInnerError<W>(W, Error);
@@ -477,16 +498,67 @@ impl<W: Write> Drop for BufWriter<W> {
477498
}
478499

479500
impl<W> IntoInnerError<W> {
480-
/// Returns the error which caused the call to `into_inner` to fail.
501+
/// Returns the error which caused the call to `into_inner()` to fail.
481502
///
482503
/// This error was returned when attempting to write the internal buffer.
504+
///
505+
/// # Examples
506+
///
507+
/// ```no_run
508+
/// use std::io::BufWriter;
509+
/// use std::net::TcpStream;
510+
///
511+
/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
512+
///
513+
/// // do stuff with the stream
514+
///
515+
/// // we want to get our `TcpStream` back, so let's try:
516+
///
517+
/// let stream = match stream.into_inner() {
518+
/// Ok(s) => s,
519+
/// Err(e) => {
520+
/// // Here, e is an IntoInnerError, let's log the inner error.
521+
/// //
522+
/// // We'll just 'log' to stdout for this example.
523+
/// println!("{}", e.error());
524+
///
525+
/// panic!("An unexpected error occurred.");
526+
/// }
527+
/// };
528+
/// ```
483529
#[stable(feature = "rust1", since = "1.0.0")]
484530
pub fn error(&self) -> &Error { &self.1 }
485531

486532
/// Returns the buffered writer instance which generated the error.
487533
///
488534
/// The returned object can be used for error recovery, such as
489535
/// re-inspecting the buffer.
536+
///
537+
/// # Examples
538+
///
539+
/// ```no_run
540+
/// use std::io::BufWriter;
541+
/// use std::net::TcpStream;
542+
///
543+
/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
544+
///
545+
/// // do stuff with the stream
546+
///
547+
/// // we want to get our `TcpStream` back, so let's try:
548+
///
549+
/// let stream = match stream.into_inner() {
550+
/// Ok(s) => s,
551+
/// Err(e) => {
552+
/// // Here, e is a IntoInnerError, let's re-examine the buffer:
553+
/// let buffer = e.into_inner();
554+
///
555+
/// // do stuff to try to recover
556+
///
557+
/// // afterwards, let's just return the stream
558+
/// buffer.into_inner().unwrap()
559+
/// }
560+
/// };
561+
/// ```
490562
#[stable(feature = "rust1", since = "1.0.0")]
491563
pub fn into_inner(self) -> W { self.0 }
492564
}

0 commit comments

Comments
 (0)