Skip to content

Commit d2aee95

Browse files
committed
Expand documentation for IntoInnerError
Mostly adding examples.
1 parent 4e51763 commit d2aee95

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
@@ -269,6 +269,27 @@ pub struct BufWriter<W: Write> {
269269
/// An error returned by `into_inner` which combines an error that
270270
/// happened while writing out the buffer, and the buffered writer object
271271
/// which may be used to recover from the condition.
272+
///
273+
/// # Examples
274+
///
275+
/// ```no_run
276+
/// use std::io::BufWriter;
277+
/// use std::net::TcpStream;
278+
///
279+
/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
280+
///
281+
/// // do stuff with the stream
282+
///
283+
/// // we want to get our `TcpStream` back, so let's try:
284+
///
285+
/// let stream = match stream.into_inner() {
286+
/// Ok(s) => s,
287+
/// Err(e) => {
288+
/// // Here, e is an IntoInnerError
289+
/// panic!("An error occurred");
290+
/// }
291+
/// };
292+
/// ```
272293
#[derive(Debug)]
273294
#[stable(feature = "rust1", since = "1.0.0")]
274295
pub struct IntoInnerError<W>(W, Error);
@@ -384,16 +405,67 @@ impl<W: Write> Drop for BufWriter<W> {
384405
}
385406

386407
impl<W> IntoInnerError<W> {
387-
/// Returns the error which caused the call to `into_inner` to fail.
408+
/// Returns the error which caused the call to `into_inner()` to fail.
388409
///
389410
/// This error was returned when attempting to write the internal buffer.
411+
///
412+
/// # Examples
413+
///
414+
/// ```no_run
415+
/// use std::io::BufWriter;
416+
/// use std::net::TcpStream;
417+
///
418+
/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
419+
///
420+
/// // do stuff with the stream
421+
///
422+
/// // we want to get our `TcpStream` back, so let's try:
423+
///
424+
/// let stream = match stream.into_inner() {
425+
/// Ok(s) => s,
426+
/// Err(e) => {
427+
/// // Here, e is an IntoInnerError, let's log the inner error.
428+
/// //
429+
/// // We'll just 'log' to stdout for this example.
430+
/// println!("{}", e.error());
431+
///
432+
/// panic!("An unexpected error occurred.");
433+
/// }
434+
/// };
435+
/// ```
390436
#[stable(feature = "rust1", since = "1.0.0")]
391437
pub fn error(&self) -> &Error { &self.1 }
392438

393439
/// Returns the buffered writer instance which generated the error.
394440
///
395441
/// The returned object can be used for error recovery, such as
396442
/// re-inspecting the buffer.
443+
///
444+
/// # Examples
445+
///
446+
/// ```no_run
447+
/// use std::io::BufWriter;
448+
/// use std::net::TcpStream;
449+
///
450+
/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
451+
///
452+
/// // do stuff with the stream
453+
///
454+
/// // we want to get our `TcpStream` back, so let's try:
455+
///
456+
/// let stream = match stream.into_inner() {
457+
/// Ok(s) => s,
458+
/// Err(e) => {
459+
/// // Here, e is a IntoInnerError, let's re-examine the buffer:
460+
/// let buffer = e.into_inner();
461+
///
462+
/// // do stuff to try to recover
463+
///
464+
/// // afterwards, let's just return the stream
465+
/// buffer.into_inner().unwrap()
466+
/// }
467+
/// };
468+
/// ```
397469
#[stable(feature = "rust1", since = "1.0.0")]
398470
pub fn into_inner(self) -> W { self.0 }
399471
}

0 commit comments

Comments
 (0)