#![allow(unexpected_cfgs)]
use core::{fmt, num};
#[cfg(feature = "nightly")]
#[repr(transparent)]
#[rustc_layout_scalar_valid_range_start(0x0000_0001)]
#[rustc_layout_scalar_valid_range_end(0x7fff_ffff)]
#[rustc_nonnull_optimization_guaranteed]
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Errno(i32);
#[cfg(not(feature = "nightly"))]
#[repr(transparent)]
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Errno(num::NonZeroI32);
impl Errno {
#[inline]
pub const fn new(errno: i32) -> Option<Self> {
if errno <= 0 || 0xFFF < errno {
return errno_out_of_range();
}
#[cfg(feature = "nightly")]
return Some(unsafe { Self(errno) });
#[cfg(not(feature = "nightly"))]
Some(Self(unsafe { num::NonZeroI32::new_unchecked(errno) }))
}
#[inline(always)]
pub const unsafe fn new_unchecked(errno: i32) -> Self {
#[cfg(feature = "nightly")]
return Self(errno);
#[cfg(not(feature = "nightly"))]
Self(num::NonZeroI32::new_unchecked(errno))
}
#[inline(always)]
pub const fn get(self) -> i32 {
#[cfg(feature = "nightly")]
return self.0;
#[cfg(not(feature = "nightly"))]
self.0.get()
}
#[inline(always)]
pub const fn get_nonzero(self) -> num::NonZeroI32 {
#[cfg(feature = "nightly")]
return unsafe { num::NonZeroI32::new_unchecked(self.0) };
#[cfg(not(feature = "nightly"))]
self.0
}
}
#[cold]
#[inline]
const fn errno_out_of_range() -> Option<Errno> {
None
}
macro_rules! errno_constants {
(
$( $(#[$meta:meta])* $name:ident = $value:literal ),+,
{ $( $(#[$meta1:meta])* $name1:ident = $value1:ident, )+ },
) => {
$(
$(#[$meta])*
pub const $name: super::Errno = unsafe {
super::Errno::new_unchecked($value)
};
)+
$(
$(#[$meta1])*
pub const $name1: super::Errno = $value1;
)*
#[inline]
pub(super) const fn err_name(err: super::Errno) -> Option<&'static str> {
match err.get() {
$(
$value => Some(stringify!($name)),
)+
_ => None,
}
}
}
}
#[cfg(any(
target_arch = "arm",
target_arch = "aarch64",
target_arch = "m68k",
target_arch = "powerpc",
target_arch = "powerpc64",
target_arch = "riscv32",
target_arch = "riscv64",
target_arch = "s390x",
target_arch = "x86",
target_arch = "x86_64",
))]
#[path = "errno/generic.rs"]
mod arch;
#[cfg(target_arch = "alpha")]
#[path = "errno/alpha.rs"]
mod arch;
#[cfg(any(target_arch = "mips", target_arch = "mips64"))]
#[path = "errno/mips.rs"]
mod arch;
#[cfg(target_arch = "parisc")]
#[path = "errno/parisc.rs"]
mod arch;
#[cfg(any(target_arch = "sparc", target_arch = "sparc64"))]
#[path = "errno/sparc.rs"]
mod arch;
pub use arch::*;
impl fmt::Debug for Errno {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match arch::err_name(*self) {
Some(name) => f.write_str(name),
_ => f.debug_tuple("Error").field(&self.get()).finish(),
}
}
}