Skip to main content

Representable

Trait Representable 

Source
pub trait Representable {
    type Representation: ByteArray;

    // Required methods
    fn from_le_bytes(bytes: &Self::Representation) -> Self;
    fn from_be_bytes(bytes: &Self::Representation) -> Self;
    fn to_le_bytes(&self) -> Self::Representation;
    fn to_be_bytes(&self) -> Self::Representation;
}
Expand description

A trait for types that can be represented as a fixed-size byte array.

This trait is implemented for all primitive integer types (u8-u128, i8-i128) and floating-point types (f32, f64). It provides methods for converting between the type and its byte representation in both little-endian and big-endian formats.

§Examples

use futures_byteorder::Representable;

let value: u16 = 0x1234;
let le_bytes = value.to_le_bytes();
let be_bytes = value.to_be_bytes();

assert_eq!(le_bytes, [0x34, 0x12]);
assert_eq!(be_bytes, [0x12, 0x34]);

assert_eq!(u16::from_le_bytes(le_bytes), value);
assert_eq!(u16::from_be_bytes(be_bytes), value);

Required Associated Types§

Source

type Representation: ByteArray

The byte array type that represents this type.

For example, u16::Representation is [u8; 2], and u64::Representation is [u8; 8].

Required Methods§

Source

fn from_le_bytes(bytes: &Self::Representation) -> Self

Creates a value from its little-endian byte representation.

Source

fn from_be_bytes(bytes: &Self::Representation) -> Self

Creates a value from its big-endian byte representation

Source

fn to_le_bytes(&self) -> Self::Representation

Returns the little-endian byte representation of this value.

Source

fn to_be_bytes(&self) -> Self::Representation

Returns the big-endian byte representation of this value.

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.

Implementations on Foreign Types§

Source§

impl Representable for f32

Source§

impl Representable for f64

Source§

impl Representable for i8

Source§

impl Representable for i16

Source§

impl Representable for i32

Source§

impl Representable for i64

Source§

impl Representable for i128

Source§

impl Representable for u8

Source§

impl Representable for u16

Source§

impl Representable for u32

Source§

impl Representable for u64

Source§

impl Representable for u128

Implementors§