@@ -12,6 +12,7 @@ use error;
12
12
use fmt;
13
13
use result;
14
14
use sys;
15
+ use convert:: From ;
15
16
16
17
/// A specialized [`Result`](../result/enum.Result.html) type for I/O
17
18
/// operations.
@@ -62,6 +63,7 @@ pub struct Error {
62
63
63
64
enum Repr {
64
65
Os ( i32 ) ,
66
+ Simple ( ErrorKind ) ,
65
67
Custom ( Box < Custom > ) ,
66
68
}
67
69
@@ -171,6 +173,43 @@ pub enum ErrorKind {
171
173
__Nonexhaustive,
172
174
}
173
175
176
+ impl ErrorKind {
177
+ fn as_str ( & self ) -> & ' static str {
178
+ match * self {
179
+ ErrorKind :: NotFound => "entity not found" ,
180
+ ErrorKind :: PermissionDenied => "permission denied" ,
181
+ ErrorKind :: ConnectionRefused => "connection refused" ,
182
+ ErrorKind :: ConnectionReset => "connection reset" ,
183
+ ErrorKind :: ConnectionAborted => "connection aborted" ,
184
+ ErrorKind :: NotConnected => "not connected" ,
185
+ ErrorKind :: AddrInUse => "address in use" ,
186
+ ErrorKind :: AddrNotAvailable => "address not available" ,
187
+ ErrorKind :: BrokenPipe => "broken pipe" ,
188
+ ErrorKind :: AlreadyExists => "entity already exists" ,
189
+ ErrorKind :: WouldBlock => "operation would block" ,
190
+ ErrorKind :: InvalidInput => "invalid input parameter" ,
191
+ ErrorKind :: InvalidData => "invalid data" ,
192
+ ErrorKind :: TimedOut => "timed out" ,
193
+ ErrorKind :: WriteZero => "write zero" ,
194
+ ErrorKind :: Interrupted => "operation interrupted" ,
195
+ ErrorKind :: Other => "other os error" ,
196
+ ErrorKind :: UnexpectedEof => "unexpected end of file" ,
197
+ ErrorKind :: __Nonexhaustive => unreachable ! ( )
198
+ }
199
+ }
200
+ }
201
+
202
+ /// Intended for use for errors not exposed to the user, where allocating onto
203
+ /// the heap (for normal construction via Error::new) is too costly.
204
+ #[ stable( feature = "io_error_from_errorkind" , since = "1.14.0" ) ]
205
+ impl From < ErrorKind > for Error {
206
+ fn from ( kind : ErrorKind ) -> Error {
207
+ Error {
208
+ repr : Repr :: Simple ( kind)
209
+ }
210
+ }
211
+ }
212
+
174
213
impl Error {
175
214
/// Creates a new I/O error from a known kind of error as well as an
176
215
/// arbitrary error payload.
@@ -285,6 +324,7 @@ impl Error {
285
324
match self . repr {
286
325
Repr :: Os ( i) => Some ( i) ,
287
326
Repr :: Custom ( ..) => None ,
327
+ Repr :: Simple ( ..) => None ,
288
328
}
289
329
}
290
330
@@ -317,6 +357,7 @@ impl Error {
317
357
pub fn get_ref ( & self ) -> Option < & ( error:: Error +Send +Sync +' static ) > {
318
358
match self . repr {
319
359
Repr :: Os ( ..) => None ,
360
+ Repr :: Simple ( ..) => None ,
320
361
Repr :: Custom ( ref c) => Some ( & * c. error ) ,
321
362
}
322
363
}
@@ -387,6 +428,7 @@ impl Error {
387
428
pub fn get_mut ( & mut self ) -> Option < & mut ( error:: Error +Send +Sync +' static ) > {
388
429
match self . repr {
389
430
Repr :: Os ( ..) => None ,
431
+ Repr :: Simple ( ..) => None ,
390
432
Repr :: Custom ( ref mut c) => Some ( & mut * c. error ) ,
391
433
}
392
434
}
@@ -420,6 +462,7 @@ impl Error {
420
462
pub fn into_inner ( self ) -> Option < Box < error:: Error +Send +Sync > > {
421
463
match self . repr {
422
464
Repr :: Os ( ..) => None ,
465
+ Repr :: Simple ( ..) => None ,
423
466
Repr :: Custom ( c) => Some ( c. error )
424
467
}
425
468
}
@@ -447,6 +490,7 @@ impl Error {
447
490
match self . repr {
448
491
Repr :: Os ( code) => sys:: decode_error_kind ( code) ,
449
492
Repr :: Custom ( ref c) => c. kind ,
493
+ Repr :: Simple ( kind) => kind,
450
494
}
451
495
}
452
496
}
@@ -458,6 +502,7 @@ impl fmt::Debug for Repr {
458
502
fmt. debug_struct ( "Os" ) . field ( "code" , code)
459
503
. field ( "message" , & sys:: os:: error_string ( * code) ) . finish ( ) ,
460
504
Repr :: Custom ( ref c) => fmt. debug_tuple ( "Custom" ) . field ( c) . finish ( ) ,
505
+ Repr :: Simple ( kind) => fmt. debug_tuple ( "Kind" ) . field ( & kind) . finish ( ) ,
461
506
}
462
507
}
463
508
}
@@ -471,6 +516,7 @@ impl fmt::Display for Error {
471
516
write ! ( fmt, "{} (os error {})" , detail, code)
472
517
}
473
518
Repr :: Custom ( ref c) => c. error . fmt ( fmt) ,
519
+ Repr :: Simple ( kind) => write ! ( fmt, "{}" , kind. as_str( ) ) ,
474
520
}
475
521
}
476
522
}
@@ -479,34 +525,15 @@ impl fmt::Display for Error {
479
525
impl error:: Error for Error {
480
526
fn description ( & self ) -> & str {
481
527
match self . repr {
482
- Repr :: Os ( ..) => match self . kind ( ) {
483
- ErrorKind :: NotFound => "entity not found" ,
484
- ErrorKind :: PermissionDenied => "permission denied" ,
485
- ErrorKind :: ConnectionRefused => "connection refused" ,
486
- ErrorKind :: ConnectionReset => "connection reset" ,
487
- ErrorKind :: ConnectionAborted => "connection aborted" ,
488
- ErrorKind :: NotConnected => "not connected" ,
489
- ErrorKind :: AddrInUse => "address in use" ,
490
- ErrorKind :: AddrNotAvailable => "address not available" ,
491
- ErrorKind :: BrokenPipe => "broken pipe" ,
492
- ErrorKind :: AlreadyExists => "entity already exists" ,
493
- ErrorKind :: WouldBlock => "operation would block" ,
494
- ErrorKind :: InvalidInput => "invalid input parameter" ,
495
- ErrorKind :: InvalidData => "invalid data" ,
496
- ErrorKind :: TimedOut => "timed out" ,
497
- ErrorKind :: WriteZero => "write zero" ,
498
- ErrorKind :: Interrupted => "operation interrupted" ,
499
- ErrorKind :: Other => "other os error" ,
500
- ErrorKind :: UnexpectedEof => "unexpected end of file" ,
501
- ErrorKind :: __Nonexhaustive => unreachable ! ( )
502
- } ,
528
+ Repr :: Os ( ..) | Repr :: Simple ( ..) => self . kind ( ) . as_str ( ) ,
503
529
Repr :: Custom ( ref c) => c. error . description ( ) ,
504
530
}
505
531
}
506
532
507
533
fn cause ( & self ) -> Option < & error:: Error > {
508
534
match self . repr {
509
535
Repr :: Os ( ..) => None ,
536
+ Repr :: Simple ( ..) => None ,
510
537
Repr :: Custom ( ref c) => c. error . cause ( ) ,
511
538
}
512
539
}
0 commit comments