@@ -304,6 +304,27 @@ pub struct BufWriter<W: Write> {
304
304
/// An error returned by `into_inner` which combines an error that
305
305
/// happened while writing out the buffer, and the buffered writer object
306
306
/// 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
+ /// ```
307
328
#[ derive( Debug ) ]
308
329
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
309
330
pub struct IntoInnerError < W > ( W , Error ) ;
@@ -477,16 +498,67 @@ impl<W: Write> Drop for BufWriter<W> {
477
498
}
478
499
479
500
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.
481
502
///
482
503
/// 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
+ /// ```
483
529
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
484
530
pub fn error ( & self ) -> & Error { & self . 1 }
485
531
486
532
/// Returns the buffered writer instance which generated the error.
487
533
///
488
534
/// The returned object can be used for error recovery, such as
489
535
/// 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
+ /// ```
490
562
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
491
563
pub fn into_inner ( self ) -> W { self . 0 }
492
564
}
0 commit comments