Skip to main content

to_msgpack

Function to_msgpack 

Source
pub fn to_msgpack<T: ToMessagePack>(value: &T, buf: &mut [u8]) -> Result<usize>
Expand description

Serializes a value of type T into the provided buffer, returning the number of bytes written.

§Errors

Serialization can fail if T’s implementation of ToMessagePack returns an error, or if the provided buffer is too small.

§Examples

#[derive(zerompk::ToMessagePack)]
struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let point = Point { x: 1, y: 2 };
    let mut buf = [0u8; 10];
    let bytes_written = zerompk::to_msgpack(&point, &mut buf).unwrap();

    assert_eq!(bytes_written, 3);
    assert_eq!(&buf[..bytes_written], &[0x92, 0x01, 0x02]);
}