@@ -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 ) ;
@@ -476,16 +497,67 @@ impl<W: Write> Drop for BufWriter<W> {
476
497
}
477
498
478
499
impl < W > IntoInnerError < W > {
479
- /// Returns the error which caused the call to `into_inner` to fail.
500
+ /// Returns the error which caused the call to `into_inner() ` to fail.
480
501
///
481
502
/// This error was returned when attempting to write the internal buffer.
503
+ ///
504
+ /// # Examples
505
+ ///
506
+ /// ```no_run
507
+ /// use std::io::BufWriter;
508
+ /// use std::net::TcpStream;
509
+ ///
510
+ /// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
511
+ ///
512
+ /// // do stuff with the stream
513
+ ///
514
+ /// // we want to get our `TcpStream` back, so let's try:
515
+ ///
516
+ /// let stream = match stream.into_inner() {
517
+ /// Ok(s) => s,
518
+ /// Err(e) => {
519
+ /// // Here, e is an IntoInnerError, let's log the inner error.
520
+ /// //
521
+ /// // We'll just 'log' to stdout for this example.
522
+ /// println!("{}", e.error());
523
+ ///
524
+ /// panic!("An unexpected error occurred.");
525
+ /// }
526
+ /// };
527
+ /// ```
482
528
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
483
529
pub fn error ( & self ) -> & Error { & self . 1 }
484
530
485
531
/// Returns the buffered writer instance which generated the error.
486
532
///
487
533
/// The returned object can be used for error recovery, such as
488
534
/// re-inspecting the buffer.
535
+ ///
536
+ /// # Examples
537
+ ///
538
+ /// ```no_run
539
+ /// use std::io::BufWriter;
540
+ /// use std::net::TcpStream;
541
+ ///
542
+ /// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
543
+ ///
544
+ /// // do stuff with the stream
545
+ ///
546
+ /// // we want to get our `TcpStream` back, so let's try:
547
+ ///
548
+ /// let stream = match stream.into_inner() {
549
+ /// Ok(s) => s,
550
+ /// Err(e) => {
551
+ /// // Here, e is a IntoInnerError, let's re-examine the buffer:
552
+ /// let buffer = e.into_inner();
553
+ ///
554
+ /// // do stuff to try to recover
555
+ ///
556
+ /// // afterwards, let's just return the stream
557
+ /// buffer.into_inner().unwrap()
558
+ /// }
559
+ /// };
560
+ /// ```
489
561
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
490
562
pub fn into_inner ( self ) -> W { self . 0 }
491
563
}
0 commit comments