use core::fmt::{self, Display, Formatter};
#[non_exhaustive]
#[derive(
Clone,
Copy,
Debug,
Eq,
Hash,
Ord,
PartialEq,
PartialOrd
)]
pub enum ErrorKind {
AddrInUse,
AddrNotAvailable,
AlreadyExists,
ArgumentListTooLong,
BrokenPipe,
ConnectionAborted,
ConnectionRefused,
ConnectionReset,
CrossesDevices,
Deadlock,
DirectoryNotEmpty,
ExecutableFileBusy,
FileTooLarge,
HostUnreachable,
Interrupted,
InvalidData,
InvalidFilename,
InvalidInput,
IsADirectory,
NetworkDown,
NetworkUnreachable,
NotADirectory,
NotConnected,
NotFound,
NotSeekable,
Other,
OutOfMemory,
PermissionDenied,
QuotaExceeded,
ReadOnlyFilesystem,
ResourceBusy,
StaleNetworkFileHandle,
StorageFull,
TimedOut,
TooManyLinks,
UnexpectedEof,
Unsupported,
WouldBlock,
WriteZero,
}
impl ErrorKind {
#[cfg(all(not(target_os = "uefi"), target_pointer_width = "64"))]
#[inline]
#[must_use]
#[track_caller]
pub(crate) const unsafe fn from_usize_unchecked(value: usize) -> Self {
match value {
_ if value == Self::AddrInUse.to_usize() => Self::AddrInUse,
_ if value == Self::AddrNotAvailable.to_usize() => Self::AddrNotAvailable,
_ if value == Self::AlreadyExists.to_usize() => Self::AlreadyExists,
_ if value == Self::ArgumentListTooLong.to_usize() => Self::ArgumentListTooLong,
_ if value == Self::BrokenPipe.to_usize() => Self::BrokenPipe,
_ if value == Self::ConnectionAborted.to_usize() => Self::ConnectionAborted,
_ if value == Self::ConnectionRefused.to_usize() => Self::ConnectionRefused,
_ if value == Self::ConnectionReset.to_usize() => Self::ConnectionReset,
_ if value == Self::CrossesDevices.to_usize() => Self::CrossesDevices,
_ if value == Self::Deadlock.to_usize() => Self::Deadlock,
_ if value == Self::DirectoryNotEmpty.to_usize() => Self::DirectoryNotEmpty,
_ if value == Self::ExecutableFileBusy.to_usize() => Self::ExecutableFileBusy,
_ if value == Self::FileTooLarge.to_usize() => Self::FileTooLarge,
_ if value == Self::HostUnreachable.to_usize() => Self::HostUnreachable,
_ if value == Self::Interrupted.to_usize() => Self::Interrupted,
_ if value == Self::InvalidData.to_usize() => Self::InvalidData,
_ if value == Self::InvalidFilename.to_usize() => Self::InvalidFilename,
_ if value == Self::InvalidInput.to_usize() => Self::InvalidInput,
_ if value == Self::IsADirectory.to_usize() => Self::IsADirectory,
_ if value == Self::NetworkDown.to_usize() => Self::NetworkDown,
_ if value == Self::NetworkUnreachable.to_usize() => Self::NetworkUnreachable,
_ if value == Self::NotADirectory.to_usize() => Self::NotADirectory,
_ if value == Self::NotConnected.to_usize() => Self::NotConnected,
_ if value == Self::NotFound.to_usize() => Self::NotFound,
_ if value == Self::NotSeekable.to_usize() => Self::NotSeekable,
_ if value == Self::OutOfMemory.to_usize() => Self::OutOfMemory,
_ if value == Self::PermissionDenied.to_usize() => Self::PermissionDenied,
_ if value == Self::QuotaExceeded.to_usize() => Self::QuotaExceeded,
_ if value == Self::ReadOnlyFilesystem.to_usize() => Self::ReadOnlyFilesystem,
_ if value == Self::ResourceBusy.to_usize() => Self::ResourceBusy,
_ if value == Self::StaleNetworkFileHandle.to_usize() => Self::StaleNetworkFileHandle,
_ if value == Self::StorageFull.to_usize() => Self::StorageFull,
_ if value == Self::TimedOut.to_usize() => Self::TimedOut,
_ if value == Self::TooManyLinks.to_usize() => Self::TooManyLinks,
_ if value == Self::UnexpectedEof.to_usize() => Self::UnexpectedEof,
_ if value == Self::Unsupported.to_usize() => Self::Unsupported,
_ if value == Self::WouldBlock.to_usize() => Self::WouldBlock,
_ if value == Self::WriteZero.to_usize() => Self::WriteZero,
_ => {
#[cfg(debug_assertions)]
panic!("cannot construct `ErrorKind` object from unknown `usize` value");
#[cfg(not(debug_assertions))]
unsafe { unreachable_unchecked() }
}
}
}
#[cfg(all(not(target_os = "uefi"), target_pointer_width = "64"))]
#[inline(always)]
#[must_use]
pub(crate) const fn to_usize(self) -> usize {
self as usize
}
}
impl Display for ErrorKind {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match *self {
Self::AddrInUse => {
write!(f, "address is already in use")
}
Self::AddrNotAvailable => {
write!(f, "address is not available")
}
Self::AlreadyExists => {
write!(f, "path already exists")
}
Self::ArgumentListTooLong => {
write!(f, "argument list is too long")
}
Self::BrokenPipe => {
write!(f, "pipe is broken")
}
Self::ConnectionAborted => {
write!(f, "connection has been aborted")
}
Self::ConnectionRefused => {
write!(f, "connection has been refused")
}
Self::ConnectionReset => {
write!(f, "connection has been reset")
}
Self::CrossesDevices => {
write!(f, "cross-device link")
}
Self::Deadlock => {
write!(f, "deadlock")
}
Self::DirectoryNotEmpty => {
write!(f, "directory is not empty")
}
Self::ExecutableFileBusy => {
write!(f, "executable file is busy")
}
Self::FileTooLarge => {
write!(f, "file is too large")
}
Self::HostUnreachable => {
write!(f, "host is unreachable")
}
Self::Interrupted => {
write!(f, "operation was interrupted")
}
Self::InvalidData => {
write!(f, "invalid data")
}
Self::InvalidFilename => {
write!(f, "invalid file name")
}
Self::InvalidInput => {
write!(f, "invalid input")
}
Self::IsADirectory => {
write!(f, "path is a directory")
}
Self::NetworkDown => {
write!(f, "network is down")
}
Self::NetworkUnreachable => {
write!(f, "network is unreachable")
}
Self::NotADirectory => {
write!(f, "path is not a directory")
}
Self::NotConnected => {
write!(f, "connection is missing")
}
Self::NotFound => {
write!(f, "path was not found")
}
Self::NotSeekable => {
write!(f, "file is unseekable")
}
Self::Other => {
write!(f, "unknown error")
}
Self::OutOfMemory => {
write!(f, "out of memory")
}
Self::PermissionDenied => {
write!(f, "permission was denied")
}
Self::QuotaExceeded => {
write!(f, "quota has been exceeded")
}
Self::ReadOnlyFilesystem => {
write!(f, "file system is read-only")
}
Self::ResourceBusy => {
write!(f, "resource is busy")
}
Self::StaleNetworkFileHandle=> {
write!(f, "network file handle is stale")
}
Self::StorageFull => {
write!(f, "storage is full")
}
Self::TimedOut => {
write!(f, "time out")
}
Self::TooManyLinks => {
write!(f, "too many links")
}
Self::UnexpectedEof => {
write!(f, "unexpected end of file")
}
Self::Unsupported => {
write!(f, "unsupported operation")
}
Self::WouldBlock => {
write!(f, "operation would block")
}
Self::WriteZero => {
write!(f, "zero write")
}
}
}
}
#[cfg(feature = "std")]
impl From<std::io::ErrorKind> for ErrorKind {
#[inline]
fn from(value: std::io::ErrorKind) -> Self {
match value {
std::io::ErrorKind::AddrInUse => Self::AddrInUse,
std::io::ErrorKind::AddrNotAvailable => Self::AddrNotAvailable,
std::io::ErrorKind::AlreadyExists => Self::AlreadyExists,
std::io::ErrorKind::ArgumentListTooLong => Self::ArgumentListTooLong,
std::io::ErrorKind::BrokenPipe => Self::BrokenPipe,
std::io::ErrorKind::ConnectionAborted => Self::ConnectionAborted,
std::io::ErrorKind::ConnectionRefused => Self::ConnectionRefused,
std::io::ErrorKind::ConnectionReset => Self::ConnectionReset,
std::io::ErrorKind::CrossesDevices => Self::CrossesDevices,
std::io::ErrorKind::Deadlock => Self::Deadlock,
std::io::ErrorKind::DirectoryNotEmpty => Self::DirectoryNotEmpty,
std::io::ErrorKind::ExecutableFileBusy => Self::ExecutableFileBusy,
std::io::ErrorKind::FileTooLarge => Self::FileTooLarge,
std::io::ErrorKind::HostUnreachable => Self::HostUnreachable,
std::io::ErrorKind::Interrupted => Self::Interrupted,
std::io::ErrorKind::InvalidData => Self::InvalidData,
std::io::ErrorKind::InvalidFilename => Self::InvalidFilename,
std::io::ErrorKind::InvalidInput => Self::InvalidInput,
std::io::ErrorKind::IsADirectory => Self::IsADirectory,
std::io::ErrorKind::NetworkDown => Self::NetworkDown,
std::io::ErrorKind::NetworkUnreachable => Self::NetworkUnreachable,
std::io::ErrorKind::NotADirectory => Self::NotADirectory,
std::io::ErrorKind::NotConnected => Self::NotConnected,
std::io::ErrorKind::NotFound => Self::NotFound,
std::io::ErrorKind::NotSeekable => Self::NotSeekable,
std::io::ErrorKind::OutOfMemory => Self::OutOfMemory,
std::io::ErrorKind::PermissionDenied => Self::PermissionDenied,
std::io::ErrorKind::QuotaExceeded => Self::QuotaExceeded,
std::io::ErrorKind::ReadOnlyFilesystem => Self::ReadOnlyFilesystem,
std::io::ErrorKind::ResourceBusy => Self::ResourceBusy,
std::io::ErrorKind::StaleNetworkFileHandle => Self::StaleNetworkFileHandle,
std::io::ErrorKind::StorageFull => Self::StorageFull,
std::io::ErrorKind::TimedOut => Self::TimedOut,
std::io::ErrorKind::TooManyLinks => Self::TooManyLinks,
std::io::ErrorKind::UnexpectedEof => Self::UnexpectedEof,
std::io::ErrorKind::Unsupported => Self::Unsupported,
std::io::ErrorKind::WouldBlock => Self::WouldBlock,
std::io::ErrorKind::WriteZero => Self::WriteZero,
| std::io::ErrorKind::Other
| _
=> Self::Other,
}
}
}
#[cfg(feature = "std")]
impl From<ErrorKind> for std::io::ErrorKind {
#[inline]
fn from(value: ErrorKind) -> Self {
match value {
ErrorKind::AddrInUse => Self::AddrInUse,
ErrorKind::AddrNotAvailable => Self::AddrNotAvailable,
ErrorKind::AlreadyExists => Self::AlreadyExists,
ErrorKind::ArgumentListTooLong => Self::ArgumentListTooLong,
ErrorKind::BrokenPipe => Self::BrokenPipe,
ErrorKind::ConnectionAborted => Self::ConnectionAborted,
ErrorKind::ConnectionRefused => Self::ConnectionRefused,
ErrorKind::ConnectionReset => Self::ConnectionReset,
ErrorKind::CrossesDevices => Self::CrossesDevices,
ErrorKind::Deadlock => Self::Deadlock,
ErrorKind::DirectoryNotEmpty => Self::DirectoryNotEmpty,
ErrorKind::ExecutableFileBusy => Self::ExecutableFileBusy,
ErrorKind::FileTooLarge => Self::FileTooLarge,
ErrorKind::HostUnreachable => Self::HostUnreachable,
ErrorKind::Interrupted => Self::Interrupted,
ErrorKind::InvalidData => Self::InvalidData,
ErrorKind::InvalidFilename => Self::InvalidFilename,
ErrorKind::InvalidInput => Self::InvalidInput,
ErrorKind::IsADirectory => Self::IsADirectory,
ErrorKind::NetworkDown => Self::NetworkDown,
ErrorKind::NetworkUnreachable => Self::NetworkUnreachable,
ErrorKind::NotADirectory => Self::NotADirectory,
ErrorKind::NotConnected => Self::NotConnected,
ErrorKind::NotFound => Self::NotFound,
ErrorKind::NotSeekable => Self::NotSeekable,
ErrorKind::Other => Self::Other,
ErrorKind::OutOfMemory => Self::OutOfMemory,
ErrorKind::PermissionDenied => Self::PermissionDenied,
ErrorKind::QuotaExceeded => Self::QuotaExceeded,
ErrorKind::ReadOnlyFilesystem => Self::ReadOnlyFilesystem,
ErrorKind::ResourceBusy => Self::ResourceBusy,
ErrorKind::StaleNetworkFileHandle => Self::StaleNetworkFileHandle,
ErrorKind::StorageFull => Self::StorageFull,
ErrorKind::TimedOut => Self::TimedOut,
ErrorKind::TooManyLinks => Self::TooManyLinks,
ErrorKind::UnexpectedEof => Self::UnexpectedEof,
ErrorKind::Unsupported => Self::Unsupported,
ErrorKind::WouldBlock => Self::WouldBlock,
ErrorKind::WriteZero => Self::WriteZero,
}
}
}