BytesCodec

Trait BytesCodec 

Source
pub trait BytesCodec {
    // Required methods
    fn to_bytes(self) -> Bytes;
    fn from_bytes(bytes: &Bytes) -> Self;
}
Expand description

A trait for converting types to and from Bytes.

This trait provides methods to convert a type into a Bytes object, as well as reconstruct the original type from a Bytes object.

§Examples

use alloy::primitives::Address;
use tycho_ethereum::BytesCodec;
use tycho_common::Bytes;

let address_value = Address::ZERO;
let bytes: Bytes = address_value.to_bytes(); // Converts Address to Bytes
let new_address = Address::from_bytes(&bytes);  // Converts Bytes back to Address

Required Methods§

Source

fn to_bytes(self) -> Bytes

Converts the current type into a Bytes object.

Source

fn from_bytes(bytes: &Bytes) -> Self

Reconstructs the type from a Bytes object.

§Arguments
  • bytes - The Bytes object to convert into the original type.
§Returns

The type that was converted from 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 BytesCodec for Address

Source§

fn to_bytes(self) -> Bytes

Converts Address to Bytes.

Source§

fn from_bytes(bytes: &Bytes) -> Self

Converts Bytes to Address.

§Panics

Will panic if the length of Bytes is not 20 (which is the size of an Address).

Source§

impl BytesCodec for B256

Source§

fn to_bytes(self) -> Bytes

Converts B256 to Bytes.

Source§

fn from_bytes(bytes: &Bytes) -> Self

Converts Bytes to B256.

§Panics

Will panic if the length of Bytes is not 32 (which is the size of a B256).

Source§

impl BytesCodec for U256

Source§

fn to_bytes(self) -> Bytes

Converts U256 to Bytes.

Source§

fn from_bytes(bytes: &Bytes) -> Self

Converts Bytes to U256 using big-endian.

§Panics

Will panic if the length of Bytes is larger than 32.

Implementors§