Skip to main content

Bytes

Trait Bytes 

Source
pub trait Bytes: Sized {
    type Array: AsRef<[u8]>;

    // Required methods
    fn to_bytes(&self) -> Self::Array;
    fn from_bytes(bytes: &[u8]) -> Result<Self>;
}
Expand description

Trait for types that can be serialized to/from bytes with explicit byte order.

This trait uses LITTLE-ENDIAN byte order for all numeric types, making the data portable across different endianness systems (x86, ARM, etc.). This is the key difference from ZeroCopyVec, which uses native byte order and is not portable.

Use this trait when:

  • You need cross-platform compatibility
  • You’re sharing data between systems with different endianness
  • You need custom serialization logic

For maximum performance on a single system, use ZeroCopyVec instead.

Required Associated Types§

Source

type Array: AsRef<[u8]>

The byte array type returned by to_bytes. For fixed-size types, this is [u8; N] where N is the size of the type.

Required Methods§

Source

fn to_bytes(&self) -> Self::Array

Serializes this value to bytes.

For numeric types, this uses little-endian byte order (via to_le_bytes).

Source

fn from_bytes(bytes: &[u8]) -> Result<Self>

Deserializes a value from bytes.

For numeric types, this uses little-endian byte order (via from_le_bytes).

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 Bytes for f32

Source§

type Array = [u8; 4]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for f64

Source§

type Array = [u8; 8]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for i8

Source§

type Array = [u8; 1]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for i16

Source§

type Array = [u8; 2]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for i32

Source§

type Array = [u8; 4]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for i64

Source§

type Array = [u8; 8]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for i128

Source§

type Array = [u8; 16]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for isize

Source§

type Array = [u8; 8]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for u8

Source§

type Array = [u8; 1]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for u16

Source§

type Array = [u8; 2]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for u32

Source§

type Array = [u8; 4]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for u64

Source§

type Array = [u8; 8]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for u128

Source§

type Array = [u8; 16]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for usize

Source§

type Array = [u8; 8]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 1]

Source§

type Array = [u8; 1]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 2]

Source§

type Array = [u8; 2]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 3]

Source§

type Array = [u8; 3]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 4]

Source§

type Array = [u8; 4]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 5]

Source§

type Array = [u8; 5]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 6]

Source§

type Array = [u8; 6]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 7]

Source§

type Array = [u8; 7]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 8]

Source§

type Array = [u8; 8]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 9]

Source§

type Array = [u8; 9]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 10]

Source§

type Array = [u8; 10]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 11]

Source§

type Array = [u8; 11]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 12]

Source§

type Array = [u8; 12]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 13]

Source§

type Array = [u8; 13]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 14]

Source§

type Array = [u8; 14]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 15]

Source§

type Array = [u8; 15]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 16]

Source§

type Array = [u8; 16]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 17]

Source§

type Array = [u8; 17]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 18]

Source§

type Array = [u8; 18]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 19]

Source§

type Array = [u8; 19]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 20]

Source§

type Array = [u8; 20]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 21]

Source§

type Array = [u8; 21]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 22]

Source§

type Array = [u8; 22]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 23]

Source§

type Array = [u8; 23]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 24]

Source§

type Array = [u8; 24]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 25]

Source§

type Array = [u8; 25]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 26]

Source§

type Array = [u8; 26]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 27]

Source§

type Array = [u8; 27]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 28]

Source§

type Array = [u8; 28]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 29]

Source§

type Array = [u8; 29]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 30]

Source§

type Array = [u8; 30]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 31]

Source§

type Array = [u8; 31]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 32]

Source§

type Array = [u8; 32]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 33]

Source§

type Array = [u8; 33]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 64]

Source§

type Array = [u8; 64]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Source§

impl Bytes for [u8; 65]

Source§

type Array = [u8; 65]

Source§

fn to_bytes(&self) -> Self::Array

Source§

fn from_bytes(bytes: &[u8]) -> Result<Self>

Implementors§