ToBytes

Trait ToBytes 

Source
pub trait ToBytes: Sized {
    type Bytes: ByteArray;

    const PREFERS_LE: bool = true;

    // Required methods
    fn to_le_bytes(self) -> Self::Bytes;
    fn to_be_bytes(self) -> Self::Bytes;

    // Provided methods
    fn to_bytes(self) -> Self::Bytes { ... }
    fn to_ne_bytes(self) -> Self::Bytes { ... }
    fn write_packed<W: Write>(self, writer: &mut W) -> Result<()> { ... }
}
Expand description

Pack this type into a stack byte array of a fixed size.

Most times, the method to_bytes should be used, as it ensures consistency by respecting the byte order set by the PREFERS_LE associated constant.

Provided Associated Constants§

Source

const PREFERS_LE: bool = true

Is it preferred to represent this type as bytes in the little endian order?

Required Associated Types§

Source

type Bytes: ByteArray

A byte array which can store a packed representation of this type.

Required Methods§

Source

fn to_le_bytes(self) -> Self::Bytes

Return the memory representation of this type as a byte array in little endian byte order.

Source

fn to_be_bytes(self) -> Self::Bytes

Return the memory representation of this type as a byte array in big endian byte order.

Provided Methods§

Source

fn to_bytes(self) -> Self::Bytes

Return the memory representation of this type as a byte array in the preferred byte order, set in the associated constant PREFERS_LE.

Source

fn to_ne_bytes(self) -> Self::Bytes

Return the memory representation of this type as a byte array in native endian byte order.

As the target platform’s native endianness is used, portable code likely wants to use to_le_bytes or to_be_bytes, as appropriate instead.

Source

fn write_packed<W: Write>(self, writer: &mut W) -> Result<()>

Write the value of this type to a writer in preferred byte order, set by the associated constant PREFERS_LE.

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 ToBytes for bool

Source§

type Bytes = [u8; 1]

Source§

fn to_le_bytes(self) -> Self::Bytes

Source§

fn to_be_bytes(self) -> Self::Bytes

Source§

impl ToBytes for char

Source§

type Bytes = [u8; 4]

Source§

fn to_le_bytes(self) -> Self::Bytes

Source§

fn to_be_bytes(self) -> Self::Bytes

Source§

impl ToBytes for f32

Source§

type Bytes = [u8; 4]

Source§

fn to_le_bytes(self) -> Self::Bytes

Source§

fn to_be_bytes(self) -> Self::Bytes

Source§

impl ToBytes for f64

Source§

type Bytes = [u8; 8]

Source§

fn to_le_bytes(self) -> Self::Bytes

Source§

fn to_be_bytes(self) -> Self::Bytes

Source§

impl ToBytes for i8

Source§

type Bytes = [u8; 1]

Source§

fn to_le_bytes(self) -> Self::Bytes

Source§

fn to_be_bytes(self) -> Self::Bytes

Source§

impl ToBytes for i16

Source§

type Bytes = [u8; 2]

Source§

fn to_le_bytes(self) -> Self::Bytes

Source§

fn to_be_bytes(self) -> Self::Bytes

Source§

impl ToBytes for i32

Source§

type Bytes = [u8; 4]

Source§

fn to_le_bytes(self) -> Self::Bytes

Source§

fn to_be_bytes(self) -> Self::Bytes

Source§

impl ToBytes for i64

Source§

type Bytes = [u8; 8]

Source§

fn to_le_bytes(self) -> Self::Bytes

Source§

fn to_be_bytes(self) -> Self::Bytes

Source§

impl ToBytes for i128

Source§

type Bytes = [u8; 16]

Source§

fn to_le_bytes(self) -> Self::Bytes

Source§

fn to_be_bytes(self) -> Self::Bytes

Source§

impl ToBytes for isize

Source§

type Bytes = [u8; 8]

Source§

fn to_le_bytes(self) -> Self::Bytes

Source§

fn to_be_bytes(self) -> Self::Bytes

Source§

impl ToBytes for u8

Source§

type Bytes = [u8; 1]

Source§

fn to_le_bytes(self) -> Self::Bytes

Source§

fn to_be_bytes(self) -> Self::Bytes

Source§

impl ToBytes for u16

Source§

type Bytes = [u8; 2]

Source§

fn to_le_bytes(self) -> Self::Bytes

Source§

fn to_be_bytes(self) -> Self::Bytes

Source§

impl ToBytes for u32

Source§

type Bytes = [u8; 4]

Source§

fn to_le_bytes(self) -> Self::Bytes

Source§

fn to_be_bytes(self) -> Self::Bytes

Source§

impl ToBytes for u64

Source§

type Bytes = [u8; 8]

Source§

fn to_le_bytes(self) -> Self::Bytes

Source§

fn to_be_bytes(self) -> Self::Bytes

Source§

impl ToBytes for u128

Source§

type Bytes = [u8; 16]

Source§

fn to_le_bytes(self) -> Self::Bytes

Source§

fn to_be_bytes(self) -> Self::Bytes

Source§

impl ToBytes for ()

Source§

type Bytes = [u8; 0]

Source§

fn to_le_bytes(self) -> Self::Bytes

Source§

fn to_be_bytes(self) -> Self::Bytes

Source§

impl ToBytes for usize

Source§

type Bytes = [u8; 8]

Source§

fn to_le_bytes(self) -> Self::Bytes

Source§

fn to_be_bytes(self) -> Self::Bytes

Source§

impl<const N: usize> ToBytes for [u8; N]

Source§

type Bytes = [u8; N]

Source§

fn to_le_bytes(self) -> Self::Bytes

Source§

fn to_be_bytes(self) -> Self::Bytes

Implementors§