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
impl FieldEncoder
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new encoder with the given initial capacity.
Sourcepub fn write_bool(&mut self, v: bool)
pub fn write_bool(&mut self, v: bool)
Writes a bool as a single byte (1 for true, 0 for false).
Sourcepub fn write_string(&mut self, s: &str)
pub fn write_string(&mut self, s: &str)
Writes a length-prefixed UTF-8 string.
Sourcepub fn write_bytes(&mut self, b: &[u8])
pub fn write_bytes(&mut self, b: &[u8])
Writes a length-prefixed byte slice.
Sourcepub fn write_option_string(&mut self, s: &Option<String>)
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.
Sourcepub fn write_option_u64(&mut self, v: &Option<u64>)
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.
Trait Implementations§
Source§impl Clone for FieldEncoder
impl Clone for FieldEncoder
Source§fn clone(&self) -> FieldEncoder
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for FieldEncoder
impl Debug for FieldEncoder
Auto Trait Implementations§
impl Freeze for FieldEncoder
impl RefUnwindSafe for FieldEncoder
impl Send for FieldEncoder
impl Sync for FieldEncoder
impl Unpin for FieldEncoder
impl UnsafeUnpin for FieldEncoder
impl UnwindSafe for FieldEncoder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more