Skip to main content

FieldEncoder

Struct FieldEncoder 

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

A sequential field encoder that appends typed values to a byte buffer.

Each write_* method appends a value in a format that the corresponding FieldDecoder::read_* method can reconstruct. Variable-length values (strings, byte slices) are length-prefixed with a 4-byte big-endian u32.

§Example

use noxu_persist::simple_serializer::{FieldEncoder, FieldDecoder};

let mut enc = FieldEncoder::new();
enc.write_u64(42);
enc.write_string("hello");
enc.write_bool(true);
let bytes = enc.finish();

let mut dec = FieldDecoder::new(&bytes);
assert_eq!(dec.read_u64().unwrap(), 42);
assert_eq!(dec.read_string().unwrap(), "hello");
assert_eq!(dec.read_bool().unwrap(), true);

Implementations§

Source§

impl FieldEncoder

Source

pub fn new() -> Self

Creates a new, empty encoder.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new encoder with the given initial capacity.

Source

pub fn write_u8(&mut self, v: u8)

Writes a u8.

Source

pub fn write_u16(&mut self, v: u16)

Writes a u16 in big-endian byte order.

Source

pub fn write_u32(&mut self, v: u32)

Writes a u32 in big-endian byte order.

Source

pub fn write_u64(&mut self, v: u64)

Writes a u64 in big-endian byte order.

Source

pub fn write_i8(&mut self, v: i8)

Writes an i8.

Source

pub fn write_i16(&mut self, v: i16)

Writes an i16 in big-endian byte order.

Source

pub fn write_i32(&mut self, v: i32)

Writes an i32 in big-endian byte order.

Source

pub fn write_i64(&mut self, v: i64)

Writes an i64 in big-endian byte order.

Source

pub fn write_f32(&mut self, v: f32)

Writes a f32 in big-endian byte order.

Source

pub fn write_f64(&mut self, v: f64)

Writes a f64 in big-endian byte order.

Source

pub fn write_bool(&mut self, v: bool)

Writes a bool as a single byte (1 for true, 0 for false).

Source

pub fn write_string(&mut self, s: &str)

Writes a length-prefixed UTF-8 string.

Source

pub fn write_bytes(&mut self, b: &[u8])

Writes a length-prefixed byte slice.

Source

pub fn write_option_string(&mut self, s: &Option<String>)

Writes an optional string. Encodes a leading bool tag followed by the string value when Some.

Source

pub fn write_option_u64(&mut self, v: &Option<u64>)

Writes an optional u64. Encodes a leading bool tag followed by the value when Some.

Source

pub fn finish(self) -> Vec<u8>

Consumes the encoder and returns the accumulated byte buffer.

Source

pub fn len(&self) -> usize

Returns the number of bytes written so far.

Source

pub fn is_empty(&self) -> bool

Returns true if no bytes have been written.

Trait Implementations§

Source§

impl Clone for FieldEncoder

Source§

fn clone(&self) -> FieldEncoder

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FieldEncoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for FieldEncoder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.