pack

Function pack 

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

Convenience function to pack a value to bytes with magic number.

This function adds the pack magic number (0xDADA) at the beginning of the data. The packed format is compact but not schema-evolution-friendly.

§Arguments

  • value - The value to pack.

§Example

use senax_encoder::{pack, unpack, Pack, Unpack};
use bytes::BytesMut;

#[derive(Pack, Unpack, 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);