BinaryEncoder

Struct BinaryEncoder 

Source
pub struct BinaryEncoder<W: Write> { /* private fields */ }
Expand description

Binary encoder with versioning support

Implementations§

Source§

impl<W: Write> BinaryEncoder<W>

Source

pub fn new(writer: W) -> Self

Create a new encoder with the current protocol version

Source

pub fn with_version(writer: W, version: u8) -> Self

Create an encoder with a specific protocol version

Source

pub fn write_header(&mut self) -> ChieResult<()>

Write the message header (magic bytes + version)

§Errors

Returns error if writing fails

Source

pub fn write_u8(&mut self, value: u8) -> ChieResult<()>

Write a u8 value

§Errors

Returns error if writing fails

Source

pub fn write_u32(&mut self, value: u32) -> ChieResult<()>

Write a u32 value (little-endian)

§Errors

Returns error if writing fails

Source

pub fn write_u64(&mut self, value: u64) -> ChieResult<()>

Write a u64 value (little-endian)

§Errors

Returns error if writing fails

Source

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

Write a variable-length byte array (length prefix + data)

§Errors

Returns error if writing fails

Source

pub fn write_string(&mut self, s: &str) -> ChieResult<()>

Write a string (UTF-8 encoded with length prefix)

§Errors

Returns error if writing fails

Source

pub fn write_bool(&mut self, value: bool) -> ChieResult<()>

Write a boolean as a single byte

§Errors

Returns error if writing fails

Source

pub fn write_checksum(&mut self, data: &[u8]) -> ChieResult<()>

Write a CRC32 checksum for the given data

§Errors

Returns error if writing fails

Source

pub fn get_mut(&mut self) -> &mut W

Get a mutable reference to the underlying writer

Source

pub fn into_inner(self) -> W

Consume the encoder and return the underlying writer

Source

pub fn write_bytes_chunked<R: Read>( &mut self, reader: &mut R, total_size: u64, chunk_size: usize, ) -> ChieResult<()>

Write large byte array in chunks to avoid excessive memory allocation

§Examples
use chie_shared::encoding::BinaryEncoder;

let data = vec![1u8; 10_000]; // 10KB of data
let mut reader = &data[..];
let mut buf = Vec::new();
let mut encoder = BinaryEncoder::new(&mut buf);

// Write in 1KB chunks
encoder.write_bytes_chunked(&mut reader, 10_000, 1024).unwrap();
§Errors

Returns error if writing fails

Source

pub fn copy_from_reader<R: Read>(&mut self, reader: &mut R) -> ChieResult<u64>

Stream data directly from a reader without buffering entire payload

§Errors

Returns error if copying fails

Auto Trait Implementations§

§

impl<W> Freeze for BinaryEncoder<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for BinaryEncoder<W>
where W: RefUnwindSafe,

§

impl<W> Send for BinaryEncoder<W>
where W: Send,

§

impl<W> Sync for BinaryEncoder<W>
where W: Sync,

§

impl<W> Unpin for BinaryEncoder<W>
where W: Unpin,

§

impl<W> UnwindSafe for BinaryEncoder<W>
where W: UnwindSafe,

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.