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 encasted or decasted 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

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);
}

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);

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);
}

Returns the name of the endianness (self).

use castflip::Endian;

assert_eq!(Endian::Native.name(), "Native");
assert_eq!(Endian::Swapped.name(), "Swapped");
assert_eq!(Endian::Little.name(), "Little");
assert_eq!(Endian::Big.name(), "Big");

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.