1#![no_std]
8
9use core::fmt;
10use zx_types as sys;
11
12#[macro_export]
18macro_rules! assoc_values {
19 ($typename:ident, [$($(#[$attr:meta])* $name:ident = $value:path;)*]) => {
20 #[allow(non_upper_case_globals)]
21 impl $typename {
22 $(
23 $(#[$attr])*
24 pub const $name: $typename = $typename($value);
25 )*
26
27 fn assoc_const_name(&self) -> Option<&'static str> {
28 match self.0 {
29 $(
30 $value => Some(stringify!($name)),
31 )*
32 _ => None,
33 }
34 }
35 }
36
37 impl ::core::fmt::Debug for $typename {
38 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
39 f.write_str(concat!(stringify!($typename), "("))?;
40 match self.assoc_const_name() {
41 Some(name) => f.write_str(&name)?,
42 None => ::core::fmt::Debug::fmt(&self.0, f)?,
43 }
44 f.write_str(")")
45 }
46 }
47 }
48}
49
50#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
57#[repr(transparent)]
58pub struct Status(sys::zx_status_t);
59impl Status {
60 pub fn ok(raw: sys::zx_status_t) -> Result<(), Status> {
63 if raw == Status::OK.0 { Ok(()) } else { Err(Status(raw)) }
64 }
65
66 pub fn from_raw(raw: sys::zx_status_t) -> Self {
67 Status(raw)
68 }
69
70 pub fn into_raw(self) -> sys::zx_status_t {
71 self.0
72 }
73}
74
75pub fn ok(raw: sys::zx_status_t) -> Result<(), Status> {
77 Status::ok(raw)
78}
79
80assoc_values!(Status, [
82 #[doc = "Indicates an operation was successful."]
83 OK = sys::ZX_OK;
84 #[doc = "The system encountered an otherwise unspecified error while performing the"]
85 #[doc = "operation."]
86 INTERNAL = sys::ZX_ERR_INTERNAL;
87 #[doc = "The operation is not implemented, supported, or enabled."]
88 NOT_SUPPORTED = sys::ZX_ERR_NOT_SUPPORTED;
89 #[doc = "The system was not able to allocate some resource needed for the operation."]
90 NO_RESOURCES = sys::ZX_ERR_NO_RESOURCES;
91 #[doc = "The system was not able to allocate memory needed for the operation."]
92 NO_MEMORY = sys::ZX_ERR_NO_MEMORY;
93 #[doc = "The system call was interrupted, but should be retried. This should not be"]
94 #[doc = "seen outside of the VDSO."]
95 INTERRUPTED_RETRY = sys::ZX_ERR_INTERRUPTED_RETRY;
96 #[doc = "An argument is invalid. For example, a null pointer when a null pointer is"]
97 #[doc = "not permitted."]
98 INVALID_ARGS = sys::ZX_ERR_INVALID_ARGS;
99 #[doc = "A specified handle value does not refer to a handle."]
100 BAD_HANDLE = sys::ZX_ERR_BAD_HANDLE;
101 #[doc = "The subject of the operation is the wrong type to perform the operation."]
102 #[doc = ""]
103 #[doc = "For example: Attempting a message_read on a thread handle."]
104 WRONG_TYPE = sys::ZX_ERR_WRONG_TYPE;
105 #[doc = "The specified syscall number is invalid."]
106 BAD_SYSCALL = sys::ZX_ERR_BAD_SYSCALL;
107 #[doc = "An argument is outside the valid range for this operation."]
108 OUT_OF_RANGE = sys::ZX_ERR_OUT_OF_RANGE;
109 #[doc = "The caller-provided buffer is too small for this operation."]
110 BUFFER_TOO_SMALL = sys::ZX_ERR_BUFFER_TOO_SMALL;
111 #[doc = "The operation failed because the current state of the object does not allow"]
112 #[doc = "it, or a precondition of the operation is not satisfied."]
113 BAD_STATE = sys::ZX_ERR_BAD_STATE;
114 #[doc = "The time limit for the operation elapsed before the operation completed."]
115 TIMED_OUT = sys::ZX_ERR_TIMED_OUT;
116 #[doc = "The operation cannot be performed currently but potentially could succeed if"]
117 #[doc = "the caller waits for a prerequisite to be satisfied, like waiting for a"]
118 #[doc = "handle to be readable or writable."]
119 #[doc = ""]
120 #[doc = "Example: Attempting to read from a channel that has no messages waiting but"]
121 #[doc = "has an open remote will return `ZX_ERR_SHOULD_WAIT`. In contrast, attempting"]
122 #[doc = "to read from a channel that has no messages waiting and has a closed remote"]
123 #[doc = "end will return `ZX_ERR_PEER_CLOSED`."]
124 SHOULD_WAIT = sys::ZX_ERR_SHOULD_WAIT;
125 #[doc = "The in-progress operation, for example, a wait, has been canceled."]
126 CANCELED = sys::ZX_ERR_CANCELED;
127 #[doc = "The operation failed because the remote end of the subject of the operation"]
128 #[doc = "was closed."]
129 PEER_CLOSED = sys::ZX_ERR_PEER_CLOSED;
130 #[doc = "The requested entity is not found."]
131 NOT_FOUND = sys::ZX_ERR_NOT_FOUND;
132 #[doc = "An object with the specified identifier already exists."]
133 #[doc = ""]
134 #[doc = "Example: Attempting to create a file when a file already exists with that"]
135 #[doc = "name."]
136 ALREADY_EXISTS = sys::ZX_ERR_ALREADY_EXISTS;
137 #[doc = "The operation failed because the named entity is already owned or controlled"]
138 #[doc = "by another entity. The operation could succeed later if the current owner"]
139 #[doc = "releases the entity."]
140 ALREADY_BOUND = sys::ZX_ERR_ALREADY_BOUND;
141 #[doc = "The subject of the operation is currently unable to perform the operation."]
142 #[doc = ""]
143 #[doc = "This is used when there's no direct way for the caller to observe when the"]
144 #[doc = "subject will be able to perform the operation and should thus retry."]
145 UNAVAILABLE = sys::ZX_ERR_UNAVAILABLE;
146 #[doc = "The caller did not have permission to perform the specified operation."]
147 ACCESS_DENIED = sys::ZX_ERR_ACCESS_DENIED;
148 #[doc = "Otherwise-unspecified error occurred during I/O."]
149 IO = sys::ZX_ERR_IO;
150 #[doc = "The entity the I/O operation is being performed on rejected the operation."]
151 #[doc = ""]
152 #[doc = "Example: an I2C device NAK'ing a transaction or a disk controller rejecting"]
153 #[doc = "an invalid command, or a stalled USB endpoint."]
154 IO_REFUSED = sys::ZX_ERR_IO_REFUSED;
155 #[doc = "The data in the operation failed an integrity check and is possibly"]
156 #[doc = "corrupted."]
157 #[doc = ""]
158 #[doc = "Example: CRC or Parity error."]
159 IO_DATA_INTEGRITY = sys::ZX_ERR_IO_DATA_INTEGRITY;
160 #[doc = "The data in the operation is currently unavailable and may be permanently"]
161 #[doc = "lost."]
162 #[doc = ""]
163 #[doc = "Example: A disk block is irrecoverably damaged."]
164 IO_DATA_LOSS = sys::ZX_ERR_IO_DATA_LOSS;
165 #[doc = "The device is no longer available (has been unplugged from the system,"]
166 #[doc = "powered down, or the driver has been unloaded)."]
167 IO_NOT_PRESENT = sys::ZX_ERR_IO_NOT_PRESENT;
168 #[doc = "More data was received from the device than expected."]
169 #[doc = ""]
170 #[doc = "Example: a USB \"babble\" error due to a device sending more data than the"]
171 #[doc = "host queued to receive."]
172 IO_OVERRUN = sys::ZX_ERR_IO_OVERRUN;
173 #[doc = "An operation did not complete within the required timeframe."]
174 #[doc = ""]
175 #[doc = "Example: A USB isochronous transfer that failed to complete due to an"]
176 #[doc = "overrun or underrun."]
177 IO_MISSED_DEADLINE = sys::ZX_ERR_IO_MISSED_DEADLINE;
178 #[doc = "The data in the operation is invalid parameter or is out of range."]
179 #[doc = ""]
180 #[doc = "Example: A USB transfer that failed to complete with TRB Error"]
181 IO_INVALID = sys::ZX_ERR_IO_INVALID;
182 #[doc = "Path name is too long."]
183 BAD_PATH = sys::ZX_ERR_BAD_PATH;
184 #[doc = "The object is not a directory or does not support directory operations."]
185 #[doc = ""]
186 #[doc = "Example: Attempted to open a file as a directory or attempted to do"]
187 #[doc = "directory operations on a file."]
188 NOT_DIR = sys::ZX_ERR_NOT_DIR;
189 #[doc = "Object is not a regular file."]
190 NOT_FILE = sys::ZX_ERR_NOT_FILE;
191 #[doc = "This operation would cause a file to exceed a filesystem-specific size"]
192 #[doc = "limit."]
193 FILE_BIG = sys::ZX_ERR_FILE_BIG;
194 #[doc = "The filesystem or device space is exhausted."]
195 NO_SPACE = sys::ZX_ERR_NO_SPACE;
196 #[doc = "The directory is not empty for an operation that requires it to be empty."]
197 #[doc = ""]
198 #[doc = "For example, non-recursively deleting a directory with files still in it."]
199 NOT_EMPTY = sys::ZX_ERR_NOT_EMPTY;
200 #[doc = "An indicate to not call again."]
201 #[doc = ""]
202 #[doc = "The flow control values `ZX_ERR_STOP`, `ZX_ERR_NEXT`, and `ZX_ERR_ASYNC` are"]
203 #[doc = "not errors and will never be returned by a system call or public API. They"]
204 #[doc = "allow callbacks to request their caller perform some other operation."]
205 #[doc = ""]
206 #[doc = "For example, a callback might be called on every event until it returns"]
207 #[doc = "something other than `ZX_OK`. This status allows differentiation between"]
208 #[doc = "\"stop due to an error\" and \"stop because work is done.\""]
209 STOP = sys::ZX_ERR_STOP;
210 #[doc = "Advance to the next item."]
211 #[doc = ""]
212 #[doc = "The flow control values `ZX_ERR_STOP`, `ZX_ERR_NEXT`, and `ZX_ERR_ASYNC` are"]
213 #[doc = "not errors and will never be returned by a system call or public API. They"]
214 #[doc = "allow callbacks to request their caller perform some other operation."]
215 #[doc = ""]
216 #[doc = "For example, a callback could use this value to indicate it did not consume"]
217 #[doc = "an item passed to it, but by choice, not due to an error condition."]
218 NEXT = sys::ZX_ERR_NEXT;
219 #[doc = "Ownership of the item has moved to an asynchronous worker."]
220 #[doc = ""]
221 #[doc = "The flow control values `ZX_ERR_STOP`, `ZX_ERR_NEXT`, and `ZX_ERR_ASYNC` are"]
222 #[doc = "not errors and will never be returned by a system call or public API. They"]
223 #[doc = "allow callbacks to request their caller perform some other operation."]
224 #[doc = ""]
225 #[doc = "Unlike `ZX_ERR_STOP`, which implies that iteration on an object"]
226 #[doc = "should stop, and `ZX_ERR_NEXT`, which implies that iteration"]
227 #[doc = "should continue to the next item, `ZX_ERR_ASYNC` implies"]
228 #[doc = "that an asynchronous worker is responsible for continuing iteration."]
229 #[doc = ""]
230 #[doc = "For example, a callback will be called on every event, but one event needs"]
231 #[doc = "to handle some work asynchronously before it can continue. `ZX_ERR_ASYNC`"]
232 #[doc = "implies the worker is responsible for resuming iteration once its work has"]
233 #[doc = "completed."]
234 ASYNC = sys::ZX_ERR_ASYNC;
235 #[doc = "The specified protocol is not supported."]
236 PROTOCOL_NOT_SUPPORTED = sys::ZX_ERR_PROTOCOL_NOT_SUPPORTED;
237 #[doc = "The host is unreachable."]
238 ADDRESS_UNREACHABLE = sys::ZX_ERR_ADDRESS_UNREACHABLE;
239 #[doc = "Address is being used by someone else."]
240 ADDRESS_IN_USE = sys::ZX_ERR_ADDRESS_IN_USE;
241 #[doc = "The socket is not connected."]
242 NOT_CONNECTED = sys::ZX_ERR_NOT_CONNECTED;
243 #[doc = "The remote peer rejected the connection."]
244 CONNECTION_REFUSED = sys::ZX_ERR_CONNECTION_REFUSED;
245 #[doc = "The connection was reset."]
246 CONNECTION_RESET = sys::ZX_ERR_CONNECTION_RESET;
247 #[doc = "The connection was aborted."]
248 CONNECTION_ABORTED = sys::ZX_ERR_CONNECTION_ABORTED;
249]);
250impl Status {
253 pub fn from_result(res: Result<(), Self>) -> Self {
254 res.into()
255 }
256}
257
258impl fmt::Display for Status {
259 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
260 match self.assoc_const_name() {
261 Some(name) => name.fmt(f),
262 None => write!(f, "Unknown zircon status code: {}", self.0),
263 }
264 }
265}
266
267impl core::error::Error for Status {}
268
269impl From<Result<(), Status>> for Status {
270 fn from(res: Result<(), Status>) -> Status {
271 match res {
272 Ok(()) => Self::OK,
273 Err(status) => status,
274 }
275 }
276}
277
278impl From<Status> for Result<(), Status> {
279 fn from(src: Status) -> Result<(), Status> {
280 Status::ok(src.into_raw())
281 }
282}
283
284#[cfg(test)]
285mod test {
286 extern crate std;
287 use super::Status;
288
289 #[test]
290 fn status_debug_format() {
291 let cases = [
292 ("Status(OK)", Status::OK),
293 ("Status(BAD_SYSCALL)", Status::BAD_SYSCALL),
294 ("Status(NEXT)", Status::NEXT),
295 ("Status(-5050)", Status(-5050)),
296 ];
297 for &(expected, value) in &cases {
298 assert_eq!(expected, std::format!("{:?}", value));
299 }
300 }
301
302 #[test]
303 fn status_into_result() {
304 let ok_result: Result<(), Status> = Status::OK.into();
305 assert_eq!(ok_result, Ok(()));
306
307 let err_result: Result<(), Status> = Status::BAD_SYSCALL.into();
308 assert_eq!(err_result, Err(Status::BAD_SYSCALL));
309 }
310}