1use std::io;
6use zx_status::Status;
7
8pub trait StatusExt {
10 fn into_io_error(self) -> io::Error;
11 fn into_io_error_kind(self) -> io::ErrorKind;
12}
13
14impl StatusExt for Status {
15 fn into_io_error(self) -> io::Error {
16 io::Error::from(self.into_io_error_kind())
17 }
18
19 fn into_io_error_kind(self) -> io::ErrorKind {
20 use std::io::ErrorKind::*;
21 match self {
22 Status::INTERRUPTED_RETRY => Interrupted,
23 Status::BAD_HANDLE => BrokenPipe,
24 Status::TIMED_OUT => TimedOut,
25 Status::SHOULD_WAIT => WouldBlock,
26 Status::PEER_CLOSED => ConnectionAborted,
27 Status::NOT_FOUND => NotFound,
28 Status::ALREADY_EXISTS => AlreadyExists,
29 Status::ALREADY_BOUND => AlreadyExists,
30 Status::UNAVAILABLE => AddrNotAvailable,
31 Status::ACCESS_DENIED => PermissionDenied,
32 Status::IO_REFUSED => ConnectionRefused,
33 Status::IO_DATA_INTEGRITY => InvalidData,
34
35 Status::BAD_PATH | Status::INVALID_ARGS | Status::OUT_OF_RANGE | Status::WRONG_TYPE => {
36 InvalidInput
37 }
38
39 Status::NEXT
40 | Status::STOP
41 | Status::NO_SPACE
42 | Status::FILE_BIG
43 | Status::NOT_FILE
44 | Status::NOT_DIR
45 | Status::IO_DATA_LOSS
46 | Status::IO
47 | Status::CANCELED
48 | Status::BAD_STATE
49 | Status::BUFFER_TOO_SMALL
50 | Status::BAD_SYSCALL
51 | Status::INTERNAL
52 | Status::NOT_SUPPORTED
53 | Status::NO_RESOURCES
54 | Status::NO_MEMORY
55 | _ => Other,
56 }
57 }
58}
59
60pub trait IoErrorKindExt {
62 fn to_status(self) -> Status;
63}
64
65impl IoErrorKindExt for io::ErrorKind {
66 fn to_status(self) -> Status {
67 use std::io::ErrorKind::*;
68 match self {
69 NotFound => Status::NOT_FOUND,
70 PermissionDenied => Status::ACCESS_DENIED,
71 ConnectionRefused => Status::IO_REFUSED,
72 ConnectionAborted => Status::PEER_CLOSED,
73 AddrInUse => Status::ALREADY_BOUND,
74 AddrNotAvailable => Status::UNAVAILABLE,
75 BrokenPipe => Status::PEER_CLOSED,
76 AlreadyExists => Status::ALREADY_EXISTS,
77 WouldBlock => Status::SHOULD_WAIT,
78 InvalidInput => Status::INVALID_ARGS,
79 TimedOut => Status::TIMED_OUT,
80 Interrupted => Status::INTERRUPTED_RETRY,
81 UnexpectedEof | WriteZero | ConnectionReset | NotConnected | Other | _ => Status::IO,
82 }
83 }
84}