@@ -269,6 +269,27 @@ pub struct BufWriter<W: Write> {
269
269
/// An error returned by `into_inner` which combines an error that
270
270
/// happened while writing out the buffer, and the buffered writer object
271
271
/// 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
+ /// ```
272
293
#[ derive( Debug ) ]
273
294
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
274
295
pub struct IntoInnerError < W > ( W , Error ) ;
@@ -384,16 +405,67 @@ impl<W: Write> Drop for BufWriter<W> {
384
405
}
385
406
386
407
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.
388
409
///
389
410
/// 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
+ /// ```
390
436
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
391
437
pub fn error ( & self ) -> & Error { & self . 1 }
392
438
393
439
/// Returns the buffered writer instance which generated the error.
394
440
///
395
441
/// The returned object can be used for error recovery, such as
396
442
/// 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
+ /// ```
397
469
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
398
470
pub fn into_inner ( self ) -> W { self . 0 }
399
471
}
0 commit comments