Function kafka_serde::to_writer [−][src]
pub fn to_writer<W, T>(writer: W, value: &T) -> Result<W> where
T: Serialize,
W: Write,
Serializes a kafka payload into a I/O stream
Often times the kafka protocol will require the message to be manipulated after serializing (for instance to add the size to the beginning), so the initial stream is returned.
Examples
use serde::Serialize; use std::io::{Write, Cursor}; #[derive(Serialize, Debug)] struct RequestHeader { api_key: i16, api_version: i16, correlation_id: i32, client_id: &'static str, } let req = RequestHeader { api_key: 0, api_version: 0, correlation_id: 1, client_id: "" }; let mut x = Cursor::new(Vec::<u8>::with_capacity(std::mem::size_of::<RequestHeader>())); x.set_position(4u64); // leave space for the size; let mut x = kafka_serde::to_writer(x, &req).unwrap(); let sz = (x.position() as usize - std::mem::size_of::<i32>()) as i32; x.set_position(0u64); x.write(&sz.to_be_bytes()).unwrap(); // writes the size to the beginning of the payload