Function sbp::to_writer

source · []
pub fn to_writer<W, M>(writer: W, msg: &M) -> Result<(), Error> where
    W: Write,
    M: SbpMessage
Expand description

Serialize the given message into the IO stream.

Example

use sbp::messages::logging::MsgLog;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let text = String::from("hello");
    let msg = MsgLog {
        sender_id: Some(1),
        level: 1,
        text: text.clone().into(),
    };
    let mut writer = Vec::new();
    sbp::to_writer(&mut writer, &msg)?;
    assert_eq!(
        &writer[sbp::HEADER_LEN + 1..writer.len() - sbp::CRC_LEN],
        text.as_bytes()
    );
    Ok(())
}