Skip to main content

TailCodec

Trait TailCodec 

Source
pub trait TailCodec: Sized {
    const MAX_ENCODED_LEN: usize;

    // Required methods
    fn encode(&self, out: &mut [u8]) -> Result<usize, ProgramError>;
    fn decode(input: &[u8]) -> Result<(Self, usize), ProgramError>;
}
Expand description

Canonical serializer for dynamic-tail payloads.

Implementations encode into a caller-provided buffer and decode from a caller-provided slice, returning the byte count consumed in both directions. Byte counts drive the length-prefix handling inside #[hopper::state]’s generated tail accessors. the encoding must be deterministic and bidirectional.

Required Associated Constants§

Source

const MAX_ENCODED_LEN: usize

Upper bound on the encoded size. Used by generated helpers to verify the account has enough room before invoking encode. Implementors should pick the smallest valid bound. Hopper uses this to pre-size reallocs.

Required Methods§

Source

fn encode(&self, out: &mut [u8]) -> Result<usize, ProgramError>

Serialize self into out. Returns the number of bytes written (always <= MAX_ENCODED_LEN). Fails with AccountDataTooSmall when out.len() < encoded_len.

Source

fn decode(input: &[u8]) -> Result<(Self, usize), ProgramError>

Deserialize from input. Returns (value, bytes_consumed). Fails with InvalidAccountData on malformed encoding.

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

Source§

const MAX_ENCODED_LEN: usize = 1

Source§

fn encode(&self, out: &mut [u8]) -> Result<usize, ProgramError>

Source§

fn decode(input: &[u8]) -> Result<(Self, usize), ProgramError>

Source§

impl TailCodec for i16

Source§

const MAX_ENCODED_LEN: usize = 2

Source§

fn encode(&self, out: &mut [u8]) -> Result<usize, ProgramError>

Source§

fn decode(input: &[u8]) -> Result<(Self, usize), ProgramError>

Source§

impl TailCodec for i32

Source§

const MAX_ENCODED_LEN: usize = 4

Source§

fn encode(&self, out: &mut [u8]) -> Result<usize, ProgramError>

Source§

fn decode(input: &[u8]) -> Result<(Self, usize), ProgramError>

Source§

impl TailCodec for i64

Source§

const MAX_ENCODED_LEN: usize = 8

Source§

fn encode(&self, out: &mut [u8]) -> Result<usize, ProgramError>

Source§

fn decode(input: &[u8]) -> Result<(Self, usize), ProgramError>

Source§

impl TailCodec for i128

Source§

const MAX_ENCODED_LEN: usize = 16

Source§

fn encode(&self, out: &mut [u8]) -> Result<usize, ProgramError>

Source§

fn decode(input: &[u8]) -> Result<(Self, usize), ProgramError>

Source§

impl TailCodec for u8

Source§

const MAX_ENCODED_LEN: usize = 1

Source§

fn encode(&self, out: &mut [u8]) -> Result<usize, ProgramError>

Source§

fn decode(input: &[u8]) -> Result<(Self, usize), ProgramError>

Source§

impl TailCodec for u16

Source§

const MAX_ENCODED_LEN: usize = 2

Source§

fn encode(&self, out: &mut [u8]) -> Result<usize, ProgramError>

Source§

fn decode(input: &[u8]) -> Result<(Self, usize), ProgramError>

Source§

impl TailCodec for u32

Source§

const MAX_ENCODED_LEN: usize = 4

Source§

fn encode(&self, out: &mut [u8]) -> Result<usize, ProgramError>

Source§

fn decode(input: &[u8]) -> Result<(Self, usize), ProgramError>

Source§

impl TailCodec for u64

Source§

const MAX_ENCODED_LEN: usize = 8

Source§

fn encode(&self, out: &mut [u8]) -> Result<usize, ProgramError>

Source§

fn decode(input: &[u8]) -> Result<(Self, usize), ProgramError>

Source§

impl TailCodec for u128

Source§

const MAX_ENCODED_LEN: usize = 16

Source§

fn encode(&self, out: &mut [u8]) -> Result<usize, ProgramError>

Source§

fn decode(input: &[u8]) -> Result<(Self, usize), ProgramError>

Source§

impl<T: TailCodec> TailCodec for Option<T>

Source§

const MAX_ENCODED_LEN: usize

Source§

fn encode(&self, out: &mut [u8]) -> Result<usize, ProgramError>

Source§

fn decode(input: &[u8]) -> Result<(Self, usize), ProgramError>

Source§

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

Source§

const MAX_ENCODED_LEN: usize = N

Source§

fn encode(&self, out: &mut [u8]) -> Result<usize, ProgramError>

Source§

fn decode(input: &[u8]) -> Result<(Self, usize), ProgramError>

Implementors§