Skip to main content

Encoder

Struct Encoder 

Source
pub struct Encoder { /* private fields */ }
Expand description

A bounded, state-machine driven encoder.

The Encoder maintains a stack of open scopes to enforce structural strictness and automatically back-patch length headers.

§Structural Invariants

All write methods validate the operation against the current Scope. Returns an Error if the write violates the following rules:

  1. Map Scopes: Only Tag::Variant items may be written.
  2. ADT Scopes (Option, Result, Variant): Exactly one item must be written. Attempts to write >1 item or close the scope with 0 items will fail.
  3. Root Scope: The encoder must end in the Root scope to finalize bytes.

Implementations§

Source§

impl Encoder

Source

pub fn new() -> Self

Creates a new encoder with default capacity.

Source

pub fn into_bytes(self) -> Result<Vec<u8>>

Consumes the encoder and returns the final byte vector.

§Errors

Returns Error::ScopeStillOpen if the stack depth > 1.

Source

pub fn as_bytes(&self) -> Result<&[u8]>

Returns a view of the current buffer.

§Errors

Returns Error::ScopeStillOpen if the stack depth > 1.

Source

pub fn bool(&mut self, v: bool) -> Result<()>

Encodes a boolean value.

Source

pub fn u8(&mut self, v: u8) -> Result<()>

Encodes an unsigned 8-bit integer.

Source

pub fn s8(&mut self, v: i8) -> Result<()>

Encodes a signed 8-bit integer.

Source

pub fn u16(&mut self, v: u16) -> Result<()>

Encodes an unsigned 16-bit integer (LE).

Source

pub fn s16(&mut self, v: i16) -> Result<()>

Encodes a signed 16-bit integer (LE).

Source

pub fn u32(&mut self, v: u32) -> Result<()>

Encodes an unsigned 32-bit integer (LE).

Source

pub fn s32(&mut self, v: i32) -> Result<()>

Encodes a signed 32-bit integer (LE).

Source

pub fn u64(&mut self, v: u64) -> Result<()>

Encodes an unsigned 64-bit integer (LE).

Source

pub fn s64(&mut self, v: i64) -> Result<()>

Encodes a signed 64-bit integer (LE).

Source

pub fn f32(&mut self, v: f32) -> Result<()>

Encodes a 32-bit float (LE).

Source

pub fn f64(&mut self, v: f64) -> Result<()>

Encodes a 64-bit float (LE).

Source

pub fn char(&mut self, v: char) -> Result<()>

Encodes a char as u32 (LE).

Source

pub fn unit(&mut self) -> Result<()>

Encodes Unit ().

Source

pub fn option_none(&mut self) -> Result<()>

Encodes Option::None.

Source

pub fn str(&mut self, v: &str) -> Result<()>

Encodes a UTF-8 string blob.

Source

pub fn bytes(&mut self, v: &[u8]) -> Result<()>

Encodes a raw byte blob.

Source

pub fn append_raw(&mut self, v: &[u8]) -> Result<()>

Appends pre-encoded neopack bytes directly to the buffer.

This is used to inject already-encoded data (like a pre-encoded list of values) into the stream without re-encoding. The caller must ensure the bytes are valid neopack-encoded data.

Source

pub fn list_begin(&mut self) -> Result<()>

Begins a List container.

§Invariants
  • Must be closed via list_end().
  • Allows any number of items.
Source

pub fn list_end(&mut self) -> Result<()>

Ends a List container.

Source

pub fn map_begin(&mut self) -> Result<()>

Begins a Map container.

§Invariants
  • Must be closed via map_end().
  • Strict: Only variant_begin() (Key/Value pair) is allowed as a direct child.
Source

pub fn map_end(&mut self) -> Result<()>

Ends a Map container.

Source

pub fn option_some_begin(&mut self) -> Result<()>

Begins an Option::Some container.

§Invariants
  • Must be closed via option_some_end().
  • Strict: Requires exactly one item to be written.
Source

pub fn option_some_end(&mut self) -> Result<()>

Ends an Option::Some container.

Source

pub fn result_ok_begin(&mut self) -> Result<()>

Begins a Result::Ok container.

§Invariants
  • Must be closed via result_ok_end().
  • Strict: Requires exactly one item to be written.
Source

pub fn result_ok_end(&mut self) -> Result<()>

Ends a Result::Ok container.

Source

pub fn result_err_begin(&mut self) -> Result<()>

Begins a Result::Err container.

§Invariants
  • Must be closed via result_err_end().
  • Strict: Requires exactly one item to be written.
Source

pub fn result_err_end(&mut self) -> Result<()>

Ends a Result::Err container.

Source

pub fn variant_begin(&mut self, name: &str) -> Result<()>

Begins a Variant (Named Payload).

Encodes the name string immediately.

§Invariants
  • Must be closed via variant_end().
  • Strict: Requires exactly one item (the payload) to be written after this call.
Source

pub fn variant_end(&mut self) -> Result<()>

Ends a Variant.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.