]> git.proxmox.com Git - rustc.git/blob - vendor/wasi/src/error.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / vendor / wasi / src / error.rs
1 use super::Errno;
2 use core::fmt;
3 use core::num::NonZeroU16;
4
5 /// A raw error returned by wasi APIs, internally containing a 16-bit error
6 /// code.
7 #[derive(Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
8 pub struct Error {
9 code: NonZeroU16,
10 }
11
12 impl Error {
13 /// Constructs a new error from a raw error code, returning `None` if the
14 /// error code is zero (which means success).
15 pub fn from_raw_error(error: Errno) -> Option<Error> {
16 Some(Error {
17 code: NonZeroU16::new(error)?,
18 })
19 }
20
21 /// Returns the raw error code that this error represents.
22 pub fn raw_error(&self) -> u16 {
23 self.code.get()
24 }
25 }
26
27 impl fmt::Display for Error {
28 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29 write!(
30 f,
31 "{} (error {})",
32 super::strerror(self.code.get()),
33 self.code
34 )?;
35 Ok(())
36 }
37 }
38
39 impl fmt::Debug for Error {
40 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41 f.debug_struct("Error")
42 .field("code", &self.code)
43 .field("message", &super::strerror(self.code.get()))
44 .finish()
45 }
46 }
47
48 #[cfg(feature = "std")]
49 extern crate std;
50 #[cfg(feature = "std")]
51 impl std::error::Error for Error {}