use crate::include_doc;
#[doc = include_doc!("enum_endian.md")]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Endian {
Native,
Swapped,
Little,
Big,
}
impl Endian {
pub fn relative(&self) -> Self {
if cfg!(target_endian = "little") {
match self {
Self::Native => Self::Native,
Self::Swapped => Self::Swapped,
Self::Little => Self::Native,
Self::Big => Self::Swapped,
}
} else if cfg!(target_endian = "big") {
match self {
Self::Native => Self::Native,
Self::Swapped => Self::Swapped,
Self::Little => Self::Swapped,
Self::Big => Self::Native,
}
} else {
panic!("The endian of the target system is not supported.");
}
}
pub fn absolute(&self) -> Self {
if cfg!(target_endian = "little") {
match self {
Self::Native => Self::Little,
Self::Swapped => Self::Big,
Self::Little => Self::Little,
Self::Big => Self::Big,
}
} else if cfg!(target_endian = "big") {
match self {
Self::Native => Self::Big,
Self::Swapped => Self::Little,
Self::Little => Self::Little,
Self::Big => Self::Big,
}
} else {
panic!("The endian of the target system is not supported.");
}
}
#[inline]
pub fn need_swap(self) -> bool {
#[allow(clippy::collapsible_else_if)]
if self == Self::Swapped {
true
} else {
if cfg!(target_endian = "little") {
self == Self::Big
} else if cfg!(target_endian = "big") {
self == Self::Little
} else {
panic!("The endian of the target system is not supported.");
}
}
}
pub fn name(self) -> &'static str {
match self {
Self::Native => "Native",
Self::Swapped => "Swapped",
Self::Little => "Little",
Self::Big => "Big",
}
}
}
pub const NE: Endian = Endian::Native;
pub const SE: Endian = Endian::Swapped;
pub const LE: Endian = Endian::Little;
pub const BE: Endian = Endian::Big;