/// Bit conversion operations.
pub trait BitConversionOps {
/// Converts an integer from big endian to the target's endianness.
///
/// On big endian this is a no-op. On little endian the bytes are swapped.
fn from_be(x: Self) -> Self;
/// Converts an integer from little endian to the target's endianness.
///
/// On little endian this is a no-op. On big endian the bytes are swapped.
fn from_le(x: Self) -> Self;
/// Converts `self` to big endian from the target's endianness.
///
/// On big endian this is a no-op. On little endian the bytes are swapped.
fn to_be(self) -> Self;
/// Converts `self` to little endian from the target's endianness.
///
/// On little endian this is a no-op. On big endian the bytes are swapped.
fn to_le(self) -> Self;
}
/// Implements [`BitConversionOps`].
macro_rules! impl_bit_conversion {
($($t:ty),*) => {
$(impl BitConversionOps for $t {
#[inline] fn from_be(x: Self) -> Self { Self::from_be(x) }
#[inline] fn from_le(x: Self) -> Self { Self::from_le(x) }
#[inline] fn to_be(self) -> Self { self.to_be() }
#[inline] fn to_le(self) -> Self { self.to_le() }
})*
};
}
impl_bit_conversion!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);