@@ -208,6 +208,14 @@ impl Error {
208
208
/// This function reads the value of `errno` for the target platform (e.g.
209
209
/// `GetLastError` on Windows) and will return a corresponding instance of
210
210
/// `Error` for the error code.
211
+ ///
212
+ /// # Examples
213
+ ///
214
+ /// ```
215
+ /// use std::io::Error;
216
+ ///
217
+ /// println!("last OS error: {:?}", Error::last_os_error());
218
+ /// ```
211
219
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
212
220
pub fn last_os_error ( ) -> Error {
213
221
Error :: from_raw_os_error ( sys:: os:: errno ( ) as i32 )
@@ -248,6 +256,27 @@ impl Error {
248
256
/// If this `Error` was constructed via `last_os_error` or
249
257
/// `from_raw_os_error`, then this function will return `Some`, otherwise
250
258
/// it will return `None`.
259
+ ///
260
+ /// # Examples
261
+ ///
262
+ /// ```
263
+ /// use std::io::{Error, ErrorKind};
264
+ ///
265
+ /// fn print_os_error(err: &Error) {
266
+ /// if let Some(raw_os_err) = err.raw_os_error() {
267
+ /// println!("raw OS error: {:?}", raw_os_err);
268
+ /// } else {
269
+ /// println!("Not an OS error");
270
+ /// }
271
+ /// }
272
+ ///
273
+ /// fn main() {
274
+ /// // Will print "raw OS error: ...".
275
+ /// print_os_error(&Error::last_os_error());
276
+ /// // Will print "Not an OS error".
277
+ /// print_os_error(&Error::new(ErrorKind::Other, "oh no!"));
278
+ /// }
279
+ /// ```
251
280
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
252
281
pub fn raw_os_error ( & self ) -> Option < i32 > {
253
282
match self . repr {
@@ -260,6 +289,27 @@ impl Error {
260
289
///
261
290
/// If this `Error` was constructed via `new` then this function will
262
291
/// return `Some`, otherwise it will return `None`.
292
+ ///
293
+ /// # Examples
294
+ ///
295
+ /// ```
296
+ /// use std::io::{Error, ErrorKind};
297
+ ///
298
+ /// fn print_error(err: &Error) {
299
+ /// if let Some(inner_err) = err.get_ref() {
300
+ /// println!("Inner error: {:?}", inner_err);
301
+ /// } else {
302
+ /// println!("No inner error");
303
+ /// }
304
+ /// }
305
+ ///
306
+ /// fn main() {
307
+ /// // Will print "No inner error".
308
+ /// print_error(&Error::last_os_error());
309
+ /// // Will print "Inner error: ...".
310
+ /// print_error(&Error::new(ErrorKind::Other, "oh no!"));
311
+ /// }
312
+ /// ```
263
313
#[ stable( feature = "io_error_inner" , since = "1.3.0" ) ]
264
314
pub fn get_ref ( & self ) -> Option < & ( error:: Error +Send +Sync +' static ) > {
265
315
match self . repr {
@@ -273,6 +323,63 @@ impl Error {
273
323
///
274
324
/// If this `Error` was constructed via `new` then this function will
275
325
/// return `Some`, otherwise it will return `None`.
326
+ ///
327
+ /// # Examples
328
+ ///
329
+ /// ```
330
+ /// use std::io::{Error, ErrorKind};
331
+ /// use std::{error, fmt};
332
+ /// use std::fmt::Display;
333
+ ///
334
+ /// #[derive(Debug)]
335
+ /// struct MyError {
336
+ /// v: String,
337
+ /// }
338
+ ///
339
+ /// impl MyError {
340
+ /// fn new() -> MyError {
341
+ /// MyError {
342
+ /// v: "oh no!".to_owned()
343
+ /// }
344
+ /// }
345
+ ///
346
+ /// fn change_message(&mut self, new_message: &str) {
347
+ /// self.v = new_message.to_owned();
348
+ /// }
349
+ /// }
350
+ ///
351
+ /// impl error::Error for MyError {
352
+ /// fn description(&self) -> &str { &self.v }
353
+ /// }
354
+ ///
355
+ /// impl Display for MyError {
356
+ /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
357
+ /// write!(f, "MyError: {}", &self.v)
358
+ /// }
359
+ /// }
360
+ ///
361
+ /// fn change_error(mut err: Error) -> Error {
362
+ /// if let Some(inner_err) = err.get_mut() {
363
+ /// inner_err.downcast_mut::<MyError>().unwrap().change_message("I've been changed!");
364
+ /// }
365
+ /// err
366
+ /// }
367
+ ///
368
+ /// fn print_error(err: &Error) {
369
+ /// if let Some(inner_err) = err.get_ref() {
370
+ /// println!("Inner error: {}", inner_err);
371
+ /// } else {
372
+ /// println!("No inner error");
373
+ /// }
374
+ /// }
375
+ ///
376
+ /// fn main() {
377
+ /// // Will print "No inner error".
378
+ /// print_error(&change_error(Error::last_os_error()));
379
+ /// // Will print "Inner error: ...".
380
+ /// print_error(&change_error(Error::new(ErrorKind::Other, MyError::new())));
381
+ /// }
382
+ /// ```
276
383
#[ stable( feature = "io_error_inner" , since = "1.3.0" ) ]
277
384
pub fn get_mut ( & mut self ) -> Option < & mut ( error:: Error +Send +Sync +' static ) > {
278
385
match self . repr {
@@ -285,6 +392,27 @@ impl Error {
285
392
///
286
393
/// If this `Error` was constructed via `new` then this function will
287
394
/// return `Some`, otherwise it will return `None`.
395
+ ///
396
+ /// # Examples
397
+ ///
398
+ /// ```
399
+ /// use std::io::{Error, ErrorKind};
400
+ ///
401
+ /// fn print_error(err: Error) {
402
+ /// if let Some(inner_err) = err.into_inner() {
403
+ /// println!("Inner error: {}", inner_err);
404
+ /// } else {
405
+ /// println!("No inner error");
406
+ /// }
407
+ /// }
408
+ ///
409
+ /// fn main() {
410
+ /// // Will print "No inner error".
411
+ /// print_error(Error::last_os_error());
412
+ /// // Will print "Inner error: ...".
413
+ /// print_error(Error::new(ErrorKind::Other, "oh no!"));
414
+ /// }
415
+ /// ```
288
416
#[ stable( feature = "io_error_inner" , since = "1.3.0" ) ]
289
417
pub fn into_inner ( self ) -> Option < Box < error:: Error +Send +Sync > > {
290
418
match self . repr {
@@ -294,6 +422,23 @@ impl Error {
294
422
}
295
423
296
424
/// Returns the corresponding `ErrorKind` for this error.
425
+ ///
426
+ /// # Examples
427
+ ///
428
+ /// ```
429
+ /// use std::io::{Error, ErrorKind};
430
+ ///
431
+ /// fn print_error(err: Error) {
432
+ /// println!("{:?}", err.kind());
433
+ /// }
434
+ ///
435
+ /// fn main() {
436
+ /// // Will print "No inner error".
437
+ /// print_error(Error::last_os_error());
438
+ /// // Will print "Inner error: ...".
439
+ /// print_error(Error::new(ErrorKind::AddrInUse, "oh no!"));
440
+ /// }
441
+ /// ```
297
442
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
298
443
pub fn kind ( & self ) -> ErrorKind {
299
444
match self . repr {
0 commit comments