pack_to

Function pack_to 

Source
pub fn pack_to<T: Packer>(value: &T, writer: &mut BytesMut) -> Result<()>
Expand description

Convenience function to pack a value to an existing BytesMut buffer with magic number.

This function adds the pack magic number (0xDADA) at the current position in the buffer. The packed format is compact but not schema-evolution-friendly.

§Arguments

  • value - The value to pack.
  • writer - The buffer to write the packed bytes into.

§Example

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

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

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