Skip to main content

zx_status_ext/
lib.rs

1// Copyright 2026 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use std::io;
6use zx_status::Status;
7
8/// Extension trait for `zx_status::Status` to provide conversions to `std::io` types.
9pub 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::OK
40            | Status::NEXT
41            | Status::STOP
42            | Status::NO_SPACE
43            | Status::FILE_BIG
44            | Status::NOT_FILE
45            | Status::NOT_DIR
46            | Status::IO_DATA_LOSS
47            | Status::IO
48            | Status::CANCELED
49            | Status::BAD_STATE
50            | Status::BUFFER_TOO_SMALL
51            | Status::BAD_SYSCALL
52            | Status::INTERNAL
53            | Status::NOT_SUPPORTED
54            | Status::NO_RESOURCES
55            | Status::NO_MEMORY
56            | _ => Other,
57        }
58    }
59}
60
61/// Extension trait for `std::io::ErrorKind` to provide conversions to `zx_status::Status`.
62pub trait IoErrorKindExt {
63    fn to_status(self) -> Status;
64}
65
66impl IoErrorKindExt for io::ErrorKind {
67    fn to_status(self) -> Status {
68        use std::io::ErrorKind::*;
69        match self {
70            NotFound => Status::NOT_FOUND,
71            PermissionDenied => Status::ACCESS_DENIED,
72            ConnectionRefused => Status::IO_REFUSED,
73            ConnectionAborted => Status::PEER_CLOSED,
74            AddrInUse => Status::ALREADY_BOUND,
75            AddrNotAvailable => Status::UNAVAILABLE,
76            BrokenPipe => Status::PEER_CLOSED,
77            AlreadyExists => Status::ALREADY_EXISTS,
78            WouldBlock => Status::SHOULD_WAIT,
79            InvalidInput => Status::INVALID_ARGS,
80            TimedOut => Status::TIMED_OUT,
81            Interrupted => Status::INTERRUPTED_RETRY,
82            UnexpectedEof | WriteZero | ConnectionReset | NotConnected | Other | _ => Status::IO,
83        }
84    }
85}