#![deny(missing_docs)]
#[allow(unused_imports)]
#[macro_use]
extern crate endian_trait_derive;
pub use endian_trait_derive::*;
pub trait Endian {
fn to_be(self) -> Self;
fn to_le(self) -> Self;
fn from_be(self) -> Self;
fn from_le(self) -> Self;
}
macro_rules! implendian {
( $( $t:tt ),* ) => { $(
impl Endian for $t {
#[inline(always)]
fn from_be(self) -> Self {
$t::from_be(self)
}
#[inline(always)]
fn from_le(self) -> Self {
$t::from_le(self)
}
#[inline(always)]
fn to_be(self) -> Self {
$t::to_be(self)
}
#[inline(always)]
fn to_le(self) -> Self {
$t::to_le(self)
}
}
)* };
}
macro_rules! implendian_f {
( $( $t:tt ),* ) => { $(
impl Endian for $t {
fn from_be(self) -> Self {
Self::from_bits(self.to_bits().from_be())
}
fn from_le(self) -> Self {
Self::from_bits(self.to_bits().from_le())
}
fn to_be(self) -> Self {
Self::from_bits(self.to_bits().to_be())
}
fn to_le(self) -> Self {
Self::from_bits(self.to_bits().to_le())
}
}
)* };
}
impl Endian for bool {
fn from_be(self) -> Self { self }
fn from_le(self) -> Self { self }
fn to_be(self) -> Self { self }
fn to_le(self) -> Self { self }
}
impl Endian for char {
fn from_be(self) -> Self {
let flip: u32 = (self as u32).from_be();
if flip > ::std::char::MAX as u32 {
panic!("A `char` cannot have a value of {:X}", flip);
}
unsafe { ::std::mem::transmute(flip) }
}
fn from_le(self) -> Self {
let flip: u32 = (self as u32).from_le();
if flip > ::std::char::MAX as u32 {
panic!("A `char` cannot have a value of {:X}", flip);
}
unsafe { ::std::mem::transmute(flip) }
}
fn to_be(self) -> Self {
unsafe { ::std::mem::transmute((self as u32).to_be()) }
}
fn to_le(self) -> Self {
unsafe { ::std::mem::transmute((self as u32).to_le()) }
}
}
implendian!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128);
implendian_f!(f32, f64);
#[cfg(feature = "arrays")]
mod arrays;
mod slices;