Trait packbytes::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<usize> { ... }
}
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.

Required Associated Types§

source

type Bytes: ByteArray

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

Provided Associated Constants§

source

const PREFERS_LE: bool = true

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

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<usize>

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

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl ToBytes for bool

§

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

§

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

§

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

§

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

§

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

§

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

§

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

§

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

§

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

§

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

§

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

§

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

§

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

§

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

§

type Bytes = [u8; 16]

source§

fn to_le_bytes(self) -> Self::Bytes

source§

fn to_be_bytes(self) -> Self::Bytes

source§

impl ToBytes for ()

§

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

§

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]

§

type Bytes = [u8; N]

source§

fn to_le_bytes(self) -> Self::Bytes

source§

fn to_be_bytes(self) -> Self::Bytes

Implementors§