Skip to main content

Endianness

Trait Endianness 

Source
pub trait Endianness: Sealed {
    // Required methods
    fn from_representable<R: Representable>(
        representable: &R,
    ) -> R::Representation;
    fn to_representable<R: Representable>(
        representation: &R::Representation,
    ) -> R;
}
Expand description

A trait defining byte order conversion behavior.

This trait is implemented by BigEndian, LittleEndian, and their type aliases to define how numeric values should be converted to and from byte representations.

§Examples

use futures_byteorder::{Endianness, BigEndian, Representable};

let value: u16 = 0x1234;
let bytes = BigEndian::from_representable(&value);
assert_eq!(bytes, [0x12, 0x34]);

let decoded = BigEndian::to_representable::<u16>(&bytes);
assert_eq!(decoded, 0x1234);

Required Methods§

Source

fn from_representable<R: Representable>(representable: &R) -> R::Representation

Converts a representable value to its byte representation using this endianness.

§Examples
use futures_byteorder::{Endianness, LittleEndian};

let value: u32 = 0x12345678;
let bytes = LittleEndian::from_representable(&value);
assert_eq!(bytes, [0x78, 0x56, 0x34, 0x12]);
Source

fn to_representable<R: Representable>(representation: &R::Representation) -> R

Converts a byte representation to a representable value using this endianness.

§Examples
use futures_byteorder::{Endianness, BigEndian};

let bytes = [0x12, 0x34, 0x56, 0x78];
let value = BigEndian::to_representable::<u32>(&bytes);
assert_eq!(value, 0x12345678);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§