serialize

Function serialize 

Source
pub fn serialize<CFG, W, T>(writer: W, value: &T) -> Result<()>
where CFG: Cfg, W: Write, T: Serialize + ?Sized,
Expand description

Serialize a value of type T to a std::io::Write.

The CFG parameter controls the serialization format and can be either:

  • Full: Serializes struct field identifiers and enum variant identifiers as strings
  • Slim: Serializes without identifiers, using indices for enum variants

ยงExample

use serde::{Serialize, Deserialize};
use postbag::{serialize, cfg::Full};

#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Person {
    name: String,
    age: u32,
}

let person = Person {
    name: "Alice".to_string(),
    age: 30,
};

let mut buffer = Vec::new();
serialize::<Full, _, _>(&mut buffer, &person).unwrap();
println!("Serialized {} bytes", buffer.len());