encode_to

Function encode_to 

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

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

This function adds the encode magic number (0xA55A) at the current position in the buffer and provides schema evolution support through field IDs and type tags.

§Arguments

  • value - The value to encode.
  • writer - The buffer to write the encoded bytes into.

§Example

use senax_encoder::{encode_to, decode, Encode, Decode};
use bytes::{BytesMut, Bytes};

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

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