pub enum Endian {
Native,
Swapped,
Little,
Big,
}
Expand description
Defines two types of endianness: relative endian (Native or Swapped) and absolute endian (Little or Big).
Note: In this crate, the term encast
means decoding a number of
bytes to one or more values, the term decast
means encoding one
or more variables to a number of bytes, and the term endian-flip
means flipping the endianness of value(s).
Description
It defines the following two types of endianness.
Relative endinan is useful when encoding or decoding binaries whose endiannesses are specified by some magic numbers indicating whether native-endian or swapped-endian (e.g., Mach-o, PcapFile). Absolute endian is useful when encoding or decoding binaries whose endiannesses are explicitly specified in some fields or in their specification as little-endian or big-endian (e.g., ELF, TCP/IP).
For convenience, their short aliases are exported as
NE
, SE
, LE
and BE
, respectively.
The purpose of enum Endian
is to specify the endianness of the
bytes to be encast
ed or decast
ed in the arguments of methods.
Example
Enum Endian
has four methods. Here are examples of two of them.
use castflip::Endian;
let abs_endian = Endian::Native.absolute();
let abs_name = abs_endian.name();
if cfg!(target_endian = "little") {
assert_eq!(abs_endian, Endian::Little);
assert_eq!(abs_name, "Little");
} else if cfg!(target_endian = "big") {
assert_eq!(abs_endian, Endian::Big);
assert_eq!(abs_name, "Big");
}
Variants
Native
Native-Endian.
- The byte-order is the same as the one on the target system.
- Values can be processed without byte-swapping on the target system.
- Its antonym is
Swapped
.
Swapped
Swapped-Endian.
- The byte-order is different from the one on the target system.
- Values needs to be byte-swapped before and after processing them on the target system.
- Its antonym is
Native
.
Little
Little-Endian.
- Values are stored as the least significant byte comes first.
- The need for byte-swapping depends on the endianness of the target system.
- Its antonym is
Big
.
Big
Big-Endian.
- Values are stored as the most significant byte comes first.
- The need for byte-swapping depends on the endianness of the target system.
- Its antonym is
Little
.
Implementations
sourceimpl Endian
impl Endian
sourcepub fn relative(&self) -> Endian
pub fn relative(&self) -> Endian
Returns the corresponding relative endianness.
use castflip::Endian;
// Relative endian
assert_eq!(Endian::Native.relative(), Endian::Native);
assert_eq!(Endian::Swapped.relative(), Endian::Swapped);
// Absolute endian
if cfg!(target_endian = "little") {
assert_eq!(Endian::Little.relative(), Endian::Native);
assert_eq!(Endian::Big.relative(), Endian::Swapped);
} else if cfg!(target_endian = "big") {
assert_eq!(Endian::Little.relative(), Endian::Swapped);
assert_eq!(Endian::Big.relative(), Endian::Native);
}
sourcepub fn absolute(&self) -> Endian
pub fn absolute(&self) -> Endian
Returns the corresponding absolute endianness.
use castflip::Endian;
// Relative endian
if cfg!(target_endian = "little") {
assert_eq!(Endian::Native.absolute(), Endian::Little);
assert_eq!(Endian::Swapped.absolute(), Endian::Big);
} else if cfg!(target_endian = "big") {
assert_eq!(Endian::Native.absolute(), Endian::Big);
assert_eq!(Endian::Swapped.absolute(), Endian::Little);
}
// Absolute endian
assert_eq!(Endian::Little.absolute(), Endian::Little);
assert_eq!(Endian::Big.absolute(), Endian::Big);
sourcepub fn need_swap(self) -> bool
pub fn need_swap(self) -> bool
Returns true
if the endianness (self) is different from the
one on the target system.
use castflip::Endian;
// Relative endian
assert_eq!(Endian::Native.need_swap(), false);
assert_eq!(Endian::Swapped.need_swap(), true);
// Absolute endian
if cfg!(target_endian = "little") {
assert_eq!(Endian::Little.need_swap(), false);
assert_eq!(Endian::Big.need_swap(), true);
} else if cfg!(target_endian = "big") {
assert_eq!(Endian::Little.need_swap(), true);
assert_eq!(Endian::Big.need_swap(), false);
}
Trait Implementations
impl Copy for Endian
impl Eq for Endian
impl StructuralEq for Endian
impl StructuralPartialEq for Endian
Auto Trait Implementations
impl RefUnwindSafe for Endian
impl Send for Endian
impl Sync for Endian
impl Unpin for Endian
impl UnwindSafe for Endian
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more