diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/wasi-0.10.2+wasi-snapshot-preview1/src/error.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/wasi-0.10.2+wasi-snapshot-preview1/src/error.rs')
-rw-r--r-- | vendor/wasi-0.10.2+wasi-snapshot-preview1/src/error.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/vendor/wasi-0.10.2+wasi-snapshot-preview1/src/error.rs b/vendor/wasi-0.10.2+wasi-snapshot-preview1/src/error.rs new file mode 100644 index 000000000..fb8b44389 --- /dev/null +++ b/vendor/wasi-0.10.2+wasi-snapshot-preview1/src/error.rs @@ -0,0 +1,52 @@ +use super::Errno; +use core::fmt; +use core::num::NonZeroU16; + +/// A raw error returned by wasi APIs, internally containing a 16-bit error +/// code. +#[derive(Copy, Clone, PartialEq, Eq, Ord, PartialOrd)] +pub struct Error { + code: NonZeroU16, +} + +impl Error { + /// Constructs a new error from a raw error code, returning `None` if the + /// error code is zero (which means success). + pub fn from_raw_error(error: Errno) -> Option<Error> { + Some(Error { + code: NonZeroU16::new(error)?, + }) + } + + /// Returns the raw error code that this error represents. + pub fn raw_error(&self) -> u16 { + self.code.get() + } +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{} (error {})", + super::errno_name(self.code.get()), + self.code + )?; + Ok(()) + } +} + +impl fmt::Debug for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Error") + .field("code", &self.code) + .field("name", &super::errno_name(self.code.get())) + .field("message", &super::errno_docs(self.code.get())) + .finish() + } +} + +#[cfg(feature = "std")] +extern crate std; +#[cfg(feature = "std")] +impl std::error::Error for Error {} |