@@ -55,7 +55,9 @@ pub type Result<T> = result::Result<T, Error>;
55
55
///
56
56
/// Errors mostly originate from the underlying OS, but custom instances of
57
57
/// `Error` can be created with crafted error messages and a particular value of
58
- /// `ErrorKind`.
58
+ /// [`ErrorKind`].
59
+ ///
60
+ /// [`ErrorKind`]: enum.ErrorKind.html
59
61
#[ derive( Debug ) ]
60
62
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
61
63
pub struct Error {
@@ -77,6 +79,10 @@ struct Custom {
77
79
///
78
80
/// This list is intended to grow over time and it is not recommended to
79
81
/// exhaustively match against it.
82
+ ///
83
+ /// It is used with the [`io::Error`] type.
84
+ ///
85
+ /// [`io::Error`]: struct.Error.html
80
86
#[ derive( Copy , PartialEq , Eq , Clone , Debug ) ]
81
87
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
82
88
#[ allow( deprecated) ]
@@ -208,6 +214,14 @@ impl Error {
208
214
/// This function reads the value of `errno` for the target platform (e.g.
209
215
/// `GetLastError` on Windows) and will return a corresponding instance of
210
216
/// `Error` for the error code.
217
+ ///
218
+ /// # Examples
219
+ ///
220
+ /// ```
221
+ /// use std::io::Error;
222
+ ///
223
+ /// println!("last OS error: {:?}", Error::last_os_error());
224
+ /// ```
211
225
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
212
226
pub fn last_os_error ( ) -> Error {
213
227
Error :: from_raw_os_error ( sys:: os:: errno ( ) as i32 )
@@ -248,6 +262,27 @@ impl Error {
248
262
/// If this `Error` was constructed via `last_os_error` or
249
263
/// `from_raw_os_error`, then this function will return `Some`, otherwise
250
264
/// it will return `None`.
265
+ ///
266
+ /// # Examples
267
+ ///
268
+ /// ```
269
+ /// use std::io::{Error, ErrorKind};
270
+ ///
271
+ /// fn print_os_error(err: &Error) {
272
+ /// if let Some(raw_os_err) = err.raw_os_error() {
273
+ /// println!("raw OS error: {:?}", raw_os_err);
274
+ /// } else {
275
+ /// println!("Not an OS error");
276
+ /// }
277
+ /// }
278
+ ///
279
+ /// fn main() {
280
+ /// // Will print "raw OS error: ...".
281
+ /// print_os_error(&Error::last_os_error());
282
+ /// // Will print "Not an OS error".
283
+ /// print_os_error(&Error::new(ErrorKind::Other, "oh no!"));
284
+ /// }
285
+ /// ```
251
286
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
252
287
pub fn raw_os_error ( & self ) -> Option < i32 > {
253
288
match self . repr {
@@ -260,6 +295,27 @@ impl Error {
260
295
///
261
296
/// If this `Error` was constructed via `new` then this function will
262
297
/// return `Some`, otherwise it will return `None`.
298
+ ///
299
+ /// # Examples
300
+ ///
301
+ /// ```
302
+ /// use std::io::{Error, ErrorKind};
303
+ ///
304
+ /// fn print_error(err: &Error) {
305
+ /// if let Some(inner_err) = err.get_ref() {
306
+ /// println!("Inner error: {:?}", inner_err);
307
+ /// } else {
308
+ /// println!("No inner error");
309
+ /// }
310
+ /// }
311
+ ///
312
+ /// fn main() {
313
+ /// // Will print "No inner error".
314
+ /// print_error(&Error::last_os_error());
315
+ /// // Will print "Inner error: ...".
316
+ /// print_error(&Error::new(ErrorKind::Other, "oh no!"));
317
+ /// }
318
+ /// ```
263
319
#[ stable( feature = "io_error_inner" , since = "1.3.0" ) ]
264
320
pub fn get_ref ( & self ) -> Option < & ( error:: Error +Send +Sync +' static ) > {
265
321
match self . repr {
@@ -273,6 +329,63 @@ impl Error {
273
329
///
274
330
/// If this `Error` was constructed via `new` then this function will
275
331
/// return `Some`, otherwise it will return `None`.
332
+ ///
333
+ /// # Examples
334
+ ///
335
+ /// ```
336
+ /// use std::io::{Error, ErrorKind};
337
+ /// use std::{error, fmt};
338
+ /// use std::fmt::Display;
339
+ ///
340
+ /// #[derive(Debug)]
341
+ /// struct MyError {
342
+ /// v: String,
343
+ /// }
344
+ ///
345
+ /// impl MyError {
346
+ /// fn new() -> MyError {
347
+ /// MyError {
348
+ /// v: "oh no!".to_owned()
349
+ /// }
350
+ /// }
351
+ ///
352
+ /// fn change_message(&mut self, new_message: &str) {
353
+ /// self.v = new_message.to_owned();
354
+ /// }
355
+ /// }
356
+ ///
357
+ /// impl error::Error for MyError {
358
+ /// fn description(&self) -> &str { &self.v }
359
+ /// }
360
+ ///
361
+ /// impl Display for MyError {
362
+ /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
363
+ /// write!(f, "MyError: {}", &self.v)
364
+ /// }
365
+ /// }
366
+ ///
367
+ /// fn change_error(mut err: Error) -> Error {
368
+ /// if let Some(inner_err) = err.get_mut() {
369
+ /// inner_err.downcast_mut::<MyError>().unwrap().change_message("I've been changed!");
370
+ /// }
371
+ /// err
372
+ /// }
373
+ ///
374
+ /// fn print_error(err: &Error) {
375
+ /// if let Some(inner_err) = err.get_ref() {
376
+ /// println!("Inner error: {}", inner_err);
377
+ /// } else {
378
+ /// println!("No inner error");
379
+ /// }
380
+ /// }
381
+ ///
382
+ /// fn main() {
383
+ /// // Will print "No inner error".
384
+ /// print_error(&change_error(Error::last_os_error()));
385
+ /// // Will print "Inner error: ...".
386
+ /// print_error(&change_error(Error::new(ErrorKind::Other, MyError::new())));
387
+ /// }
388
+ /// ```
276
389
#[ stable( feature = "io_error_inner" , since = "1.3.0" ) ]
277
390
pub fn get_mut ( & mut self ) -> Option < & mut ( error:: Error +Send +Sync +' static ) > {
278
391
match self . repr {
@@ -285,6 +398,27 @@ impl Error {
285
398
///
286
399
/// If this `Error` was constructed via `new` then this function will
287
400
/// return `Some`, otherwise it will return `None`.
401
+ ///
402
+ /// # Examples
403
+ ///
404
+ /// ```
405
+ /// use std::io::{Error, ErrorKind};
406
+ ///
407
+ /// fn print_error(err: Error) {
408
+ /// if let Some(inner_err) = err.into_inner() {
409
+ /// println!("Inner error: {}", inner_err);
410
+ /// } else {
411
+ /// println!("No inner error");
412
+ /// }
413
+ /// }
414
+ ///
415
+ /// fn main() {
416
+ /// // Will print "No inner error".
417
+ /// print_error(Error::last_os_error());
418
+ /// // Will print "Inner error: ...".
419
+ /// print_error(Error::new(ErrorKind::Other, "oh no!"));
420
+ /// }
421
+ /// ```
288
422
#[ stable( feature = "io_error_inner" , since = "1.3.0" ) ]
289
423
pub fn into_inner ( self ) -> Option < Box < error:: Error +Send +Sync > > {
290
424
match self . repr {
@@ -294,6 +428,23 @@ impl Error {
294
428
}
295
429
296
430
/// Returns the corresponding `ErrorKind` for this error.
431
+ ///
432
+ /// # Examples
433
+ ///
434
+ /// ```
435
+ /// use std::io::{Error, ErrorKind};
436
+ ///
437
+ /// fn print_error(err: Error) {
438
+ /// println!("{:?}", err.kind());
439
+ /// }
440
+ ///
441
+ /// fn main() {
442
+ /// // Will print "No inner error".
443
+ /// print_error(Error::last_os_error());
444
+ /// // Will print "Inner error: ...".
445
+ /// print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
446
+ /// }
447
+ /// ```
297
448
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
298
449
pub fn kind ( & self ) -> ErrorKind {
299
450
match self . repr {
0 commit comments