Function pack

Source
pub fn pack<T: Encoder>(value: &T) -> Result<Bytes>
Expand description

Convenience function to pack a value to bytes.

This is equivalent to calling value.pack(writer) but provides a more ergonomic API. Unlike encode, this does not store field IDs and is not schema-evolution-friendly.

§Arguments

  • value - The value to pack.

§Example

use senax_encoder::{pack, unpack, Encode, Decode};
use bytes::BytesMut;

#[derive(Encode, Decode, PartialEq, Debug)]
struct MyStruct {
    id: u32,
    name: String,
}

let value = MyStruct { id: 42, name: "hello".to_string() };
let mut buf = pack(&value).unwrap();
let decoded: MyStruct = unpack(&mut buf).unwrap();
assert_eq!(value, decoded);